-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (99 loc) · 2.64 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
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
// main.go
package main
import (
"html/template"
"log"
"os"
"strings"
"whirled2/api"
"whirled2/game/server"
"whirled2/utils"
"github.com/joho/godotenv"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)
func main() {
log.SetFlags(log.Llongfile)
args := os.Args[1:]
// Check if "--http" is present in the arguments
debug := true
for _, arg := range args {
if strings.HasPrefix(arg, "--http") {
debug = false
break
}
}
if debug {
log.Println("Debug mode enabled")
godotenv.Load(".env.local")
localIPs, err := utils.GetLocalIP()
if err == nil {
os.Args = append(os.Args, "--http=0.0.0.0:42069", "--origins=http://127.0.0.1:6969,http://" + localIPs[0] + ":6969")
} else {
os.Args = append(os.Args, "--http=0.0.0.0:42069", "--origins=http://127.0.0.1:6969")
}
} else {
godotenv.Load()
}
utils.Start()
app := pocketbase.NewWithConfig(pocketbase.Config{
HideStartBanner: true,
// DefaultDebug: false
})
routes := []func(*core.ServeEvent, *pocketbase.PocketBase){
api.AddBaseRoutes,
api.AddAuthRoutes,
api.AddProfileRoutes,
api.AddRoomRoutes,
api.AddStuffRoutes,
server.AddAuthRoutes,
// Add more routes here
}
customEventHooks := []func(*pocketbase.PocketBase){
api.AddAuthEventHooks,
api.AddProfileEventHooks,
api.AddRoomEventHooks,
api.AddStuffEventHooks,
// Add more event hooks here
api.AddBaseEventHooks, // keep last
}
for _, AddEventHooks := range customEventHooks {
AddEventHooks(app)
}
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
utils.Bootstrap(app)
// serves static files from the provided public dir (if exists)
e.Router.GET("/static/*", func(c echo.Context) error {
// Disable client-side caching for development
c.Response().Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
return apis.StaticDirectoryHandler(os.DirFS("./web/static"), false)(c)
})
e.Router.Pre(api.AuthMiddleware(app))
e.Router.Use(
utils.IdleMiddleware,
api.FormMiddleware,
api.BaseMiddleware,
)
for _, AddRoutes := range routes {
AddRoutes(e, app)
}
e.Router.GET("/api/hello", func(c echo.Context) error {
return c.String(200, "Hello whirled!")
})
e.Router.GET("/test", func(c echo.Context) error {
tmpl := template.Must(template.ParseFiles("web/templates/pages/test.gohtml"))
if err := tmpl.Execute(c.Response().Writer, nil); err != nil {
return err
}
return nil
})
// start gecgos.io game server
server.Start(42069, app, debug)
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}