/
server.go
130 lines (115 loc) · 4.54 KB
/
server.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
128
129
130
package handlers
import (
"log"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/markbates/pkger"
"userstyles.world/handlers/api"
"userstyles.world/handlers/core"
"userstyles.world/handlers/jwt"
jwtware "userstyles.world/handlers/jwt"
oauthprovider "userstyles.world/handlers/oauthProvider"
"userstyles.world/handlers/style"
"userstyles.world/handlers/user"
"userstyles.world/modules/config"
"userstyles.world/modules/templates"
"userstyles.world/utils"
)
// Get proper IP depending on the environment.
func proxyHeader() (s string) {
if config.Production {
s = "X-Real-IP"
}
return s
}
func Initialize() {
app := fiber.New(fiber.Config{
Views: templates.New(),
ViewsLayout: "layouts/main",
ProxyHeader: proxyHeader(),
JSONEncoder: utils.JSONEncoder,
})
if !config.Production {
app.Use(logger.New())
}
app.Use(core.HSTSMiddleware)
app.Use(compress.New())
if config.Production {
app.Use(limiter.New(limiter.Config{Max: 300}))
}
app.Use(jwtware.New("user", jwtware.NormalJWTSigning))
app.Get("/", core.Home)
app.Get("/search", core.Search)
app.Get("/login", user.LoginGet)
app.Post("/login", user.LoginPost)
app.Get("/register", user.RegisterGet)
app.Post("/register", user.RegisterPost)
app.Get("/oauth/:type", user.AuthLoginGet)
app.Get("/verify/:key", user.VerifyGet)
app.Get("/recover", user.RecoverGet)
app.Post("/recover", user.RecoverPost)
app.Get("/reset/:key", user.ResetGet)
app.Post("/reset/:key", user.ResetPost)
app.Get("/explore", style.GetExplore)
app.Get("/style/:id/:name?", style.GetStylePage)
app.Get("/user/:name", user.Profile)
app.Get("~:name", user.Profile)
app.Get("/docs/:document", core.GetDocs)
app.Get("/modlog", core.GetModLog)
app.Get("/logout", jwtware.Protected, user.Logout)
app.Get("/account", jwtware.Protected, user.Account)
app.Post("/account", jwtware.Protected, user.EditAccount)
app.Get("/add", jwtware.Protected, style.CreateGet)
app.Post("/add", jwtware.Protected, style.CreatePost)
app.Get("/delete/:id", jwt.Protected, style.DeleteGet)
app.Post("/delete/:id", jwt.Protected, style.DeletePost)
app.Get("/import", jwtware.Protected, style.ImportGet)
app.Post("/import", jwtware.Protected, style.ImportPost)
app.Get("/edit/:id", jwtware.Protected, style.EditGet)
app.Post("/edit/:id", jwtware.Protected, style.EditPost)
app.Post("/style/:id/promote", jwtware.Protected, style.Promote)
app.Get("/oauth_settings/:id?", jwtware.Protected, oauthprovider.OAuthSettingsGet)
app.Post("/oauth_settings/:id?", jwtware.Protected, oauthprovider.OAuthSettingsPost)
app.Get("/user/ban/:id", jwtware.Protected, user.Ban)
app.Post("/user/ban/:id", jwtware.Protected, user.ConfirmBan)
app.Get("/dashboard", jwtware.Protected, core.Dashboard)
app.Get("/monitor", jwtware.Protected, core.Monitor)
v1 := app.Group("/api", api.ParseAPIJWT)
v1.Head("/style/:id.user.css", api.GetStyleEtag)
v1.Get("/style/:id.user.css", api.GetStyleSource)
v1.Get("/style/:id", api.GetStyleDetails)
v1.Get("/style/preview/:id", api.GetPreviewScreenshot)
v1.Get("/index/:format?", api.GetStyleIndex)
v1.Get("/search/:query", api.GetSearchResult)
v1.Get("/callback/:rcode", api.CallbackGet)
v1.Get("/user", api.ProtectedAPI, api.UserGet)
v1.Get("/user/:identifier", api.SpecificUserGet)
v1.Get("/styles", api.ProtectedAPI, api.StylesGet)
v1.Post("/style/new", api.ProtectedAPI, api.NewStyle)
v1.Post("/style/:id", api.ProtectedAPI, api.StylePost)
v1.Delete("/style/:id", api.ProtectedAPI, api.DeleteStyle)
v1.Get("/style", api.ProtectedAPI, api.StyleGet)
oauthV1 := app.Group("/api/oauth")
oauthV1.Get("/auth", jwtware.Protected, oauthprovider.AuthorizeGet)
oauthV1.Get("/style/link", jwtware.Protected, oauthprovider.OAuthStyleGet)
oauthV1.Post("/style/link", jwtware.Protected, oauthprovider.OAuthStylePost)
oauthV1.Get("/style/new", jwtware.Protected, oauthprovider.OAuthStyleNewPost)
oauthV1.Post("/style/new", jwtware.Protected, oauthprovider.OAuthStyleNewPost)
oauthV1.Post("/auth/:id/:token", jwtware.Protected, oauthprovider.AuthPost)
oauthV1.Post("/token", oauthprovider.TokenPost)
// Allows assets to be reloaded in dev mode.
// That means, they're not embedded into executable file.
if !config.Production {
app.Static("/", "/static")
}
app.Use("/", filesystem.New(filesystem.Config{
MaxAge: int(time.Hour) * 2,
Root: pkger.Dir("/static"),
}))
app.Use(core.NotFound)
log.Fatal(app.Listen(config.PORT))
}