From 6dd7fe85809c697615cf4c68f0e610c86f2ce2d2 Mon Sep 17 00:00:00 2001 From: Sven Schmalle Date: Thu, 27 Feb 2020 21:09:41 +0100 Subject: [PATCH] Service-Funktion (z.B. als Windows Service) eingebaut --- README.md | 21 +++++++++++++++++-- main.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cc1be64..ca6de10 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,25 @@ Zum kompilieren folgende Go-Abhängigkeiten installieren: - `go get github.com/mandolyte/mdtopdf` - `go get github.com/blevesearch/bleve` - `go get github.com/shuLhan/go-bindata` + - `go get github.com/kardianos/service` - Die Build-Scripte liegen im Unterordner `build` -Download der Binaries unter: [https://nc.masilux.de/index.php/s/dGRdPsa6XPPiyQk](https://nc.masilux.de/index.php/s/dGRdPsa6XPPiyQk) \ No newline at end of file +Download der Binaries unter: [https://nc.masilux.de/index.php/s/dGRdPsa6XPPiyQk](https://nc.masilux.de/index.php/s/dGRdPsa6XPPiyQk) + + +---- +#### Installation als Dienst unter Windows: +``` +sc create go-wiki-srv binpath="P:\Go\gowiki\gowiki_amd64.exe" start=delayed-auto DisplayName="GoWiki" +sc start go-wiki-srv +sc query go-wiki-srv +``` + +Deinstallation: +``` +sc stop go-wiki-srv +sc delete go-wiki-srv +``` + +---- diff --git a/main.go b/main.go index b5c6899..d722786 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,64 @@ import ( "github.com/blevesearch/bleve" "github.com/blevesearch/bleve/search/highlight/highlighter/html" "github.com/gorilla/mux" + "github.com/kardianos/service" "github.com/mandolyte/mdtopdf" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing/object" ) -var config = readConfig("") +//------------------------------------------------------------------------------------------------- +// Service Configuration +var logger service.Logger + +type program struct{} + +func (p *program) Start(s service.Service) error { + // Start should not block. Do the actual work async. + go p.run() + return nil +} +func (p *program) run() { + wikimain() +} +func (p *program) Stop(s service.Service) error { + // Stop should not block. Return with a few seconds. + return nil +} + +func main() { + svcConfig := &service.Config{ + Name: "GoWiki", + DisplayName: "GoWiki", + Description: "GoWiki Service", + } + + prg := &program{} + s, err := service.New(prg, svcConfig) + if err != nil { + log.Fatal(err) + } + if len(os.Args) > 1 { + err = service.Control(s, os.Args[1]) + if err != nil { + log.Fatal(err) + } + return + } + + logger, err = s.Logger(nil) + if err != nil { + log.Fatal(err) + } + err = s.Run() + if err != nil { + logger.Error(err) + } +} + +//------------------------------------------------------------------------------------------------- + +var config *Configuration type Configuration struct { Host string @@ -35,7 +87,12 @@ type Configuration struct { } // our main function -func main() { +func wikimain() { + // Startverzeichnis auslesen und als Arbeitsverzeichnis setzen + dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + os.Chdir(dir) + + config = readConfig(path.Join(dir, "config.json")) // Ausgeben der Config-Optionen fmt.Println("Host: " + config.Host) @@ -423,7 +480,7 @@ func readConfig(filename string) *Configuration { // initialize conf with default values. conf := &Configuration{Host: "http://127.0.0.1", Port: "8000", DataPath: "./data", DataPathFTS: "", WebUser: "", WebPasswd: ""} - b, err := ioutil.ReadFile("./config.json") + b, err := ioutil.ReadFile(filename) if err != nil { return conf }