-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
221 lines (191 loc) · 6.09 KB
/
http.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (C) 2023 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
package handlers
import (
"context"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/toitware/tpkg/build/proto/registry"
"github.com/toitware/tpkg/config"
"github.com/toitware/tpkg/controllers"
"github.com/toitware/tpkg/pkg/network"
doc "github.com/toitware/tpkg/pkg/toitdoc"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func proxyHTTPToGRPC(lc fx.Lifecycle, router *mux.Router, mux *runtime.ServeMux, address network.HostAddress, cfg *config.Config) {
ctx, cancel := context.WithCancel(context.Background())
var options []grpc.DialOption
options = append(options, grpc.WithInsecure())
registry.RegisterRegistryServiceHandlerFromEndpoint(ctx, mux, address.String(), options)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return nil
},
OnStop: func(ctx context.Context) error {
cancel()
return nil
},
})
}
type httpHandlers struct {
logger *zap.Logger
registry controllers.Registry
toitdoc controllers.Toitdoc
toitdocCfg config.Toitdocs
webFilePath string
}
func provideHTTPHandlers(logger *zap.Logger, cfg *config.Config, registry controllers.Registry, toitdoc controllers.Toitdoc) *httpHandlers {
return &httpHandlers{
logger: logger,
registry: registry,
toitdoc: toitdoc,
toitdocCfg: cfg.Toitdocs,
webFilePath: cfg.WebPath,
}
}
func bindHTTPHandlers(router *mux.Router, cfg *config.Config, logger *zap.Logger, h *httpHandlers, apiHandler *runtime.ServeMux) {
router.NotFoundHandler = network.HTTPHandle(h.web)
router.Handle("/{package:[^@]+}/docs/{path:.*}", network.HTTPHandle(h.toitdocs))
router.Handle("/{package:[^@]+}@{version:[^/]+}/docs/{path:.*}", network.HTTPHandle(h.toitdocs))
router.PathPrefix("/api/").Handler(http.StripPrefix("/api", apiHandler))
middlewares := []mux.MiddlewareFunc{
handlers.CORS(
handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "Authorization"}),
handlers.AllowedMethods([]string{"GET", "POST", "DELETE", "HEAD", "OPTIONS"}),
handlers.AllowedOriginValidator(func(host string) bool { return true }),
handlers.AllowCredentials(),
),
}
if cfg.HTTPS {
redirect := redirectHTTPToHTTPS(logger)
middlewares = append(middlewares, redirect)
router.NotFoundHandler = redirect(router.NotFoundHandler)
}
router.Use(middlewares...)
}
func redirectHTTPToHTTPS(log *zap.Logger) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// http -> https
if r.Header.Get("X-Forwarded-Proto") == "http" && r.URL.Path != "/health" {
url := *r.URL
url.Host = r.Host
url.Scheme = "https"
log.Info("http to https redirect", zap.String("url", url.String()))
http.Redirect(w, r, url.String(), http.StatusMovedPermanently)
return
}
h.ServeHTTP(w, r)
})
}
}
func parsePackage(identifier string) (pkg, version, path string) {
if parts := strings.SplitN(identifier, "@", 2); len(parts) == 2 {
pkg = parts[0]
version = parts[1]
} else {
pkg = identifier
}
return
}
func (h *httpHandlers) toitdocs(rw http.ResponseWriter, r *http.Request) error {
pkgName := mux.Vars(r)["package"]
pkg, err := h.registry.Package(r.Context(), pkgName)
if err != nil {
return err
}
version, noVersion := mux.Vars(r)["version"]
if !noVersion {
version = pkg.Latest().Version
}
desc, ok := pkg.Lookup[version]
if !ok {
return status.Errorf(codes.NotFound, "package '%s' did not have a version '%s'", pkgName, version)
}
path := mux.Vars(r)["path"]
h.logger.Debug("Serving toitdoc for package", zap.String("package", desc.URL), zap.String("version", desc.Version), zap.String("path", path))
doc, err := h.toitdoc.Load(r.Context(), desc)
if err != nil {
h.logger.Error("failed to load toitdoc", zap.Error(err), zap.String("package", desc.URL), zap.String("version", desc.Version), zap.String("path", path))
return status.Errorf(codes.Internal, "failed to load package '%s@%s", desc.URL, desc.Version)
}
srv := &toitdocFileServer{
doc: doc,
viewerPath: h.toitdocCfg.ViewerPath,
}
return srv.serve(rw, r, path)
}
func (h *httpHandlers) web(rw http.ResponseWriter, r *http.Request) error {
p := strings.Trim(r.URL.Path, "/")
if p == "" {
p = "index.html"
}
if strings.HasSuffix(p, "/") {
p += "index.html"
}
p = filepath.Join(h.webFilePath, p)
mainIndexPath := filepath.Join(h.webFilePath, "index.html")
err := serveFile(rw, r, p, mainIndexPath)
if status.Code(err) == codes.NotFound {
http.ServeFile(rw, r, mainIndexPath)
return nil
}
return err
}
type toitdocFileServer struct {
doc doc.Doc
viewerPath string
}
func (t *toitdocFileServer) serve(rw http.ResponseWriter, r *http.Request, name string) error {
if name == "" {
name = "index.html"
}
// Never serve directories.
if strings.HasSuffix(name, "/") {
name += "index.html"
}
if name == "index.html" {
http.ServeFile(rw, r, t.doc.ViewerIndexPath())
return nil
}
if name == "toitdocs/latest.json" {
http.ServeFile(rw, r, t.doc.JSONPath())
return nil
}
return serveFile(rw, r, filepath.Join(t.viewerPath, name), t.doc.ViewerIndexPath())
}
func serveFile(rw http.ResponseWriter, r *http.Request, path string, mainIndexPath string) error {
stat, err := os.Stat(path)
if !os.IsNotExist(err) {
if err == nil {
// If its not a directory serve the file.
if !stat.IsDir() {
http.ServeFile(rw, r, path)
return nil
}
} else {
return err
}
}
// If the path has no ext. try and serve the index file from the directory.
if filepath.Ext(path) == "" {
path := filepath.Join(path, "index.html")
if _, err := os.Stat(path); err == nil {
http.ServeFile(rw, r, path)
} else {
http.ServeFile(rw, r, mainIndexPath)
}
return nil
}
return status.Error(codes.NotFound, "404 page not found")
}