-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_prod.go
45 lines (36 loc) · 1.08 KB
/
server_prod.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
//go:build prod
// +build prod
package main
import (
"log/slog"
"net/http"
"os"
"time"
"golang.org/x/crypto/acme/autocert"
)
func (s *ApiServer) Run() {
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
serverLogger := slog.NewLogLogger(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}), slog.LevelDebug)
router := s.InitRoutes()
loggingMiddleware := LoggingMiddleware(logger)
loggedRouter := loggingMiddleware(router)
certManager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(s.domainName),
Cache: autocert.DirCache("/certs"),
}
httpsServer := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second,
Addr: ":https",
TLSConfig: certManager.TLSConfig(),
Handler: loggedRouter,
ErrorLog: serverLogger,
}
logger.Info("Starting HTTPS sever")
if err := httpsServer.ListenAndServeTLS("", ""); err != nil {
logger.Error("Failed to start HTTPS server", err)
os.Exit(1)
}
}