-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
106 lines (83 loc) · 1.9 KB
/
server.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
package main
import (
"github.com/Junbong/mankato-server/servers"
"github.com/Junbong/mankato-server/configs"
"github.com/Junbong/mankato-server/db/collections"
"gopkg.in/yaml.v2"
"flag"
"log"
"fmt"
"os"
"os/signal"
"syscall"
"io/ioutil"
"sync"
)
const (
version = "0.0.1"
)
var (
ophost = flag.String("h", "localhost", "Set host address of server")
opport = flag.Int("p", 7120, "Set port number of server")
opconf = flag.String("conf", "./etc/configuration.yml", "Configuration path")
config *configs.Config
)
func init() {
log.Println("Project Mankato", version)
// Parse program flags and read configurations
flag.Parse()
// Load configuration
if dat, err := ioutil.ReadFile(*opconf); err == nil {
config = &configs.Config{}
if err := yaml.Unmarshal([]byte(dat), config); err == nil {
// Use host command option first
if config.Server.Host != *ophost {
config.Server.Host = *ophost
}
// Use port command option first
if config.Server.Port != *opport {
config.Server.Port = *opport
}
} else {
panic(err)
}
} else {
panic(err)
}
log.Printf("Use configuration %s", *opconf)
}
func main() {
// Initialize database
col := collection.New(config.Collection.DefaultName, config)
col.Open()
// Start router & server
svr := server.NewRouter(config, col)
svr.SetupRoutes()
// Run server and watch system signal
watchSysSigs(svr.StartServe, svr.StopServe)
// Called when shutdown
defer shutdownGraceful(col)
}
func watchSysSigs(fn1, fn2 func()) {
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fn1()
}()
select {
case sig := <-sigs:
fmt.Println()
log.Println(sig, "signal")
}
fn2()
wg.Wait()
}
func shutdownGraceful(col *collection.Collection) {
log.Println("Shutdown graceful...")
// Shutdown
col.Close()
log.Println("Bye :]")
}