-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.go
69 lines (51 loc) · 1.78 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
package main
import (
"flag"
"net/http"
"github.com/golang/glog"
"github.com/gorilla/context"
"github.com/haruyama/golang-goji-sample/controllers"
"github.com/haruyama/golang-goji-sample/system"
"github.com/zenazn/goji"
"github.com/zenazn/goji/graceful"
"github.com/zenazn/goji/web"
)
func main() {
filename := flag.String("config", "config.toml", "Path to configuration file")
flag.Parse()
defer glog.Flush()
var application = &system.Application{}
application.Init(filename)
application.LoadTemplates()
// Setup static files
static := web.New()
publicPath := application.Config.Get("general.public_path").(string)
static.Get("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir(publicPath))))
http.Handle("/assets/", static)
// Apply middleware
goji.Use(application.ApplyTemplates)
goji.Use(application.ApplySessions)
goji.Use(application.ApplyDbMap)
goji.Use(application.ApplyAuth)
goji.Use(application.ApplyIsXhr)
goji.Use(application.ApplyCsrfProtection)
goji.Use(context.ClearHandler)
controller := &controllers.MainController{}
// Couple of files - in the real world you would use nginx to serve them.
goji.Get("/robots.txt", http.FileServer(http.Dir(publicPath)))
goji.Get("/favicon.ico", http.FileServer(http.Dir(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()
}