This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (86 loc) · 3.33 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
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
package main
import (
"fmt"
"net/http"
"github.com/urfave/negroni"
mgosession "github.com/RoflCopter24/citation-db/nigronimgosession"
"github.com/RoflCopter24/negroni-sessions"
"github.com/RoflCopter24/citation-db/settings"
"github.com/RoflCopter24/citation-db/middleware"
"github.com/RoflCopter24/citation-db/handlers"
"gopkg.in/mgo.v2"
"log"
"os"
"strconv"
"github.com/RoflCopter24/negroni-sessions/mongostore"
"github.com/gorilla/securecookie"
)
var (
appSettings settings.AppSettings
)
func setupMgo(n *negroni.Negroni, s *settings.AppSettings) *mgo.Session {
fmt.Println("Connecting to MongoDB: ", s.DbServer)
fmt.Println("Database Name: ", s.DbName)
connStr := s.GenMgoConnStr()
// Creating the database accessor here.
// Pointer to this database accessor will be passed to the middleware.
dbAccessor, err := mgosession.NewDatabaseAccessor(connStr, s.DbName, "users")
if err != nil {
panic(err)
}
// Registering the middleware here.
n.Use(mgosession.NewDatabase(*dbAccessor).Middleware())
return dbAccessor.Clone()
}
func main() {
appSettings = settings.AppSettings{}
appSettings.LffOrDefault()
n := negroni.Classic()
// Setup MongoDb connection stuff
s := setupMgo(n, &appSettings)
n.Use(negroni.NewRecovery())
n.Use(negroni.NewLogger())
store := mongostore.New(*s, appSettings.DbName, "sessions", 200000, true, securecookie.GenerateRandomKey(16) , securecookie.GenerateRandomKey(16))
//store := cookiestore.New([]byte("citation-db.C_Store01"))
n.Use(sessions.Sessions("CitationSession", store))
whiteList := make([]string,5)
whiteList[0] = "/login"
whiteList[1] = "/css/"
whiteList[2] = "/js/"
whiteList[3] = "/img/"
whiteList[4] = "/favicon.ico"
ac := middleware.AuthChecker{ UsersDB: appSettings.DbName, UsersCollection: "users", WhiteList: whiteList }
n.Use(ac.Middleware())
mux := http.NewServeMux()
mux.HandleFunc("/", handlers.HandleIndex)
mux.HandleFunc("/start", handlers.HandleStart)
mux.HandleFunc("/login", handlers.HandleLogin)
mux.HandleFunc("/logout", handlers.HandleLogout)
mux.HandleFunc("/books/add", handlers.HandleBooksAdd)
mux.HandleFunc("/books", handlers.HandleBooksIndex)
mux.HandleFunc("/books/", handlers.HandleBooksIndex)
mux.HandleFunc("/books/list", handlers.HandleBooksList)
mux.HandleFunc("/books/search", handlers.HandleBooksSearchJSON)
mux.HandleFunc("/books/edit", handlers.HandleBooksEdit)
mux.HandleFunc("/books/edit/", handlers.HandleBooksEdit)
mux.HandleFunc("/books/remove/", handlers.HandleBooksDelete)
mux.HandleFunc("/quotes/", handlers.HandleQuotesIndex)
mux.HandleFunc("/quotes/search", handlers.HandleQuotesSearch)
mux.HandleFunc("/quotes/search/", handlers.HandleQuotesSearch)
mux.HandleFunc("/quotes/add", handlers.HandleQuotesAdd)
mux.HandleFunc("/quotes/edit", handlers.HandleQuotesEdit)
mux.HandleFunc("/quotes/edit/", handlers.HandleQuotesEdit)
mux.HandleFunc("/quotes/remove/", handlers.HandleQuotesRemove)
mux.Handle("/img", http.FileServer(http.Dir("img")))
mux.Handle("/js", http.FileServer(http.Dir("js")))
mux.Handle("/css", http.FileServer(http.Dir("css")))
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
})
log.Println(os.Getwd())
if appSettings.WorkingDir != "" {
os.Chdir(appSettings.WorkingDir)
}
n.UseHandler(mux)
n.Run(":" + strconv.Itoa(appSettings.Port))
}