/
main.go
84 lines (65 loc) · 1.68 KB
/
main.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
84
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/codegangsta/negroni"
"github.com/goincremental/negroni-sessions"
"github.com/goincremental/negroni-sessions/cookiestore"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
"github.com/unrolled/render"
_ "github.com/lib/pq"
)
type ctx struct {
db *sqlx.DB
r *render.Render
}
type View struct {
Title string
Script string
Name string
Error string
Data interface{}
}
func main() {
ren := render.New(render.Options{
Layout: "shared/layout",
IsDevelopment: true,
})
//sessions
store := cookiestore.New([]byte("secret"))
//db
// db, err := sqlx.Connect("postgres", os.Getenv("CONNECTIONSTRING"))
db, err := sqlx.Connect("postgres", "user=travisjones dbname=cm_app sslmode=disable")
if err != nil {
log.Println("Error initializing database...")
log.Fatalln(err)
}
c := ctx{db, ren}
n := negroni.New()
nauth := negroni.New()
//routers
router := mux.NewRouter()
authRouter := mux.NewRouter()
//AUTHORIZED ROUTES
authRouter.HandleFunc("/", c.Home)
router.Handle("/", nauth).Methods("GET")
//OPEN ROUTES
router.HandleFunc("/account/login", c.Login).Methods("GET")
router.HandleFunc("/account/login", c.LoginPost).Methods("POST")
router.HandleFunc("/account/signup", c.Signup).Methods("GET")
router.HandleFunc("/account/signup", c.SignupPost).Methods("POST")
router.HandleFunc("/account/logout", c.Logout).Methods("GET")
//Middleware
nauth.Use(negroni.HandlerFunc(RequireAuth))
nauth.UseHandler(authRouter)
//Sessions
n.Use(sessions.Sessions("authex", store))
n.Use(negroni.NewStatic(http.Dir("public")))
n.UseHandler(router)
n.Run(
fmt.Sprint(":", os.Getenv("PORT")),
)
}