forked from elcct/defaultproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
64 lines (47 loc) · 1.67 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
package main
import (
"flag"
"github.com/golang/glog"
"net/http"
"github.com/elcct/defaultproject/controllers/web"
"github.com/elcct/defaultproject/system"
"github.com/zenazn/goji"
"github.com/zenazn/goji/graceful"
gojiweb "github.com/zenazn/goji/web"
)
func main() {
filename := flag.String("config", "config.json", "Path to configuration file")
flag.Parse()
defer glog.Flush()
var application = &system.Application{}
application.Init(filename)
application.LoadTemplates()
application.ConnectToDatabase()
// Setup static files
static := gojiweb.New()
static.Get("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir(application.Configuration.PublicPath))))
http.Handle("/assets/", static)
// Apply middleware
goji.Use(application.ApplyTemplates)
goji.Use(application.ApplySessions)
goji.Use(application.ApplyDatabase)
goji.Use(application.ApplyAuth)
controller := &web.Controller{}
// Couple of files - in the real world you would use nginx to serve them.
goji.Get("/robots.txt", http.FileServer(http.Dir(application.Configuration.PublicPath)))
goji.Get("/favicon.ico", http.FileServer(http.Dir(application.Configuration.PublicPath+"/images")))
// Home page
goji.Get("/", application.Route(controller, "Index"))
// Sign In routes
goji.Get("/signin", application.Route(controller, "SignIn"))
goji.Post("/signin", application.Route(controller, "SignInPost"))
// Sign Up routes
goji.Get("/signup", application.Route(controller, "SignUp"))
goji.Post("/signup", application.Route(controller, "SignUpPost"))
// KTHXBYE
goji.Get("/logout", application.Route(controller, "Logout"))
graceful.PostHook(func() {
application.Close()
})
goji.Serve()
}