-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (102 loc) · 3.15 KB
/
Copy pathmain.go
File metadata and controls
143 lines (102 loc) · 3.15 KB
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
package main
import (
"fmt"
"html/template"
"net/http"
"reflect"
"path"
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/render"
"github.com/codegangsta/martini-contrib/binding"
"labix.org/v2/mgo"
"hp/event"
"hp/db"
"hp/conf"
)
func HomeHandler(r render.Render) {
type templateData struct {
Context *conf.Context
}
data := templateData{conf.DefaultContext(conf.Config)}
r.HTML(200, "home", data)
}
func NotFoundHandler(r render.Render) {
type templateData struct {
Context *conf.Context
}
data := templateData{conf.DefaultContext(conf.Config)}
r.HTML(200, "404", data)
}
func FacebookChannelHandler(w http.ResponseWriter, req *http.Request) {
var fbchannel = template.Must(template.ParseFiles(
path.Join(conf.Config.ProjectRoot, "templates/channel.html"),
))
type templateData struct {
Context *conf.Context
}
data := templateData{conf.DefaultContext(conf.Config)}
fbchannel.Execute(w, data)
}
func FacebookLoginHandler(r render.Render) {
// simple static page for user to click on fb connect button
type templateData struct {
Context *conf.Context
}
data := templateData{conf.DefaultContext(conf.Config)}
r.HTML(200, "facebook_login", data)
}
func FacebookAuthHandler(w http.ResponseWriter, req *http.Request) {
// construct fb graph's oauth end-point, then redirect user to this end-point
}
func FacebookRedirectHandler(w http.ResponseWriter, req *http.Request) {
// returns here
}
// DB Returns a martini.Handler
func DB(dbName string) martini.Handler {
session, err := mgo.Dial("mongodb://localhost")
if err != nil {
panic(err)
}
return func(c martini.Context) {
s := session.Clone()
c.Map(s.DB(dbName))
defer s.Close()
c.Next()
}
}
func main() {
fmt.Printf("Using config %s\n", conf.Path)
fmt.Printf("Using models:\n")
for _, m := range db.Models {
t := reflect.TypeOf(m)
fmt.Printf(" %s\n", fmt.Sprintf("%s", t)[1:])
}
// Establish session with mongodb
db.Connect(conf.Config.DbHostString(), conf.Config.DbName)
db.RegisterAllIndexes()
m := martini.Classic()
m.Use(martini.Static("static"))
m.Use(DB(conf.Config.DbName))
m.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout", // Specify a layout template
Charset: "UTF-8", // Sets encoding for json and html content-types. Default is "UTF-8".
//IndentJSON: true, // Output human readable JSON
}))
m.NotFound(NotFoundHandler)
m.Get("/", HomeHandler)
m.Get("/events", event.EventListHandler)
m.Get("/events/past", event.EventPastHandler)
m.Get("/events/next", event.EventNextHandler)
m.Get("/organise", event.OrganiseHandler)
m.Get("/event/add", event.EventAddHandler)
// Facebook related features
// one-off link that allows event owner to grab group-specific events set with group-only perms
m.Get("/facebook/login", FacebookLoginHandler)
m.Get("/channel.html", FacebookChannelHandler)
m.Get("/events/grab", event.EventGrabHandler)
m.Post("/events/import", event.EventImportHandler)
m.Post("/events/register", binding.Bind(event.Attendee{}), event.RegisterEventAttendeeHandler)
m.Get("/event/:eid", event.ShowEventAttendees)
m.Run()
}