57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
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)
|
|
}
|