-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticator.go
83 lines (72 loc) · 2.51 KB
/
authenticator.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
// Only contains code for running the service. Brings together logic from various
// other services.
package main
import (
"database/sql"
"log"
"net/http"
"text/template"
"github.com/codegangsta/negroni"
"github.com/coopernurse/gorp"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"github.com/julienschmidt/httprouter"
"github.com/nyc-camp/authenticator/libtmpl"
"github.com/nyc-camp/authenticator/libuser"
)
var userStorageMySQL UserStorageMySQL
var sessionStorage sessions.Store
func init() {
db, err := sql.Open("mysql", "authenticator:authenticator@tcp(localhost:3306)/authenticator?parseTime=true")
if err != nil {
log.Printf("%v\n", err)
log.Panic("An error occured while connecting to the database.")
}
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
dbmap.AddTableWithName(libuser.User{}, "user").SetKeys(false, "uid")
userStorageMySQL = UserStorageMySQL{Dbmap: dbmap}
sessionStorage = sessions.NewCookieStore([]byte("7Iow7KmwXj5x9e3q41396e4pd1A31Rme"), []byte("3vC4APPo2HoBY9AhguVz8EU24D0n0I5G"))
}
func main() {
tmplCfg := libtmpl.HTMLTemplateConfig{TemplateDir: "templates/", DefaultErrorFunc: libuser.HandleError}
router := httprouter.New()
router.NotFound = func(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/404.html")
if err != nil {
return
}
t.ExecuteTemplate(w, "html", nil)
}
recoveryMiddleware := negroni.NewRecovery()
recoveryMiddleware.PrintStack = false
n := negroni.New(recoveryMiddleware, negroni.NewLogger(), negroni.NewStatic(http.Dir("public")))
userRegistration := libuser.UserRegistration{
Storage: userStorageMySQL,
TemplateConfig: tmplCfg,
SessionStore: sessionStorage,
}
userLogin := libuser.UserLogin{
Storage: userStorageMySQL,
TemplateConfig: tmplCfg,
SessionStore: sessionStorage,
}
userAccount := libuser.UserAccount{
Storage: userStorageMySQL,
TemplateConfig: tmplCfg,
SessionStore: sessionStorage,
}
userLogout := libuser.UserLogout{
Storage: userStorageMySQL,
TemplateConfig: tmplCfg,
SessionStore: sessionStorage,
}
router.GET("/register", userRegistration.GetRegistrationForm)
router.POST("/register", userRegistration.HandleRegistrationSubmission)
router.GET("/login", userLogin.LoginForm)
router.POST("/login", userLogin.LoginSubmission)
router.GET("/account", userAccount.AccountPage)
router.GET("/logout", userLogout.Logout)
n.UseHandler(context.ClearHandler(router))
n.Run(":4567")
}