-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 2.18 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
package main
import (
"bytes"
"context"
"log"
"time"
"github.com/a-h/templ"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/xanzy/go-gitlab"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)
func RequestOptionFuncWithParam(params map[string]string) gitlab.RequestOptionFunc {
return func(req *retryablehttp.Request) error {
for k, v := range params {
q := req.URL.Query()
q.Add(k, v)
req.URL.RawQuery = q.Encode()
}
return nil
}
}
func Out(c *gin.Context, in string, b *bytes.Buffer) {
if err := Output(in).Render(context.Background(), b); err != nil {
c.String(200, err.Error())
}
c.String(200, b.String())
}
func OutToken(c *gin.Context, in string, b *bytes.Buffer) {
if err := OutputToken(in).Render(context.Background(), b); err != nil {
c.String(200, err.Error())
}
c.String(200, b.String())
}
func main() {
db, err := InitDB()
if err != nil {
log.Fatalln("DB init error", err)
}
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("session", store))
year, _, _ := time.Now().Date()
// index
r.GET("/", gin.WrapH(templ.Handler(Site(year))))
// htmx html returns
r.GET("/home", gin.WrapH(templ.Handler(Home())))
r.GET("/serviceaccount", AuthMiddleware(), gin.WrapH(templ.Handler(CreateServiceUser())))
r.GET("/token", AuthMiddleware(), gin.WrapH(templ.Handler(CreatePersonalAccessToken(Scopes))))
r.GET("/login", gin.WrapH(templ.Handler(Auth("login"))))
r.GET("/logout", AuthMiddleware(), gin.WrapH(templ.Handler(LogoutComponent())))
r.GET("/register", gin.WrapH(templ.Handler(Auth("register"))))
r.GET("/sessionstore", AuthMiddleware(), gin.WrapH(templ.Handler(SessionComponent())))
r.GET("/nav/login", gin.WrapH(templ.Handler(NavbarLoggedIn())))
r.GET("/nav/logout", gin.WrapH(templ.Handler(NavbarLoggedOut())))
// login + register + session
r.POST("/login", Login(db))
r.POST("/logout", Logout())
r.POST("/register", Register(db))
// buisness logic
r.POST("/serviceaccount", Serviceaccount)
r.POST("/token", Token)
r.POST("/sessionstore", SessionStore)
if err := r.Run(":8080"); err != nil {
log.Fatalln("Gin Run Error: ", err)
}
}