/
server.go
77 lines (67 loc) · 1.86 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
// Package http provides the server and API implementation
// for the webclient. The webclient's static files are also served.
package http
import (
"context"
"log"
"net/http"
"time"
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/store"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/julienschmidt/httprouter"
)
// Server provides the HTTP server for the API
// and the assets.
type Server struct {
*http.Server
cfg *config.Config
routesStore *store.RoutesStore
neighborsStore *store.NeighborsStore
pool *pgxpool.Pool
}
// NewServer creates a new server
func NewServer(
cfg *config.Config,
pool *pgxpool.Pool,
routesStore *store.RoutesStore,
neighborsStore *store.NeighborsStore,
) *Server {
return &Server{
cfg: cfg,
routesStore: routesStore,
neighborsStore: neighborsStore,
pool: pool,
}
}
// Start starts a HTTP server and begins to listen
// on the configured port.
func (s *Server) Start(ctx context.Context) {
router := httprouter.New()
// Register routes
if err := s.webRegisterAssets(ctx, router); err != nil {
log.Fatal(err)
}
if err := s.apiRegisterEndpoints(router); err != nil {
log.Fatal(err)
}
httpTimeout := time.Duration(s.cfg.Server.HTTPTimeout) * time.Second
log.Println("Web server HTTP timeout set to:", httpTimeout)
log.Println("Listening on:", s.cfg.Server.Listen)
if s.cfg.Server.EnablePrefixLookup {
log.Println("Prefix Lookup (Search): enabled")
log.Println("Prefix Lookup Community Filter Cutoff:",
s.cfg.Server.PrefixLookupCommunityFilterCutoff)
} else {
log.Println("Prefix Lookup (Search): disabled")
}
s.Server = &http.Server{
Addr: s.cfg.Server.Listen,
Handler: router,
ReadTimeout: httpTimeout,
WriteTimeout: httpTimeout,
IdleTimeout: httpTimeout,
}
// Start http server
log.Fatal(s.ListenAndServe())
}