-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (69 loc) · 1.73 KB
/
main.go
File metadata and controls
86 lines (69 loc) · 1.73 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"log"
"makedotcsh/database"
"makedotcsh/routes"
"os"
cshauth "github.com/computersciencehouse/csh-auth/v2"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/logger"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func errorHandler(c *gin.Context) {
c.Next()
err := c.Errors.Last()
if err != nil {
c.JSON(500, gin.H{
"error": "Internal server error",
})
}
}
func serveIndex(c *gin.Context) {
c.File("./web/dist/index.html")
}
func main() {
godotenv.Load()
router := gin.New()
host := os.Getenv("MAKE_HOST")
fmt.Println(os.Getenv("MAKE_OIDC_ID"))
// init db
database.Init()
// init auth
auth, err := cshauth.Init(
os.Getenv("MAKE_OIDC_ID"),
os.Getenv("MAKE_OIDC_SECRET"),
host,
host+"/auth/login",
host+"/auth/callback",
[]string{"profile", "email", "groups"},
)
if err != nil {
log.Panicf("Error initializing CSH auth %v", err)
}
router.Use(gzip.Gzip(gzip.DefaultCompression))
router.Use(cors.Default())
router.Use(logger.SetLogger())
router.Use(errorHandler)
// auth
router.GET("/auth/login", auth.HandleLogin) // This endpoint should match the path for loginURL
router.GET("/auth/callback", auth.HandleCallback) // This endpoint should match the path for callbackURL
router.GET("/auth/logout", auth.HandleLogout)
// api
routes.SetRoutes(router, auth)
// frontend
frontend := router.Group("/")
frontend.Use(auth.CookieMiddleware())
if os.Getenv("DEV") == "true" {
router.NoRoute(createViteProxy())
} else {
frontend.Static("/assets", "./web/dist/assets")
frontend.GET("/", serveIndex)
frontend.GET("/:path", serveIndex)
frontend.GET("/:path/*rest", serveIndex)
}
log.Println("running")
router.Run()
}