-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkulloserver.go
151 lines (129 loc) · 4.06 KB
/
kulloserver.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2013–2020 Kullo GmbH
*
* This source code is licensed under the 3-clause BSD license. See LICENSE.txt
* in the root directory of this source tree for details.
*/
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"golang.org/x/text/language"
"bitbucket.org/kullo/server/dao"
"bitbucket.org/kullo/server/dbconn"
"bitbucket.org/kullo/server/logging"
"bitbucket.org/kullo/server/notifications"
"bitbucket.org/kullo/server/util"
"bitbucket.org/kullo/server/webservice"
"github.com/emicklei/go-restful"
"github.com/kylelemons/go-gypsy/yaml"
_ "github.com/lib/pq"
)
func openDb(dbEnvironment string, configDir string) {
conf, err := yaml.ReadFile(configDir + "/dbconf.yml")
if err != nil {
log.Fatal(err)
}
dbstr, err := conf.Get(dbEnvironment + ".open")
if err != nil {
log.Fatal(err)
}
err = dbconn.Open("postgres", dbstr)
if err != nil {
panic(err)
}
}
func statusHandler(rw http.ResponseWriter, req *http.Request) {
users := dao.Users{}
_, err := users.UserExists("hi#kullo.net")
if err != nil {
util.LogServerError(err)
rw.WriteHeader(http.StatusInternalServerError)
io.WriteString(rw, "HTTP 500\n\nCouldn't access database")
return
}
rw.WriteHeader(http.StatusOK)
io.WriteString(rw, "HTTP 200\n\nUp and running")
}
func main() {
// configure logging (should be the first thing to be executed)
log.SetFlags(log.Flags() | log.Lshortfile)
// command line parsing
port := flag.Int("port", 8001, "Server port")
configDir := flag.String("configDir", "./config", "configuration directory")
dbEnvironment := flag.String("env", "local", "database configuration environment name")
domain := flag.String("domain", "kullo.test", "domain part of this server's addresses")
gcmApiKey := flag.String("gcmApiKey", "", "API key for Google Cloud Messaging")
accessLogFile := flag.String("accessLogFile", "/tmp/kulloserver-access.log", "file name of the access log")
errorLogFile := flag.String("errorLogFile", "", "file name of the error log")
cpuProfile := flag.String("cpuprofile", "", "write cpu profile to given file")
memProfile := flag.String("memprofile", "", "write memory profile to given file")
flag.Parse()
logging.OpenErrorLog(*errorLogFile)
defer logging.CloseErrorLog()
logging.OpenAccessLog(*accessLogFile)
defer logging.CloseAccessLog()
// CPU profiling
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// handle SIGINT (Ctrl+C)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGUSR1)
go func() {
for {
sig := <-signalChan
switch sig {
case syscall.SIGINT:
// write memory profile
if *memProfile != "" {
f, err := os.Create(*memProfile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
os.Exit(0)
case syscall.SIGUSR1:
// reopen logs
log.Println("Reopening logs")
logging.OpenErrorLog(*errorLogFile)
logging.OpenAccessLog(*accessLogFile)
}
}
}()
// status page
http.HandleFunc("/status", statusHandler)
// open DB
openDb(*dbEnvironment, *configDir)
defer dbconn.Close()
webservice.SetAvailableLanguages(language.English, language.German)
// set up restful
restful.Filter(logging.AccessLoggingFilter())
restful.Filter(webservice.LanguageFilter)
restful.DefaultResponseContentType(restful.MIME_JSON)
restful.PrettyPrintResponses = false
restful.Add(webservice.NewAccounts(*domain).RestfulWebService)
restful.Add(webservice.NewAccount().RestfulWebService)
restful.Add(webservice.NewMessages().RestfulWebService)
restful.Add(webservice.NewKeysSymm().RestfulWebService)
restful.Add(webservice.NewKeysAsymm().RestfulWebService)
restful.Add(webservice.NewPush().RestfulWebService)
restful.Add(webservice.NewProfile().RestfulWebService)
notifications.StartWorkers(*gcmApiKey)
log.Print(fmt.Sprintf("Starting HTTP server for %s on port %d ...", *domain, *port))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}