-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·85 lines (72 loc) · 2.99 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
package main
import (
"flag"
"fmt"
"net/http"
"github.com/TerrenceHo/simplephotohost.com/controllers"
"github.com/TerrenceHo/simplephotohost.com/middleware"
"github.com/TerrenceHo/simplephotohost.com/models"
"github.com/TerrenceHo/simplephotohost.com/rand"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
)
func main() {
boolPtr := flag.Bool("prod", false, "Provide this flag in production. This ensures that a .config file is provided before the application starts.")
flag.Parse()
cfg := LoadConfig(*boolPtr)
dbCfg := cfg.Database
services, err := models.NewServices(
models.WithGorm(dbCfg.Dialect(), dbCfg.ConnectionInfo()),
models.WithLogMode(!cfg.IsProd()),
models.WithUser(cfg.Pepper, cfg.HMACKey),
models.WithGallery(),
models.WithImage(),
)
must(err)
defer services.Close()
services.AutoMigrate()
r := mux.NewRouter()
staticC := controllers.NewStatic()
usersC := controllers.NewUsers(services.User)
galleriesC := controllers.NewGalleries(services.Gallery, services.Image, r)
b, err := rand.Bytes(32)
must(err)
csrfMw := csrf.Protect(b, csrf.Secure(cfg.IsProd()))
userMw := middleware.User{
UserService: services.User,
}
requireUserMw := middleware.RequireUser{
User: userMw,
}
r.Handle("/", staticC.Home).Methods("GET")
r.Handle("/contact", staticC.Contact).Methods("GET")
r.HandleFunc("/signup", usersC.New).Methods("GET")
r.HandleFunc("/signup", usersC.Create).Methods("POST")
r.Handle("/login", usersC.LoginView).Methods("GET")
r.HandleFunc("/login", usersC.Login).Methods("POST")
// Assets
assetHandler := http.FileServer(http.Dir("./assets/"))
assetHandler = http.StripPrefix("/assets/", assetHandler)
r.PathPrefix("/assets/").Handler(assetHandler)
// Image routes
imageHandler := http.FileServer(http.Dir("./images/"))
r.PathPrefix("/images/").Handler(http.StripPrefix("/images/", imageHandler))
// Gallery routes
r.Handle("/galleries", requireUserMw.ApplyFn(galleriesC.Index)).Methods("GET")
r.Handle("/galleries/new", requireUserMw.Apply(galleriesC.New)).Methods("GET")
r.HandleFunc("/galleries", requireUserMw.ApplyFn(galleriesC.Create)).Methods("POST")
r.HandleFunc("/galleries/{id:[0-9]+}/edit", requireUserMw.ApplyFn(galleriesC.Edit)).Methods("GET").Name(controllers.EditGallery)
r.HandleFunc("/galleries/{id:[0-9]+}/update", requireUserMw.ApplyFn(galleriesC.Update)).Methods("POST")
r.HandleFunc("/galleries/{id:[0-9]+}/delete", requireUserMw.ApplyFn(galleriesC.Delete)).Methods("POST")
r.HandleFunc("/galleries/{id:[0-9]+}/images", requireUserMw.ApplyFn(galleriesC.ImageUpload)).Methods("POST")
// /galleries/:id/images/:filename/delete
r.HandleFunc("/galleries/{id:[0-9]+}/images/{filename}/delete", requireUserMw.ApplyFn(galleriesC.ImageDelete)).Methods("POST")
r.HandleFunc("/galleries/{id:[0-9]+}", galleriesC.Show).Methods("GET").Name(controllers.ShowGallery)
fmt.Printf("Starting the server on :%d...\n", cfg.Port)
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), csrfMw(userMw.Apply(r)))
}
func must(err error) {
if err != nil {
panic(err)
}
}