forked from nytimes/gcs-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
33 lines (29 loc) · 904 Bytes
/
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
package main
import (
"net/http"
"strings"
"cloud.google.com/go/storage"
"github.com/NYTimes/gcs-helper/v3/handlers"
)
func getHandler(c handlers.Config, client *storage.Client, hc *http.Client) http.HandlerFunc {
proxyHandler := handlers.Proxy(c, hc)
mapHandler := handlers.Map(c, client)
return func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, c.Proxy.Endpoint):
r.URL.Path = strings.Replace(r.URL.Path, c.Proxy.Endpoint, "", 1)
if !strings.HasPrefix(r.URL.Path, "/") {
r.URL.Path = "/" + r.URL.Path
}
proxyHandler.ServeHTTP(w, r)
case strings.HasPrefix(r.URL.Path, c.Map.Endpoint):
r.URL.Path = strings.Replace(r.URL.Path, c.Map.Endpoint, "", 1)
mapHandler.ServeHTTP(w, r)
case r.URL.Path == "/":
// healthcheck
w.WriteHeader(http.StatusOK)
default:
http.Error(w, "not found", http.StatusNotFound)
}
}
}