-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
executable file
·222 lines (176 loc) · 5.3 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
// Standard libs
"encoding/gob"
"fmt"
"log"
"net/http"
"path"
"github.com/ab22/abcd/config"
"github.com/ab22/abcd/handlers"
"github.com/ab22/abcd/httputils"
"github.com/ab22/abcd/routes"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
"golang.org/x/net/context"
)
// Server contains instance details for the server.
type Server struct {
cfg *config.Config
cookieStore *sessions.CookieStore
router *mux.Router
db *gorm.DB
}
// NewServer returns a new instance of the server. All server configuration
// is done at the Configure() function.
func NewServer() *Server {
var (
err error
server = &Server{}
)
log.Println("Configuring server...")
server.cfg = config.NewConfig()
server.cfg.Print()
err = server.cfg.Validate()
if err != nil {
log.Fatalln(err)
}
log.Println("Creating database connection...")
server.db, err = server.createDatabaseConn()
if err != nil {
log.Fatalln(err)
}
server.configureCookieStore()
log.Println("Setting up routes...")
server.configureRouter()
log.Println("Creating static file server...")
server.createStaticFilesServer()
return server
}
// ListenAndServe attaches the current server to the specified configuration
// port.
func (s *Server) ListenAndServe() error {
return http.ListenAndServe(
fmt.Sprintf(":%d", s.cfg.App.Port),
s.router,
)
}
// createDatabaseConn creates a new GORM database with the specified database
// configuration.
func (s *Server) createDatabaseConn() (*gorm.DB, error) {
var (
db *gorm.DB
err error
dbCfg = s.cfg.Db
connectionString = fmt.Sprintf(
"host=%v port=%v user=%v password=%v dbname=%v sslmode=disable",
dbCfg.Host,
dbCfg.Port,
dbCfg.User,
dbCfg.Password,
dbCfg.Name,
)
)
db, err = gorm.Open("postgres", connectionString)
if err != nil {
return nil, err
}
db.DB().SetMaxIdleConns(10)
db.LogMode(s.cfg.DbLogMode)
return db, nil
}
// configureCookieStore creates the cookie store used to validate user
// sessions.
func (s *Server) configureCookieStore() {
secretKey := s.cfg.App.Secret
gob.Register(&httputils.SessionData{})
s.cookieStore = sessions.NewCookieStore([]byte(secretKey))
s.cookieStore.MaxAge(0)
}
// configureRouter initializes the server's router.
func (s *Server) configureRouter() {
s.router = mux.NewRouter().StrictSlash(true)
r := routes.NewRoutes(s.cfg, s.db)
s.bindRoutes(r.TemplateRoutes, false)
s.bindRoutes(r.APIRoutes, true)
}
// bindRoutes adds all routes to the server's router.
func (s *Server) bindRoutes(r []routes.Route, apiRoute bool) {
for _, route := range r {
var routePath string
httpHandler := s.makeHTTPHandler(route)
if apiRoute {
routePath = "/api/" + route.Pattern()
} else {
routePath = route.Pattern()
}
s.router.
Methods(route.Method()).
Path(routePath).
HandlerFunc(httpHandler)
}
}
// createStaticFilesServer creates a static file server to server all of the
// frontend files (html, js, css, etc).
func (s *Server) createStaticFilesServer() {
var (
adminAppPath = s.cfg.App.Frontend.Admin
staticFilesPath = path.Join(adminAppPath, "static")
)
contextHandler := func(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
file := path.Join(staticFilesPath, req.URL.Path)
http.ServeFile(w, req, file)
return nil
}
contextHandler = handlers.HandleHTTPError(contextHandler)
contextHandler = handlers.GzipContent(contextHandler)
contextHandler = handlers.NoDirListing(contextHandler)
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := context.Background()
err := contextHandler(ctx, w, req)
if err != nil {
log.Printf("static file handler [%s][%s] returned error: %s", req.Method, req.URL.Path, err)
}
})
s.router.
PathPrefix("/static/").
Handler(http.StripPrefix("/static/", handler))
}
// makeHTTPHandler creates a http.HandlerFunc from a httputils.ContextHandler.
func (s *Server) makeHTTPHandler(route routes.Route) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
err error
handlerFunc httputils.ContextHandler
ctx = context.Background()
)
handlerFunc = s.handleWithMiddlewares(route)
err = handlerFunc(ctx, w, r)
if err != nil {
log.Printf("Handler [%s][%s] returned error: %s", r.Method, r.URL.Path, err)
}
}
}
// handleWithMiddlewares applies all middlewares to the specified route. Some
// middleware functions are applied depending on the route's properties, such
// as ValidateAuth and Authorize middlewares. These last 2 functions require
// that the route RequiresAuth() and that RequiredRoles() > 0.
func (s *Server) handleWithMiddlewares(route routes.Route) httputils.ContextHandler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
serverCtx := context.WithValue(ctx, "cookieStore", s.cookieStore)
serverCtx = context.WithValue(serverCtx, "route", route)
serverCtx = context.WithValue(serverCtx, "config", s.cfg)
h := route.HandlerFunc()
h = handlers.HandleHTTPError(h)
h = handlers.GzipContent(h)
if route.RequiresAuth() {
if requiredRoles := route.RequiredRoles(); len(requiredRoles) > 0 {
h = handlers.Authorize(h)
}
h = handlers.ValidateAuth(h)
}
return h(serverCtx, w, r)
}
}