From 289d6238d0947dd9538b67b1e4790010b509e8a3 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Mon, 16 Mar 2020 22:24:08 +0100 Subject: [PATCH] Http server ready! --- .gitignore | 3 +++ main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 .gitignore create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d13c162 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.log +*.pid + diff --git a/main.go b/main.go new file mode 100644 index 0000000..e8ed204 --- /dev/null +++ b/main.go @@ -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("

Bla!

"), + Err: nil, + } + request.Response <- response + } + }() + + ended := <-end + + log.Printf("%s ended so closing the daemon", ended) +}