-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (90 loc) · 2.85 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"sockets/controllers"
"sockets/middleware"
"sockets/models"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
type spaHandler struct {
staticPath string
indexPath string
}
// ServeHTTP serves static js assets
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path, err := filepath.Abs(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
path = filepath.Join(h.staticPath, path)
_, err = os.Stat(path)
if os.IsNotExist(err) {
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
csrf.TemplateField(r)
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}
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.JWTSecret),
models.WithFriend(),
)
must(err)
defer services.Close()
services.AutoMigrate()
userMw := middleware.User{
UserService: services.User,
}
requireUserMw := middleware.RequireUser{
User: userMw,
}
r := mux.NewRouter()
usersC := controllers.NewUsers(services.User)
friendsC := controllers.NewFriends(services.Friend, r)
r.HandleFunc("/api/auth", usersC.Load).Methods("GET")
r.HandleFunc("/api/signup", usersC.Create).Methods("POST")
r.HandleFunc("/api/login", usersC.Login).Methods("POST")
r.HandleFunc("/api/logout", requireUserMw.ApplyFn(usersC.Logout)).Methods("POST")
r.HandleFunc("/api/friends", friendsC.Index).Methods("GET")
r.HandleFunc("/api/friends", friendsC.Create).Methods("POST")
spa := spaHandler{staticPath: "client/build", indexPath: "index.html"}
r.PathPrefix("/").Handler(spa)
fmt.Printf("Starting the server on :%d...\n", cfg.Port)
headersOk := handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "Authorization"})
originsOk := handlers.AllowedOrigins([]string{"http://localhost:3000", "http://localhost:5000"})
methodsOk := handlers.AllowedMethods([]string{"POST", "GET", "OPTIONS", "PUT", "DELETE"})
credentialsOk := handlers.AllowCredentials()
corsHandler := handlers.CORS(originsOk, headersOk, methodsOk, credentialsOk)(userMw.Apply(r))
srv := &http.Server{
Handler: corsHandler,
Addr: fmt.Sprintf(":%d", cfg.Port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func must(err error) {
if err != nil {
panic(err)
}
}