/
main.go
124 lines (112 loc) · 3.27 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
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"embed"
"errors"
"fmt"
"net/http"
"os"
"github.com/gofiber/template/html/v2"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/encryptcookie"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/lukeshay/records/pkg/config"
"github.com/lukeshay/records/pkg/database"
"github.com/lukeshay/records/pkg/env"
"github.com/lukeshay/records/pkg/log"
"github.com/lukeshay/records/pkg/middleware"
"github.com/lukeshay/records/pkg/routers"
fibertrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gofiber/fiber.v2"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
)
//go:embed public/* templates/*
var assets embed.FS
func createTmplData(data map[string]any) map[string]any {
data["Version"] = config.Version
data["Environment"] = config.Environment
data["DatadogClientToken"] = config.DatadogClientToken
return data
}
func main() {
ddAgent, ok := os.LookupEnv("DD_AGENT_HOST")
if ok && config.Environment != "local" {
tracer.Start(
tracer.WithEnv(config.Environment),
tracer.WithService("deployer"),
tracer.WithServiceVersion(config.Version),
tracer.WithLogStartup(false),
tracer.WithDebugMode(false),
)
profiler.Start(
profiler.WithEnv(config.Environment),
profiler.WithService("deployer"),
profiler.WithVersion(config.Version),
profiler.WithProfileTypes(
profiler.CPUProfile,
profiler.HeapProfile,
),
profiler.WithLogStartup(false),
)
statsd.New(fmt.Sprintf("http://%s:8125", ddAgent))
}
defer tracer.Stop()
log.InitLogger()
database.InitDb()
engine := html.NewFileSystem(http.FS(assets), ".html")
engine.AddFunc("dict", func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
})
app := fiber.New(fiber.Config{
Views: engine,
})
app.Use(recover.New())
app.Use(fibertrace.Middleware(fibertrace.WithServiceName("records")))
app.Use(logger.New())
app.Use(encryptcookie.New(encryptcookie.Config{
Key: config.CookieKey,
}))
app.Use("/public", filesystem.New(filesystem.Config{
Root: http.FS(assets),
PathPrefix: "public",
Browse: config.Environment == "local",
}))
app.Get("/ping", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"message": "pong",
})
})
app.Get("/metadata", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"version": config.Version,
"environment": config.Environment,
})
})
auth := app.Group("/auth")
{
auth.Use(middleware.UnauthRequired())
auth.Get("/signin", routers.GetAuthSignIn)
auth.Post("/signin", routers.PostAuthSignIn)
auth.Get("/signup", routers.GetAuthSignUp)
auth.Post("/signup", routers.PostAuthSignUp)
}
records := app.Group("/records")
{
records.Use(middleware.AuthRequired())
records.Get("/", routers.GetRecords)
}
app.Listen(fmt.Sprintf(":%s", env.DefaultEnv("PORT", "8080")))
}