File: service_test.go

package info (click to toggle)
golang-gopkg-hlandau-service.v2 2.0.16-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 156 kB
  • sloc: makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package service_test

import "gopkg.in/hlandau/service.v2"

// The following example illustrates the minimal skeleton structure to
// implement a daemon. This example can run as a service on Windows or a daemon
// on Linux.  The systemd notify protocol is supported.
func Example() {
	service.Main(&service.Info{
		Title:       "Foobar Web Server",
		Name:        "foobar",
		Description: "Foobar Web Server is the greatest webserver ever.",

		RunFunc: func(smgr service.Manager) error {
			// Start up your service.
			// ...

			// Once initialization requiring root is done, call this.
			err := smgr.DropPrivileges()
			if err != nil {
				return err
			}

			// When it is ready to serve requests, call this.
			// You must call DropPrivileges first.
			smgr.SetStarted()

			// Optionally set a status
			smgr.SetStatus("foobar: running ok")

		loop:
			for {
				select {
				// Handle requests, or do so in another goroutine controlled from here.
				case <-smgr.StopChan():
					break loop
				}
			}

			// Do any necessary teardown.
			// ...

			return nil
		},
	})
}

// © 2015 Hugo Landau <hlandau@devever.net>  ISC License