rclone/vendor/github.com/sevlyar/go-daemon/examples/cmd/gd-simple/simple.go

49 lines
881 B
Go
Raw Normal View History

package main
import (
"fmt"
"github.com/sevlyar/go-daemon"
"html"
"log"
"net/http"
)
// To terminate the daemon use:
// kill `cat pid`
func main() {
cntxt := &daemon.Context{
PidFileName: "pid",
PidFilePerm: 0644,
LogFileName: "log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Args: []string{"[go-daemon sample]"},
}
d, err := cntxt.Reborn()
if err != nil {
log.Fatal("Unable to run: ", err)
}
if d != nil {
return
}
defer cntxt.Release()
log.Print("- - - - - - - - - - - - - - -")
log.Print("daemon started")
serveHttp()
}
func serveHttp() {
http.HandleFunc("/", httpHandler)
http.ListenAndServe("127.0.0.1:8080", nil)
}
func httpHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("request from %s: %s %q", r.RemoteAddr, r.Method, r.URL)
fmt.Fprintf(w, "go-daemon: %q", html.EscapeString(r.URL.Path))
}