-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.83 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
package main
import (
"flag"
"fmt"
"github.com/Streamlet/NoteIsSite/config"
"github.com/Streamlet/NoteIsSite/global"
"github.com/Streamlet/NoteIsSite/server"
"github.com/Streamlet/NoteIsSite/util"
"os"
"os/signal"
"syscall"
)
type commandLineArgs struct {
config string
}
func main() {
var args commandLineArgs
flag.StringVar(&args.config, "config", "site.toml", "config file path")
flag.Parse()
err := config.LoadSiteConfig(args.config)
if err != nil {
fmt.Printf("failed to load %s: %s\n", args.config, err.Error())
return
}
conf := config.GetSiteConfig()
var srv server.HttpServer
if conf.Server.Sock != "" {
srv, err = server.NewSockServer(conf.Server.Sock, conf.Note.NoteRoot, conf.Template.TemplateRoot)
} else if conf.Server.Port > 0 {
srv, err = server.NewPortServer(conf.Server.Port, conf.Note.NoteRoot, conf.Template.TemplateRoot)
} else {
util.Assert(false, "neither port or sock was specified")
return
}
if err != nil {
fmt.Printf("failed to init server with node root '%s' and template root '%s': %s.\n", conf.Note.NoteRoot, conf.Template.TemplateRoot, err.Error())
return
}
global.InitErrorChan()
err = srv.Serve()
if err != nil {
fmt.Printf("failed to start server: %s.\n", err.Error())
return
}
fmt.Printf("Server started")
if conf.Server.Sock != "" {
fmt.Printf(" on sock '%s'.\n", conf.Server.Sock)
} else if conf.Server.Port > 0 {
fmt.Printf(" on port %d.\n", conf.Server.Port)
}
fmt.Printf("Note root: %s\n", conf.Note.NoteRoot)
fmt.Printf("Template root: %s\n", conf.Template.TemplateRoot)
fmt.Printf("\n")
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-sig:
fmt.Printf("Server shutdown.\n")
case err := <-global.GetErrorChan():
fmt.Printf("Server error: %s\n", err.Error())
}
break
}
_ = srv.Shutdown()
}