aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/main.go
blob: 379fe8d41aec588a9129c6bb2941de15526679ce (plain)
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
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/jackc/pgx/v5"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	oapimiddleware "github.com/oapi-codegen/echo-middleware"

	"github.com/nsfisis/iosdc-2024-albatross/backend/api"
	"github.com/nsfisis/iosdc-2024-albatross/backend/db"
	"github.com/nsfisis/iosdc-2024-albatross/backend/game"
)

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()

	conn, err := pgx.Connect(ctx, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", config.dbHost, config.dbPort, config.dbUser, config.dbPassword, config.dbName))
	if err != nil {
		log.Fatalf("Error connecting to db %v", err)
	}
	defer conn.Close(ctx)

	queries := db.New(conn)

	e := echo.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	apiGroup := e.Group("/api")
	apiGroup.Use(oapimiddleware.OapiRequestValidator(openApiSpec))
	apiHandler := api.NewHandler(queries)
	api.RegisterHandlers(apiGroup, api.NewStrictHandler(apiHandler, []api.StrictMiddlewareFunc{
		api.NewJWTMiddleware(),
	}))

	gameHubs := game.NewGameHubs()
	err = gameHubs.RestoreFromDB(ctx, queries)
	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/watch", func(c echo.Context) error {
		return sockHandler.HandleSockGolfWatch(c)
	})
	sockGroup.GET("/golf/:gameId/play", func(c echo.Context) error {
		return sockHandler.HandleSockGolfPlay(c)
	})

	if err := e.Start(":80"); err != http.ErrServerClosed {
		log.Fatal(err)
	}
}