forked from ab22/abcd
/
server.go
256 lines (205 loc) · 6.11 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
// Standard libs
"encoding/gob"
"fmt"
"log"
"net/http"
"path"
// Routes
"github.com/ab22/abcd/router/auth"
"github.com/ab22/abcd/router/static"
"github.com/ab22/abcd/router/student"
"github.com/ab22/abcd/router/user"
// Misc.
"github.com/ab22/abcd/config"
"github.com/ab22/abcd/router"
"github.com/ab22/abcd/router/httputils"
"github.com/ab22/abcd/services"
"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 {
cookieStore *sessions.CookieStore
muxRouter *mux.Router
services *services.Services
routers []router.Router
cfg *config.Config
}
// NewServer returns a new instance of the server. All server configuration
// is done at the Configure() function.
func NewServer() *Server {
return &Server{}
}
// Configure initializes all necessary data for the server, including the
// configuration data, services and routes.
func (s *Server) Configure() error {
var err error
log.Println("Configuring server...")
s.cfg = config.NewConfig()
s.cfg.Print()
err = s.cfg.Validate()
if err != nil {
return err
}
err = s.configureServices()
if err != nil {
return err
}
s.configureCookieStore()
log.Println("Setting up routes...")
s.createMuxRouter()
s.configureRouters()
s.bindRoutes()
log.Println("Creating static file server...")
s.createStaticFilesServer()
return nil
}
// 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.muxRouter,
)
}
// 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
}
// configureServices creates the new services for the application to use.
func (s *Server) configureServices() error {
db, err := s.createDatabaseConn()
if err != nil {
return err
}
s.services = services.NewServices(db)
return nil
}
// configureCookieStore creates the cookie store used to validate user
// sessions.
func (s *Server) configureCookieStore() {
secretKey := s.cfg.App.Secret
gob.Register(&router.SessionData{})
s.cookieStore = sessions.NewCookieStore([]byte(secretKey))
s.cookieStore.MaxAge(30 * 60)
}
// addRouter appends a router to the server's router.
func (s *Server) addRouter(r router.Router) {
s.routers = append(s.routers, r)
}
// configureRouters adds all routers to the server.
func (s *Server) configureRouters() {
var appPath = s.cfg.App.Frontend.Admin
s.addRouter(static.NewRouter(appPath))
s.addRouter(auth.NewRouter())
s.addRouter(user.NewRouter())
s.addRouter(student.NewRouter())
}
// createMuxRouter initializes the server's router.
func (s *Server) createMuxRouter() {
s.muxRouter = mux.NewRouter().StrictSlash(true)
}
// bindRoutes adds all routes to the server's router.
func (s *Server) bindRoutes() {
for _, route := range s.routers {
for _, r := range route.Routes() {
var routePath string
httpHandler := s.makeHttpHandler(r)
if r.Type() == httputils.API {
routePath = "/api/" + r.Path()
} else {
routePath = r.Path()
}
s.muxRouter.
Methods(r.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 = router.NoDirListing(router.GzipContent(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)
httputils.WriteError(w, http.StatusInternalServerError, "")
}
})
s.muxRouter.
PathPrefix("/static/").
Handler(http.StripPrefix("/static/", handler))
}
// makeHttpHandler creates a http.HandlerFunc from a httputils.ContextHandler.
func (s *Server) makeHttpHandler(route router.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 router.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, "services", s.services)
h := route.Handler()
h = router.HandleHttpError(h)
h = router.GzipContent(h)
if route.RequiresAuth() {
if requiredRoles := route.RequiredRoles(); len(requiredRoles) > 0 {
h = router.Authorize(h)
}
h = router.ValidateAuth(h)
}
return h(serverCtx, w, r)
}
}