-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d577c3
commit 1417b55
Showing
13 changed files
with
159 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ _testmain.go | |
*.test | ||
*.prof | ||
.vs | ||
/deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app | ||
|
||
import "time" | ||
|
||
// The wrapper of your app | ||
func yourApp(s server) { | ||
|
||
s.winlog.Info(1, "In app.yourApp") | ||
|
||
// This is just some sample code to do something | ||
time.Sleep(1 * time.Second) | ||
s.winlog.Info(1, "Still running") | ||
|
||
time.Sleep(2 * time.Second) | ||
s.winlog.Info(1, "And running") | ||
|
||
time.Sleep(3 * time.Second) | ||
s.winlog.Info(1, "But the service will keep running") | ||
|
||
// Notice that if this exits, the service continues to run | ||
// You can launch web servers, etc. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"golang.org/x/sys/windows/svc/debug" | ||
) | ||
|
||
// Run launches the service | ||
func Run(wl *debug.Log, svcName, sha1ver string) error { | ||
|
||
s, err := setup(wl, svcName, sha1ver) | ||
if err != nil { | ||
return errors.Wrap(err, "setup") | ||
} | ||
|
||
// Your service should be launched as a GO routine | ||
go yourApp(s) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app | ||
|
||
import ( | ||
"golang.org/x/sys/windows/svc/debug" | ||
) | ||
|
||
type server struct { | ||
winlog debug.Log | ||
// a local logger | ||
// a database connection | ||
// your app configuration | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/sys/windows/svc/debug" | ||
) | ||
|
||
// if setup returns an error, the service doesn't start | ||
func setup(wl *debug.Log, svcName, sha1ver string) (server, error) { | ||
var s server | ||
|
||
// did we get a full SHA1? | ||
if len(sha1ver) == 40 { | ||
sha1ver = sha1ver[0:7] | ||
} | ||
|
||
if sha1ver == "" { | ||
sha1ver = "dev" | ||
} | ||
|
||
s.winlog = *wl | ||
|
||
// Note: any logging here goes to Windows App Log | ||
// I suggest you setup local logging | ||
s.winlog.Info(1, fmt.Sprintf("%s: setup (%s)", svcName, sha1ver)) | ||
|
||
// read configuration | ||
// configure more logging | ||
|
||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2012 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/billgraziano/go-windows-svc/app" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// This is the name you will use for the NET START command | ||
const svcName = "gosvc" | ||
|
||
// This is the name that will appear in the Services control panel | ||
const svcNameLong = "GO Service" | ||
|
||
// This is assigned the full SHA1 hash from GIT | ||
var sha1ver string | ||
|
||
func svcLauncher() error { | ||
|
||
err := app.Run(&elog, svcName, sha1ver) | ||
if err != nil { | ||
return errors.Wrap(err, "app.run") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
TARGETDIR=.\deploy | ||
proj=github.com\billgraziano\go-windows-svc | ||
sha1ver := $(shell git rev-parse HEAD) | ||
test := $(shell date /t) | ||
|
||
|
||
all: vet test buildEXE | ||
|
||
vet: | ||
go vet -all -shadow .\cmd\gosvc | ||
go vet -all -shadow .\app | ||
|
||
test: | ||
go.exe test -timeout 30s $(proj)\app | ||
|
||
# The sha1 stuff isn't working as of now | ||
buildEXE: | ||
go build -o "$(TARGETDIR)\gosvc.exe" -a -ldflags "-X main.sha1ver=$(sha1ver)" .\cmd\gosvc |