Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chai2010/winsvc
windows service
https://github.com/chai2010/winsvc
Last synced: 2 months ago
JSON representation
windows service
- Host: GitHub
- URL: https://github.com/chai2010/winsvc
- Owner: chai2010
- License: 0bsd
- Created: 2015-08-31T05:52:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-06-26T05:38:36.000Z (over 1 year ago)
- Last Synced: 2024-10-24T21:45:36.587Z (2 months ago)
- Language: Go
- Homepage: https://godoc.org/github.com/chai2010/winsvc
- Size: 11.7 KB
- Stars: 69
- Watchers: 3
- Forks: 21
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
- *Go语言QQ群: 102319854, 1055927514*
- *凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa*----
# Windows service
[![GoDoc](https://godoc.org/github.com/chai2010/winsvc?status.svg)](https://godoc.org/github.com/chai2010/winsvc)
## Install
`go get github.com/chai2010/winsvc`
## Example
```Go
package mainimport (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time""github.com/chai2010/winsvc"
)var (
appPath stringflagServiceName = flag.String("service-name", "hello-winsvc", "Set service name")
flagServiceDesc = flag.String("service-desc", "hello windows service", "Set service description")flagServiceInstall = flag.Bool("service-install", false, "Install service")
flagServiceUninstall = flag.Bool("service-remove", false, "Remove service")
flagServiceStart = flag.Bool("service-start", false, "Start service")
flagServiceStop = flag.Bool("service-stop", false, "Stop service")flagHelp = flag.Bool("help", false, "Show usage and exit.")
)func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage:
hello [options]...Options:
`)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "%s\n", `
Example:
# run hello server
$ go build -o hello.exe hello.go
$ hello.exe# install hello as windows service
$ hello.exe -service-install# start/stop hello service
$ hello.exe -service-start
$ hello.exe -service-stop# remove hello service
$ hello.exe -service-remove# help
$ hello.exe -hReport bugs to .`)
}// change to current dir
var err error
if appPath, err = winsvc.GetAppPath(); err != nil {
log.Fatal(err)
}
if err := os.Chdir(filepath.Dir(appPath)); err != nil {
log.Fatal(err)
}
}func main() {
flag.Parse()// install service
if *flagServiceInstall {
if err := winsvc.InstallService(appPath, *flagServiceName, *flagServiceDesc); err != nil {
log.Fatalf("installService(%s, %s): %v\n", *flagServiceName, *flagServiceDesc, err)
}
fmt.Printf("Done\n")
return
}// remove service
if *flagServiceUninstall {
if err := winsvc.RemoveService(*flagServiceName); err != nil {
log.Fatalln("removeService:", err)
}
fmt.Printf("Done\n")
return
}// start service
if *flagServiceStart {
if err := winsvc.StartService(*flagServiceName); err != nil {
log.Fatalln("startService:", err)
}
fmt.Printf("Done\n")
return
}// stop service
if *flagServiceStop {
if err := winsvc.StopService(*flagServiceName); err != nil {
log.Fatalln("stopService:", err)
}
fmt.Printf("Done\n")
return
}// run as service
if !winsvc.InServiceMode() {
log.Println("main:", "runService")
if err := winsvc.RunAsService(*flagServiceName, StartServer, StopServer, false); err != nil {
log.Fatalf("svc.Run: %v\n", err)
}
return
}// run as normal
StartServer()
}func StartServer() {
log.Println("StartServer, port = 8080")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "winsrv server", time.Now())
})
http.ListenAndServe(":8080", nil)
}func StopServer() {
log.Println("StopServer")
}
```BUGS
====Report bugs to .
Thanks!