-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
103 lines (90 loc) · 1.82 KB
/
main.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/kardianos/service"
)
const defaultPort = 8081
var logger service.Logger
type program struct {
port int
}
func (p program) Start(s service.Service) error {
go startHttp(p.port)
return nil
}
func (p program) Stop(s service.Service) error {
stopHttp()
return nil
}
func main() {
conf := &service.Config{
Name: "HnTopRss",
DisplayName: "HN Top Links RSS",
Description: "RSS feed for Hacker News Top Links",
}
prg := &program{defaultPort}
s, err := service.New(prg, conf)
if err != nil {
log.Fatal(err)
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
if len(os.Args) > 1 {
var err error
verb := os.Args[1]
switch verb {
case "install":
err = s.Install()
if err != nil {
fmt.Printf("Failed to install: %s\n", err)
return
}
fmt.Printf("Service \"%s\" installed.\n", conf.DisplayName)
case "uninstall":
err = s.Uninstall()
if err != nil {
fmt.Printf("Failed to uninstall: %s\n", err)
return
}
fmt.Printf("Service \"%s\" removed.\n", conf.DisplayName)
case "run":
err = s.Run()
if err != nil {
fmt.Printf("Failed to run: %s\n", err)
return
}
case "start":
err = s.Start()
if err != nil {
fmt.Printf("Failed to start: %s\n", err)
return
}
fmt.Printf("Service \"%s\" started.\n", conf.DisplayName)
case "stop":
err = s.Stop()
if err != nil {
fmt.Printf("Failed to stop: %s\n", err)
return
}
fmt.Printf("Service \"%s\" stopped.\n", conf.DisplayName)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}
func startHttp(port int) {
logger.Infof("Listening on port %d", port)
http.HandleFunc("/", hnRssHandler)
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func stopHttp() {
logger.Info("Shutting down")
}