-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (131 loc) · 3.52 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"html/template"
"log/slog"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/ministryofjustice/opg-go-common/env"
"github.com/ministryofjustice/opg-go-common/telemetry"
"github.com/ministryofjustice/opg-sirius-lpa-dashboard/internal/server"
"github.com/ministryofjustice/opg-sirius-lpa-dashboard/internal/sirius"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
func main() {
ctx := context.Background()
logger := telemetry.NewLogger("opg-sirius-lpa-dashboard")
if err := run(ctx, logger); err != nil {
logger.Error("fatal startup error", slog.Any("err", err.Error()))
os.Exit(1)
}
}
func run(ctx context.Context, logger *slog.Logger) error {
port := env.Get("PORT", "8080")
webDir := env.Get("WEB_DIR", "web")
siriusURL := env.Get("SIRIUS_URL", "http://localhost:9001")
siriusPublicURL := env.Get("SIRIUS_PUBLIC_URL", "")
prefix := env.Get("PREFIX", "")
exportTraces := env.Get("TRACING_ENABLED", "0") == "1"
layouts, _ := template.
New("").
Funcs(map[string]interface{}{
"join": func(sep string, items []string) string {
return strings.Join(items, sep)
},
"contains": func(xs interface{}, needle interface{}) bool {
switch need := needle.(type) {
case string:
for _, x := range xs.([]string) {
if x == need {
return true
}
}
case int:
for _, x := range xs.([]int) {
if x == need {
return true
}
}
}
return false
},
"prefix": func(s string) string {
return prefix + s
},
"sirius": func(s string) string {
return siriusPublicURL + s
},
"formatDate": func(d interface{}) string {
switch t := d.(type) {
case time.Time:
return t.Format("02 Jan 2006")
case sirius.SiriusDate:
return t.Format("02 Jan 2006")
default:
panic("can't format date")
}
},
"isoDate": func(d time.Time) string {
if d.IsZero() {
return ""
}
return d.Format("2006-01-02")
},
"upper": func(s string) string {
return strings.ToUpper(s)
},
"statusColour": func(s string) string {
switch s {
case "Perfect":
return "green"
case "Imperfect":
return "red"
case "Pending":
return "blue"
default:
return "grey"
}
},
}).
ParseGlob(webDir + "/template/layout/*.gotmpl")
files, _ := filepath.Glob(webDir + "/template/*.gotmpl")
tmpls := map[string]*template.Template{}
for _, file := range files {
tmpls[filepath.Base(file)] = template.Must(template.Must(layouts.Clone()).ParseFiles(file))
}
shutdown, err := telemetry.StartTracerProvider(ctx, logger, exportTraces)
defer shutdown()
if err != nil {
return err
}
httpClient := http.DefaultClient
httpClient.Transport = otelhttp.NewTransport(httpClient.Transport)
client, err := sirius.NewClient(httpClient, siriusURL)
if err != nil {
return err
}
server := &http.Server{
Addr: ":" + port,
Handler: server.New(logger, client, tmpls, prefix, siriusURL, siriusPublicURL, webDir),
ReadHeaderTimeout: 10 * time.Second,
}
go func() {
if err := server.ListenAndServe(); err != nil {
logger.Error("listen and serve error", slog.Any("err", err.Error()))
os.Exit(1)
}
}()
logger.Info("Running at :" + port)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
sig := <-c
logger.Info("signal received: ", "sig", sig)
tc, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return server.Shutdown(tc)
}