/
server.go
41 lines (32 loc) · 934 Bytes
/
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
package main
import (
"github.com/crob1140/codewiz-server/datastore"
"github.com/crob1140/codewiz-server/models/users"
"github.com/crob1140/codewiz-server/models/wizards"
"github.com/crob1140/codewiz-server/routes/api"
"github.com/crob1140/codewiz-server/routes/views"
"github.com/gorilla/mux"
"net/http"
)
const (
apiPath = "/api"
viewsPath = "/"
)
type Server struct {
Router http.Handler
}
func NewServer(db *datastore.DB) *Server {
userDao := users.NewDao(db)
wizardDao := wizards.NewDao(db)
router := mux.NewRouter()
// Add API endpoints
apiRouter := api.NewRouter(apiPath, userDao)
router.PathPrefix(apiPath).Handler(apiRouter)
// Add view endpoints
viewsRouter := views.NewRouter(viewsPath, userDao, wizardDao)
router.PathPrefix(viewsPath).Handler(viewsRouter)
return &Server{Router: router}
}
func (server *Server) ListenAndServe(address string) {
http.ListenAndServe(address, server.Router)
}