forked from dasfaha/sky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skyd.go
132 lines (111 loc) · 2.86 KB
/
skyd.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"./skyd"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"runtime"
)
//------------------------------------------------------------------------------
//
// Constants
//
//------------------------------------------------------------------------------
const (
defaultPort = 8585
defaultDataDir = "/var/lib/sky"
)
const (
portUsage = "the port to listen on"
dataDirUsage = "the data directory"
)
const (
pidPath = "/var/run/skyd.pid"
)
//------------------------------------------------------------------------------
//
// Variables
//
//------------------------------------------------------------------------------
var port uint
var dataDir string
//------------------------------------------------------------------------------
//
// Functions
//
//------------------------------------------------------------------------------
//--------------------------------------
// Initialization
//--------------------------------------
func init() {
flag.UintVar(&port, "port", defaultPort, portUsage)
flag.UintVar(&port, "p", defaultPort, portUsage+"(shorthand)")
flag.StringVar(&dataDir, "data-dir", defaultDataDir, dataDirUsage)
flag.StringVar(&dataDir, "d", defaultDataDir, dataDirUsage+"(shorthand)")
}
//--------------------------------------
// Main
//--------------------------------------
func main() {
// Parse the command line arguments.
flag.Parse()
// Hardcore parallelism right here.
runtime.GOMAXPROCS(runtime.NumCPU())
// Initialize
server := skyd.NewServer(port, dataDir)
writePidFile()
//setupSignalHandlers(server)
// Start the server up!
c := make(chan bool)
err := server.ListenAndServe(c)
if err != nil {
fmt.Printf("%v\n", err)
cleanup(server)
return
}
<- c
cleanup(server)
}
//--------------------------------------
// Signals
//--------------------------------------
// Handles signals received from the OS.
func setupSignalHandlers(server *skyd.Server) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for _ = range c {
fmt.Fprintln(os.Stderr, "Shutting down...")
cleanup(server)
fmt.Fprintln(os.Stderr, "Shutdown complete.")
os.Exit(1)
}
}()
}
//--------------------------------------
// Utility
//--------------------------------------
// Shuts down the server socket and closes the database.
func cleanup(server *skyd.Server) {
if server != nil {
server.Shutdown()
}
deletePidFile()
}
// Writes a file to /var/run that contains the current process id.
func writePidFile() {
pid := fmt.Sprintf("%d", os.Getpid())
if err := ioutil.WriteFile(pidPath, []byte(pid), 0644); err != nil {
fmt.Fprintf(os.Stderr, "Unable to write pid file: %v\n", err)
}
}
// Deletes the pid file.
func deletePidFile() {
if _, err := os.Stat(pidPath); !os.IsNotExist(err) {
if err = os.Remove(pidPath); err != nil {
fmt.Fprintf(os.Stderr, "Unable to remove pid file: %v\n", err)
}
}
}