-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroutes.go
More file actions
66 lines (54 loc) · 1.81 KB
/
routes.go
File metadata and controls
66 lines (54 loc) · 1.81 KB
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
package wireguardhttps
import (
"encoding/gob"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/secure"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/gorilla/csrf"
adapter "github.com/gwatts/gin-adapter"
"github.com/markbates/goth"
)
func Router(config *ServerConfig) *gin.Engine {
goth.UseProviders(config.AuthProviders...)
gob.Register(&UserProfile{})
router := gin.Default()
router.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
router.Use(secure.New(
secure.Config{
BrowserXssFilter: true,
IENoOpen: true,
FrameDeny: true,
ContentTypeNosniff: true,
SSLRedirect: false,
IsDevelopment: config.IsDebug,
AllowedHosts: []string{config.HTTPHost.String()},
}),
)
// JavaScript SPA frontend
router.Use(static.Serve("/", static.LocalFile(config.StaticAssetsDir, true)))
handlers := &WireguardHandlers{ServerConfig: config}
// API
api := router.Group("/api")
// Authentication
auth := api.Group("/auth")
auth.Use(ProviderWhitelistMiddleware)
auth.GET("/callback", handlers.OAuthCallbackHandler)
auth.GET("/authenticate", handlers.AuthenticateHandler)
auth.GET("/logout", handlers.LogoutHandler)
// Private routes
private := api.Group("/")
private.Use(AuthenticationRequiredMiddleware(config.SessionStore, config.SessionName))
if !config.IsDebug {
csrfMiddleware := csrf.Protect(config.CSRFKey)
private.Use(adapter.Wrap(csrfMiddleware))
}
// Devices
private.POST("/devices", handlers.NewDeviceHandler)
private.POST("/devices/:device_id", handlers.RekeyDeviceHandler)
private.DELETE("/devices/:device_id", handlers.DeleteDeviceHandler)
private.GET("/devices", handlers.ListUserDevicesHandler)
// User Profile
private.GET("/me", handlers.UserProfileInfoHandler)
return router
}