Http server ready!

This commit is contained in:
Senad Uka
2020-03-16 22:24:08 +01:00
parent 70c5999748
commit 289d6238d0
2 changed files with 59 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.log
*.pid

56
main.go Normal file
View File

@@ -0,0 +1,56 @@
package main
import (
"github.com/sevlyar/go-daemon"
"gitlab.com/saburly/kiviscraplib/structures"
"gitlab.com/saburly/kiviscraplib/webserver"
"log"
)
var queue chan structures.Request
var end chan string
// To terminate the daemon use:
// kill `cat sample.pid`
func main() {
cntxt := &daemon.Context{
PidFileName: "sample.pid",
PidFilePerm: 0644,
LogFileName: "sample.log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Args: []string{"[kivi scraping load balancer]"},
}
d, err := cntxt.Reborn()
if err != nil {
log.Fatal("Unable to run: ", err)
}
if d != nil {
return
}
defer cntxt.Release()
// todo: put queue length in env variable
queue = make(chan structures.Request, 1000)
end = make(chan string)
go webserver.ServeHTTP(queue, end)
go func() {
for {
request := <-queue
//time.Sleep(2 * time.Second)
response := structures.Response{
Url: request.Url,
Content: []byte("<html><body><h1>Bla!</h1></body><html>"),
Err: nil,
}
request.Response <- response
}
}()
ended := <-end
log.Printf("%s ended so closing the daemon", ended)
}