Service-Funktion (z.B. als Windows Service) eingebaut

This commit is contained in:
Sven Schmalle 2020-02-27 21:09:41 +01:00
parent 33b5e370ae
commit 6dd7fe8580
2 changed files with 79 additions and 5 deletions

View File

@ -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)
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
```
----

63
main.go
View File

@ -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
}