This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (60 loc) · 2.46 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
package main
import (
"encoding/base64"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
abclientstate "github.com/volatiletech/authboss-clientstate"
"github.com/zoe-gonzales/meet-up-do-stuff/api"
"github.com/zoe-gonzales/meet-up-do-stuff/auth"
"github.com/zoe-gonzales/meet-up-do-stuff/user"
)
func main() {
cookieKey := base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(64))
sessionKey := base64.StdEncoding.EncodeToString(securecookie.GenerateRandomKey(64))
cookieStoreKey, _ := base64.StdEncoding.DecodeString(cookieKey)
sessionStoreKey, _ := base64.StdEncoding.DecodeString(sessionKey)
auth.CookieStore = abclientstate.NewCookieStorer(cookieStoreKey, nil)
auth.CookieStore.HTTPOnly = false
auth.CookieStore.Secure = false
auth.SessionStore = abclientstate.NewSessionStorer(auth.SessionCookieName, sessionStoreKey, nil)
cstore := auth.SessionStore.Store.(*sessions.CookieStore)
cstore.Options.HttpOnly = false
cstore.Options.Secure = false
cstore.MaxAge(int((30 * 24 * time.Hour) / time.Second))
user.InitUserModel()
auth.InitAuth()
r := mux.NewRouter()
// public endpoints
r.HandleFunc("/login", api.AuthenticateUser).Methods("POST")
r.HandleFunc("/signup", api.RegisterNewUser).Methods("POST")
r.HandleFunc("/logout", api.LogOutUser).Methods("POST")
r.HandleFunc("/profile/{id}", api.GetProfile).Methods("GET")
r.HandleFunc("/events", api.GetAllEvents).Methods("GET")
r.HandleFunc("/events/{id}", api.GetSingleEvent).Methods("GET")
// Restricted endpoints
s := r.PathPrefix("/user/{userID}").Subrouter()
s.Use(auth.VerifyCookie)
s.HandleFunc("/event/{id}", api.GetSingleEvent).Methods("GET")
s.HandleFunc("/eventsbyowner", api.GetEventsByOwners).Methods("GET")
s.HandleFunc("/events", api.GetUsersEvents).Methods("GET")
s.HandleFunc("/profile/{id}", api.UpdateProfile).Methods("PUT")
s.HandleFunc("/", api.GetUserByID).Methods("GET")
s.HandleFunc("/account", api.UpdateUserDetails).Methods("PUT")
s.HandleFunc("/account", api.DeleteUser).Methods("DELETE")
s.HandleFunc("/event", api.AddEvent).Methods("POST")
s.HandleFunc("/event/{id}", api.UpdateEvent).Methods("PUT")
s.HandleFunc("/event/{id}", api.DeleteEvent).Methods("DELETE")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// static files
r.PathPrefix("/").Handler(http.StripPrefix("/web", http.FileServer(http.Dir("./web"))))
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":"+port, r))
}