-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
141 lines (107 loc) · 3.45 KB
/
Copy pathmain.go
File metadata and controls
141 lines (107 loc) · 3.45 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
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
131
132
133
134
135
136
137
138
139
140
141
package main // import "github.com/tapsilat/iban.im
import (
"context"
"encoding/json"
"io/fs"
"log"
"net/http"
graphql "github.com/graph-gophers/graphql-go"
"github.com/tapsilat/iban.im/config"
"github.com/tapsilat/iban.im/handler"
_ "github.com/tapsilat/iban.im/model"
"github.com/tapsilat/iban.im/resolvers"
"github.com/tapsilat/iban.im/schema"
"github.com/tapsilat/iban.im/static"
jwt "github.com/appleboy/gin-jwt/v2"
"fmt"
"github.com/gin-gonic/gin"
)
const identityKey = "UserID"
func main() {
cfg, err := config.GetConfig()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Initialize database and run AutoMigrate at startup
config.InitDB(cfg)
router := gin.Default()
router.Use(func(c *gin.Context) {
c.Next()
})
router.Use(gin.Logger())
router.LoadHTMLGlob("templates/*.tmpl.html")
// Serve embedded static files from the Vue.js frontend
staticFS, err := static.GetFS()
if err != nil {
log.Fatalf("Failed to get embedded static files: %v", err)
}
// Cache index.html in memory for efficient serving
indexHTML, err := fs.ReadFile(staticFS, "index.html")
if err != nil {
log.Fatalf("Failed to read index.html from embedded files: %v", err)
}
// Serve assets directory from embedded filesystem
assetsFS, err := fs.Sub(staticFS, "assets")
if err != nil {
log.Fatalf("Failed to get assets subdirectory: %v", err)
}
router.StaticFS("/assets", http.FS(assetsFS))
if sqlDB, err := config.DB.DB(); err == nil {
defer sqlDB.Close()
}
context.Background()
authMiddleware, err := handler.AuthMiddleware()
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
router.POST("/api/login", func(c *gin.Context) {
authMiddleware.LoginHandler(c)
})
auth := router.Group("/auth")
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
router.GET("/graph", func(c *gin.Context) {
c.HTML(http.StatusOK, "graph.tmpl.html", nil)
})
authMW := authMiddleware.MiddlewareFunc()
router.POST("/graph", func(c *gin.Context) {
ctx := c.Request.Context()
if _, ok := c.Request.Header["Authorization"]; ok {
authMW(c)
claims := jwt.ExtractClaims(c)
currentID, ok := claims[identityKey].(float64)
if !ok {
currentID = 0
}
ctx = context.WithValue(ctx, handler.ContextKey("UserID"), int(currentID))
}
var params struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
if err := json.NewDecoder(c.Request.Body).Decode(¶ms); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
schema := graphql.MustParseSchema(*schema.NewSchema(), &resolvers.Resolvers{}, opts...)
response := schema.Exec(ctx, params.Query, params.OperationName, params.Variables)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(200, response)
})
// Route for serving IBAN addresses at /:userHandle/:ibanHandle
router.GET("/:userHandle/:ibanHandle", handler.RenderIbanPage)
// Serve the Vue.js SPA for all other routes
// This enables client-side routing for the frontend
router.NoRoute(func(c *gin.Context) {
// Serve cached index.html
c.Data(http.StatusOK, "text/html; charset=utf-8", indexHTML)
})
err = http.ListenAndServe(fmt.Sprintf(":%s", cfg.App.Port), router)
if err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}