-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
146 lines (129 loc) · 4.52 KB
/
main.go
File metadata and controls
146 lines (129 loc) · 4.52 KB
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
package main
import (
"flag"
"fmt"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/inSituo/apiServer/Api"
"github.com/inSituo/apiServer/DBAccess"
"github.com/inSituo/apiServer/LeveledLogger"
"github.com/inSituo/apiServer/Middleware"
"net/http"
"os"
"runtime"
)
const (
SERVER_VER_MAJOR = 0
SERVER_VER_MINOR = 0
SERVER_VER_PATCH = 0
)
type GoogleConf struct {
clientId *string
clientSecret *string
redirectUrl *string
scope *string
}
type ServerConf struct {
mongo DBAccess.MongoConf
google GoogleConf
port *int
debug *bool
serverName *string
serverVer *string
}
func main() {
iname := "main"
conf := ServerConf{
debug: flag.Bool("debug", false, "Enable debug log messages"),
port: flag.Int("port", 80, "Server listening port"),
serverName: flag.String("servername", "InSituo API Server", "Server name for HTTP header"),
serverVer: flag.String(
"serverver",
fmt.Sprintf("v%d.%d.%d",
SERVER_VER_MAJOR,
SERVER_VER_MINOR,
SERVER_VER_PATCH),
"Server version for HTTP header",
),
mongo: DBAccess.MongoConf{
Port: flag.Int("mport", 27017, "MongoDB server port"),
Host: flag.String("mhost", "127.0.0.1", "MongoDB server host"),
DB: flag.String("mdb", "insituo-dev", "MongoDB database name"),
CUsers: flag.String("cusers", "users", "Name of users collection in DB"),
CLogins: flag.String("clogins", "logins", "Name of logins collection in DB"),
CQuestions: flag.String("cquestions", "questions", "Name of questions collection in DB"),
CAnswers: flag.String("canswers", "answers", "Name of answers collection in DB"),
},
google: GoogleConf{
clientId: flag.String("gclientid", "", "Google API client ID"),
clientSecret: flag.String("gclientsecret", "", "Google API client secret"),
redirectUrl: flag.String("gredirecturl", "", "Google API redirect URL"),
scope: flag.String("gscope", "", "Google API scope"),
},
}
flag.StringVar(&Api.API_KEY_REQ_HEADER, "apikeyheader", "X-API-KEY", "API key request-header name")
flag.StringVar(&Api.API_FMT_REQ_HEADER, "apifmtheader", "X-API-FORMAT", "API response format request-header name")
showHelp := flag.Bool("help", false, "Show help")
flag.Parse()
if *showHelp {
fmt.Printf("%s/%s\n\n", *conf.serverName, *conf.serverVer)
flag.PrintDefaults()
return
}
ll_level := LeveledLogger.LL_INFO
if *conf.debug {
ll_level = LeveledLogger.LL_DEBUG
}
log := LeveledLogger.New(os.Stdout, ll_level)
log.Debug(iname, "debug mode enabled")
log.Info(
iname,
"connecting to MongoDB",
*conf.mongo.Host,
*conf.mongo.Port,
*conf.mongo.DB,
)
db, err := DBAccess.New(conf.mongo)
if err != nil {
log.Error(iname, "failed to create a new db", err) // this will panic
}
defer db.Close()
// breakable middleware chain:
chain := Middleware.NewChain(true)
serverHttpHeader := fmt.Sprintf(
"%s/%s (%s)",
*conf.serverName,
*conf.serverVer,
runtime.GOOS,
)
log.Debug(iname, "'Server' HTTP header", serverHttpHeader)
chain.Push(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set(
"Server",
serverHttpHeader,
)
})
chain.Push(Middleware.RequestInfo(log, Api.API_KEY_REQ_HEADER))
if *conf.debug {
chain.Push(Middleware.RequestDebugInfo(log))
}
r := mux.NewRouter()
r.KeepContext = true
// init apis:
Api.InitAnswersApi(r.PathPrefix("/answers/").Subrouter(), db, log, Middleware.SetResponse)
chain.PushHandler(r)
// unbrakeable middleware:
ubchain := Middleware.NewChain(false)
ubchain.Push(chain.MakeHandler())
ubchain.Push(Middleware.AutoFormatResponder(log, Api.API_FMT_REQ_HEADER))
ubchain.Push(func(res http.ResponseWriter, req *http.Request) {
context.Clear(req)
log.Debug(iname, "request context cleared")
})
http.Handle("/", ubchain.MakeHandler())
addr := fmt.Sprintf(":%d", *conf.port)
log.Info(iname, "starting web service", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Error(iname, "failed to start server", err)
}
}