This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
115 lines (96 loc) · 2.75 KB
/
server.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
package main
//go:generate go run github.com/99designs/gqlgen generate
import (
"context"
"fmt"
"log"
"log/slog"
"net"
"net/http"
"os"
"time"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/golang-migrate/migrate/v4"
migratepgx5 "github.com/golang-migrate/migrate/v4/database/pgx/v5"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/jessedearing/service-catalog/graph"
"github.com/jessedearing/service-catalog/internal/health"
"github.com/jessedearing/service-catalog/internal/metrics"
"github.com/jessedearing/service-catalog/internal/storage"
"github.com/jessedearing/service-catalog/internal/vars"
pgxUUID "github.com/vgarvardt/pgx-google-uuid/v5"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
const defaultPort = "8080"
func main() {
ctx := context.Background()
pgurl := os.Getenv("PGURL")
dbconfig, err := pgxpool.ParseConfig(pgurl)
if err != nil {
panic(err)
}
dbconfig.AfterConnect = func(ctx context.Context, c *pgx.Conn) error {
pgxUUID.Register(c.TypeMap())
return nil
}
var dbpool *pgxpool.Pool
var initFailures int
for initFailures = 0; initFailures < 5; initFailures++ {
time.Sleep(1 * time.Second)
dbpool, err = pgxpool.NewWithConfig(ctx, dbconfig)
if err != nil {
slog.ErrorContext(ctx, err.Error())
continue
}
if err = dbpool.Ping(ctx); err != nil {
slog.ErrorContext(ctx, err.Error())
continue
}
}
if initFailures >= 5 && err != nil {
panic(err)
}
conn := stdlib.OpenDBFromPool(dbpool)
drv, err := migratepgx5.WithInstance(conn, &migratepgx5.Config{})
if err != nil {
panic(err)
}
m, err := migrate.NewWithDatabaseInstance("file://./migrations", "service_catalog", drv)
if err != nil {
panic(err)
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
slog.ErrorContext(ctx, err.Error())
} else {
slog.InfoContext(ctx, "No new db migrations to run")
}
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
mux := http.NewServeMux()
hc, err := health.NewHealthHandler()
if err != nil {
panic(err)
}
mux.Handle("/", playground.Handler("GraphQL playground", "/query"))
mux.Handle("/query", srv)
mux.Handle("/healthz", hc)
mux.Handle("/metricsz", metrics.NewMetricsHandler())
db := storage.New(dbpool)
httpserver := http.Server{
BaseContext: func(l net.Listener) context.Context {
dbctx := context.WithValue(ctx, vars.DBContextKey, db)
return dbctx
},
Addr: fmt.Sprintf("0.0.0.0:%s", port),
Handler: mux,
}
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(httpserver.ListenAndServe())
}