-
Notifications
You must be signed in to change notification settings - Fork 402
/
server.go
151 lines (127 loc) · 5.26 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
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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleserver
import (
"context"
"errors"
"net"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"storj.io/common/errs2"
"storj.io/storj/storagenode/console"
"storj.io/storj/storagenode/console/consoleapi"
"storj.io/storj/storagenode/heldamount"
"storj.io/storj/storagenode/notifications"
)
var (
mon = monkit.Package()
// Error is storagenode console web error type.
Error = errs.Class("storagenode console web error")
)
// Config contains configuration for storagenode console web server.
type Config struct {
Address string `help:"server address of the api gateway and frontend app" default:"127.0.0.1:14002"`
StaticDir string `help:"path to static resources" default:""`
}
// Server represents storagenode console web server.
//
// architecture: Endpoint
type Server struct {
log *zap.Logger
service *console.Service
notifications *notifications.Service
heldAmount *heldamount.Service
listener net.Listener
server http.Server
}
// NewServer creates new instance of storagenode console web server.
func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifications.Service, service *console.Service, heldAmount *heldamount.Service, listener net.Listener) *Server {
server := Server{
log: logger,
service: service,
listener: listener,
notifications: notifications,
heldAmount: heldAmount,
}
router := mux.NewRouter()
// handle api endpoints
storageNodeController := consoleapi.NewStorageNode(server.log, server.service)
storageNodeRouter := router.PathPrefix("/api/sno").Subrouter()
storageNodeRouter.StrictSlash(true)
storageNodeRouter.HandleFunc("/", storageNodeController.StorageNode).Methods(http.MethodGet)
storageNodeRouter.HandleFunc("/satellites", storageNodeController.Satellites).Methods(http.MethodGet)
storageNodeRouter.HandleFunc("/satellite/{id}", storageNodeController.Satellite).Methods(http.MethodGet)
storageNodeRouter.HandleFunc("/estimated-payout", storageNodeController.EstimatedPayout).Methods(http.MethodGet)
notificationController := consoleapi.NewNotifications(server.log, server.notifications)
notificationRouter := router.PathPrefix("/api/notifications").Subrouter()
notificationRouter.StrictSlash(true)
notificationRouter.HandleFunc("/list", notificationController.ListNotifications).Methods(http.MethodGet)
notificationRouter.HandleFunc("/{id}/read", notificationController.ReadNotification).Methods(http.MethodPost)
notificationRouter.HandleFunc("/readall", notificationController.ReadAllNotifications).Methods(http.MethodPost)
heldAmountController := consoleapi.NewHeldAmount(server.log, server.heldAmount)
heldAmountRouter := router.PathPrefix("/api/heldamount").Subrouter()
heldAmountRouter.StrictSlash(true)
heldAmountRouter.HandleFunc("/paystubs/{period}", heldAmountController.PayStubMonthly).Methods(http.MethodGet)
heldAmountRouter.HandleFunc("/paystubs/{start}/{end}", heldAmountController.PayStubPeriod).Methods(http.MethodGet)
heldAmountRouter.HandleFunc("/held-history", heldAmountController.HeldHistory).Methods(http.MethodGet)
heldAmountRouter.HandleFunc("/periods", heldAmountController.HeldAmountPeriods).Methods(http.MethodGet)
heldAmountRouter.HandleFunc("/payout-history/{period}", heldAmountController.PayoutHistory).Methods(http.MethodGet)
if assets != nil {
fs := http.FileServer(assets)
router.PathPrefix("/static/").Handler(server.cacheMiddleware(http.StripPrefix("/static", fs)))
router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := r.Clone(r.Context())
req.URL.Path = "/dist/"
fs.ServeHTTP(w, req)
}))
}
server.server = http.Server{
Handler: router,
}
return &server
}
// Run starts the server that host webapp and api endpoints.
func (server *Server) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
ctx, cancel := context.WithCancel(ctx)
var group errgroup.Group
group.Go(func() error {
<-ctx.Done()
return server.server.Shutdown(context.Background())
})
group.Go(func() error {
defer cancel()
err := server.server.Serve(server.listener)
if errs2.IsCanceled(err) || errors.Is(err, http.ErrServerClosed) {
err = nil
}
return err
})
return group.Wait()
}
// Close closes server and underlying listener.
func (server *Server) Close() error {
return server.server.Close()
}
// cacheMiddleware is a middleware for caching static files.
func (server *Server) cacheMiddleware(fn http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "mime" package, which http.FileServer uses, depends on Operating System
// configuration for mime-types. When a system has hardcoded mime-types to
// something else, they might serve ".js" as a "plain/text".
//
// Override any of that default behavior to ensure we get the correct types for
// common files.
if contentType, ok := CommonContentType(filepath.Ext(r.URL.Path)); ok {
w.Header().Set("Content-Type", contentType)
}
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("X-Content-Type-Options", "nosniff")
fn.ServeHTTP(w, r)
})
}