-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathserver.go
More file actions
119 lines (98 loc) · 3.21 KB
/
Copy pathserver.go
File metadata and controls
119 lines (98 loc) · 3.21 KB
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
//go:build !skiff
package cuhttp
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/snadrus/must"
"golang.org/x/xerrors"
"github.com/filecoin-project/curio/cuhttp/servicedeps"
"github.com/filecoin-project/curio/deps"
"github.com/filecoin-project/curio/lib/piecestore"
mhttp "github.com/filecoin-project/curio/market/http"
"github.com/filecoin-project/curio/market/libp2p"
"github.com/filecoin-project/curio/pdp"
storage_market "github.com/filecoin-project/curio/tasks/storage-market"
)
// RouterMap is the map that allows the library user to pass in their own routes
type RouterMap map[string]http.HandlerFunc
// libp2pConnMiddleware intercepts WebSocket upgrade requests to "/" and rewrites the path to "/libp2p"
func libp2pConnMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" || r.URL.Path == "" {
if isWebSocketUpgrade(r) {
r.URL.Path = "/libp2p"
r.RequestURI = "/libp2p"
}
}
next.ServeHTTP(w, r)
})
}
func isWebSocketUpgrade(r *http.Request) bool {
if r.Method != http.MethodGet {
return false
}
if strings.ToLower(r.Header.Get("Upgrade")) != "websocket" {
return false
}
if strings.ToLower(r.Header.Get("Connection")) != "upgrade" {
return false
}
return true
}
type ServiceDeps struct {
servicedeps.Deps
DealMarket *storage_market.CurioStorageDealMarket
}
// StartHTTPServer starts the public-facing server for market calls.
func StartHTTPServer(ctx context.Context, d *deps.Deps, sd *ServiceDeps) error {
cfg := d.Cfg.HTTP
chiRouter := NewRouter(RouterConfig{CSP: cfg.CSP})
compressionMw, err := Compression(&cfg.CompressionLevels)
if err != nil {
log.Fatalf("Failed to initialize compression middleware: %s", err)
}
chiRouter.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = fmt.Fprintf(w, "Requested resource not found")
})
chiRouter.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "Hello, World!\n -Curio\n")
})
MountStandardRoutes(chiRouter)
chiRouter, err = attachRouters(ctx, chiRouter, d, sd)
if err != nil {
return xerrors.Errorf("failed to attach routers: %w", err)
}
handler := libp2pConnMiddleware(LoggingMiddleware(compressionMw(chiRouter)))
return StartServer(ctx, &cfg, d.DB, handler, "HTTPS server")
}
func attachRouters(ctx context.Context, r *chi.Mux, d *deps.Deps, sd *ServiceDeps) (*chi.Mux, error) {
ipp, err := MountCommonPublicRoutes(ctx, r, d)
if err != nil {
return nil, err
}
rd := libp2p.NewRedirector(d.DB)
libp2p.Router(r, rd)
if sd.EthSender != nil {
if err := pdp.MountRoutes(ctx, r, pdp.MountDeps{
DB: d.DB,
PieceIO: piecestore.New(d.Stor, d.LocalStore, d.Si),
EthClient: must.One(d.EthClient.Get()),
Chain: d.Chain,
EthSender: sd.EthSender,
AlertTask: sd.AlertTask,
}, ipp); err != nil {
return nil, err
}
}
dh, err := mhttp.NewMarketHandler(d.DB, d.Cfg, sd.DealMarket, must.One(d.EthClient.Get()), d.Chain, sd.EthSender, d.LocalStore)
if err != nil {
return nil, xerrors.Errorf("failed to create new market handler: %w", err)
}
mhttp.Router(r, dh)
return r, nil
}