/
main.go
127 lines (101 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
122
123
124
125
126
127
package main
import (
"encoding/gob"
"fmt"
"net/http"
"time"
"github.com/Sirupsen/logrus"
"github.com/carbocation/interpose"
gorilla_mux "github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/heynickc/fpc/dal"
"github.com/heynickc/fpc/handlers"
"github.com/heynickc/fpc/libenv"
"github.com/heynickc/fpc/libunix"
"github.com/heynickc/fpc/middlewares"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/tylerb/graceful"
)
func init() {
gob.Register(&dal.UserRow{})
}
// NewApplication is the constructor for Application struct.
func NewApplication() (*Application, error) {
u, err := libunix.CurrentUser()
if err != nil {
return nil, err
}
dsn := libenv.EnvWithDefault("DSN", fmt.Sprintf("postgres://%v@localhost:5432/fpc?sslmode=disable", u))
db, err := sqlx.Connect("postgres", dsn)
if err != nil {
return nil, err
}
cookieStoreSecret := libenv.EnvWithDefault("COOKIE_SECRET", "qhXi3KCGubTYzcMQ")
app := &Application{}
app.dsn = dsn
app.db = db
app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret))
return app, err
}
// Application is the application object that runs HTTP server.
type Application struct {
dsn string
db *sqlx.DB
cookieStore *sessions.CookieStore
}
func (app *Application) middlewareStruct() (*interpose.Middleware, error) {
middle := interpose.New()
middle.Use(middlewares.SetDB(app.db))
middle.Use(middlewares.SetCookieStore(app.cookieStore))
middle.UseHandler(app.mux())
return middle, nil
}
func (app *Application) mux() *gorilla_mux.Router {
MustLogin := middlewares.MustLogin
router := gorilla_mux.NewRouter()
router.HandleFunc("/", handlers.GetLanding).Methods("GET")
router.HandleFunc("/schedule", handlers.GetSchedule).Methods("GET")
router.HandleFunc("/product", handlers.PostProduct).Methods("POST")
router.Handle("/home", MustLogin(http.HandlerFunc(handlers.GetHome))).Methods("GET")
router.HandleFunc("/signup", handlers.GetSignup).Methods("GET")
router.HandleFunc("/signup", handlers.PostSignup).Methods("POST")
router.HandleFunc("/login", handlers.GetLogin).Methods("GET")
router.HandleFunc("/login", handlers.PostLogin).Methods("POST")
router.HandleFunc("/logout", handlers.GetLogout).Methods("GET")
router.Handle("/users/{id:[0-9]+}", MustLogin(http.HandlerFunc(handlers.PostPutDeleteUsersID))).Methods("POST", "PUT", "DELETE")
// Path of static files must be last!
router.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
return router
}
func main() {
app, err := NewApplication()
if err != nil {
logrus.Fatal(err.Error())
}
middle, err := app.middlewareStruct()
if err != nil {
logrus.Fatal(err.Error())
}
serverAddress := libenv.EnvWithDefault("HTTP_ADDR", ":8888")
certFile := libenv.EnvWithDefault("HTTP_CERT_FILE", "")
keyFile := libenv.EnvWithDefault("HTTP_KEY_FILE", "")
drainIntervalString := libenv.EnvWithDefault("HTTP_DRAIN_INTERVAL", "1s")
drainInterval, err := time.ParseDuration(drainIntervalString)
if err != nil {
logrus.Fatal(err.Error())
}
srv := &graceful.Server{
Timeout: drainInterval,
Server: &http.Server{Addr: serverAddress, Handler: middle},
}
logrus.Infoln("Running HTTP server on " + serverAddress)
if certFile != "" && keyFile != "" {
err = srv.ListenAndServeTLS(certFile, keyFile)
} else {
err = srv.ListenAndServe()
}
if err != nil {
logrus.Fatal(err.Error())
}
}