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
|
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
oapimiddleware "github.com/oapi-codegen/echo-middleware"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/admin"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/api"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/db"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/game"
)
func connectDB(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
return nil, err
}
if err := pool.Ping(ctx); err != nil {
return nil, err
}
return pool, nil
}
func main() {
var err error
config, err := NewConfigFromEnv()
if err != nil {
log.Fatalf("Error loading env %v", err)
}
openApiSpec, err := api.GetSwaggerWithPrefix("/api")
if err != nil {
log.Fatalf("Error loading OpenAPI spec\n: %s", err)
}
ctx := context.Background()
dbDSN := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", config.dbHost, config.dbPort, config.dbUser, config.dbPassword, config.dbName)
connPool, err := connectDB(ctx, dbDSN)
if err != nil {
log.Fatalf("Error connecting to db %v", err)
}
defer connPool.Close()
queries := db.New(connPool)
e := echo.New()
e.Renderer = admin.NewRenderer()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
gameHubs := game.NewGameHubs(queries)
err = gameHubs.RestoreFromDB(ctx)
if err != nil {
log.Fatalf("Error restoring game hubs from db %v", err)
}
defer gameHubs.Close()
sockGroup := e.Group("/sock")
sockHandler := gameHubs.SockHandler()
sockGroup.GET("/golf/:gameID/play", func(c echo.Context) error {
return sockHandler.HandleSockGolfPlay(c)
})
sockGroup.GET("/golf/:gameID/watch", func(c echo.Context) error {
return sockHandler.HandleSockGolfWatch(c)
})
apiGroup := e.Group("/api")
apiGroup.Use(oapimiddleware.OapiRequestValidator(openApiSpec))
apiHandler := api.NewHandler(queries, gameHubs)
api.RegisterHandlers(apiGroup, api.NewStrictHandler(apiHandler, nil))
adminHandler := admin.NewAdminHandler(queries, gameHubs)
adminGroup := e.Group("/admin")
adminHandler.RegisterHandlers(adminGroup)
// For local dev:
// This is never used in production because the reverse proxy sends /logout
// to the app server.
e.POST("/logout", func(c echo.Context) error {
return c.Redirect(http.StatusPermanentRedirect, "http://localhost:5173/logout")
})
gameHubs.Run()
if err := e.Start(":80"); err != http.ErrServerClosed {
log.Fatal(err)
}
}
|