-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (57 loc) · 1.77 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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-logr/zapr"
"github.com/rmb938/franz-schema-registry/pkg/database/migrations"
"github.com/rmb938/franz-schema-registry/pkg/http/routers/schemas"
"github.com/rmb938/franz-schema-registry/pkg/http/routers/subjects"
"go.uber.org/zap"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
zc := zap.NewDevelopmentConfig()
// zc := zap.NewProductionConfig()
z, err := zc.Build()
if err != nil {
panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
}
log := zapr.NewLogger(z)
db, err := gorm.Open(postgres.Open(
"host=localhost user=postgres password=postgres dbname=franz-schema-registry port=5432 sslmode=disable",
), &gorm.Config{
DisableNestedTransaction: true,
})
if err != nil {
log.Error(err, "error opening database connection")
os.Exit(1)
}
log.Info("Running database migrations")
if err = migrations.RunMigrations(db); err != nil {
log.Error(err, "error running database migrations")
os.Exit(1)
}
log.Info("Done running database migrations")
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger) // TODO: convert to logr
r.Use(middleware.Recoverer)
// Set a timeout value on the request context (ctx), that will signal
// through ctx.Done() that the request has timed out and further
// processing should be stopped.
r.Use(middleware.Timeout(60 * time.Second))
r.Use(middleware.Heartbeat("/ping"))
r.Use(middleware.AllowContentType("application/json"))
r.Mount("/schemas", schemas.NewRouter())
r.Mount("/subjects", subjects.NewRouter(db))
if err := http.ListenAndServe(":9091", r); err != nil {
log.Error(err, "error running api server")
os.Exit(1)
}
}