-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
72 lines (60 loc) · 1.67 KB
/
router.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
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"go.b8s.dev/nucleus/auth"
"go.b8s.dev/nucleus/config"
"go.b8s.dev/nucleus/data"
"go.b8s.dev/nucleus/files"
"go.b8s.dev/nucleus/nxc"
"go.b8s.dev/nucleus/web"
)
type NucleusRouter struct {
DBContext *data.Context
Config *config.Config
SessionStore sessions.Store
Auth *auth.AuthMiddleware
StorageBackend files.StorageBackend
router *gin.Engine
}
//go:embed templates/* static/*
var assetFS embed.FS
func (nr *NucleusRouter) Configure() {
nr.router = gin.Default()
// Use binary-embedded templates and static assets.
tmpls := template.Must(template.New("").ParseFS(assetFS, "templates/*"))
staticlessAssetFS, err := fs.Sub(assetFS, "static")
if err != nil {
panic(err)
}
nr.router.SetHTMLTemplate(tmpls)
nr.router.StaticFS("/static", http.FS(staticlessAssetFS))
nr.router.Use(sessions.Sessions("nucleussession", nr.SessionStore))
nextcloudRouter := &nxc.NextcloudRouter{
DBContext: nr.DBContext,
Config: nr.Config,
StorageBackend: nr.StorageBackend,
Auth: nr.Auth,
}
nextcloudRouter.Mount(nr.router.Group("/nextcloud"))
webRouter := &web.WebRouter{
DBContext: nr.DBContext,
Auth: nr.Auth,
}
webRouter.Mount(nr.router.Group("/web"))
nr.router.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"alive": true})
})
nr.router.GET("/", func(c *gin.Context) {
// If you hit the root path, you probably wanted the web app.
c.Redirect(302, "/web/")
})
}
func (nr *NucleusRouter) Listen(port int) {
nr.router.Run(fmt.Sprintf(":%d", port))
}