-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (40 loc) · 1.67 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
package main
import (
"log"
"net/http"
"github.com/colin353/markdown.ninja/config"
"github.com/colin353/markdown.ninja/models"
"github.com/colin353/markdown.ninja/requesthandler"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
)
// AppConfig contains the application configuration, which is loaded
// from the config yaml files and overridden by environment variables.
var AppConfig *config.Config
func main() {
// Load the configuration file, and distribute it to the modules.
AppConfig = config.LoadConfig("./config")
models.AppConfig = AppConfig
requesthandler.AppConfig = AppConfig
// Connect to redis.
models.Connect()
// If we are in testing mode, we must delete the database contents.
if AppConfig.Mode == "test" || AppConfig.Mode == "testing" {
models.ClearDatabase()
}
// Set up the cookie store.
requesthandler.SessionStore = sessions.NewCookieStore([]byte(AppConfig.CookieSecret))
// Set up routing.
http.HandleFunc("/api/auth/", requesthandler.CreateHandler(NewAuthenticationHandler()))
http.HandleFunc("/api/edit/", requesthandler.CreateAuthenticatedHandler(NewEditHandler()))
http.HandleFunc("/api/files/", requesthandler.CreateAuthenticatedHandler(NewFileHandler()))
http.HandleFunc("/api/account/", requesthandler.CreateAuthenticatedHandler(NewAccountHandler()))
http.HandleFunc("/edit/", requesthandler.ReactHandler)
http.HandleFunc("/favicon.ico", http.FileServer(http.Dir("./web")).ServeHTTP)
http.HandleFunc("/", requesthandler.SubdomainHandler)
// Start up the server.
err := http.ListenAndServe(":"+AppConfig.Port, context.ClearHandler(http.DefaultServeMux))
if err != nil {
log.Fatalf("Unable to start server: %v", err.Error())
}
}