-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
100 lines (81 loc) · 1.89 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
package main
import (
"log"
"net/http"
_ "net/http/pprof"
"os"
"regexp"
"bishack.dev/handler"
mw "bishack.dev/middleware"
// autoload env
"github.com/aws/aws-xray-sdk-go/xray"
_ "github.com/joho/godotenv/autoload"
"github.com/gorilla/csrf"
"github.com/gorilla/pat"
)
func main() {
// csrf
csrfSecure := true
// env
rxEnv := regexp.MustCompile("`(?i)stag|prod")
isLive := rxEnv.MatchString(os.Getenv("UP_STAGE"))
// init route
r := pat.New()
// not found
r.NotFoundHandler = http.HandlerFunc(func(
w http.ResponseWriter,
r *http.Request,
) {
if r.URL.Path == "/" {
handler.Home(w, r)
} else {
handler.NotFound(w, r)
}
})
// handlers et al
// auth
r.Get("/signup", handler.Signup)
r.Get("/verify", handler.Verify)
r.Get("/login", handler.LoginForm)
r.Get("/logout", handler.Logout)
r.Post("/signup", handler.FinishSignup)
r.Post("/login", handler.Login)
// profile
r.Get("/profile", handler.Profile)
r.Post("/profile", handler.UpdateProfile)
// security
r.Get("/security", handler.Security)
r.Post("/security", handler.ChangePassword)
// like
r.Put("/like/{id}", handler.ToggleLike)
// slack
r.Get("/slack-invite", handler.SlackInvite)
// post
r.Post("/update-post", handler.UpdatePost)
r.Get("/edit/{id}", handler.EditPost)
r.Get("/new", handler.New)
r.Post("/new", handler.CreatePost)
r.Get("/{username}/{id}", handler.GetPost)
r.Get("/{username}", handler.GetUserPosts)
// on local
if !isLive {
// set secure to false
csrfSecure = false
// launch nerdy stuff(pprof) server
go func() {
_ = http.ListenAndServe(":6060", nil)
}()
}
protect := csrf.Protect([]byte(
os.Getenv("CSRF_KEY")),
csrf.Secure(csrfSecure),
)
port := ":" + os.Getenv("PORT")
log.Fatal(http.ListenAndServe(
port,
xray.Handler(
xray.NewFixedSegmentNamer("bishack.dev"),
protect(mw.Context(mw.Token(mw.SessionUser(mw.AuthRedirects(r))))),
),
))
}