-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
146 lines (125 loc) · 3.8 KB
/
transport.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
package main
import (
"archive/zip"
"context"
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
kittransport "github.com/go-kit/kit/transport"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/go-kit/log"
"github.com/gorilla/mux"
"tailscale.com/client/tailscale"
)
// makeHandler returns a handler for the tracking service.
func makeHandlerForApplication(app *Application, ts Service, lc *tailscale.LocalClient, logger log.Logger) http.Handler {
r := mux.NewRouter()
opts := []kithttp.ServerOption{
kithttp.ServerErrorHandler(kittransport.NewLogErrorHandler(logger)),
kithttp.ServerErrorEncoder(encodeError),
}
whoHandler := kithttp.NewServer(
makeWhoEndpoint(ts, lc),
decodeWhoRequest,
makeJSONResponseEncoder(logger),
opts...,
)
databasesHandler := kithttp.NewServer(
makeDatabasesEndpoint(ts),
decodeEmptyRequest,
makeJSONResponseEncoder(logger),
opts...,
)
applicationsHandler := kithttp.NewServer(
makeApplicationsEndpoint(ts, lc),
decodeEmptyRequest,
makeJSONResponseEncoder(logger),
opts...,
)
setMetaHandler := kithttp.NewServer(
makeSetMetaEndpoint(ts),
decodeSetMetaRequest,
makeJSONResponseEncoder(logger),
opts...,
)
uploadGetHandler := kithttp.NewServer(
makeUploadGetEndpoint(ts),
decodeUploadGetRequest,
makeJSONResponseEncoder(logger),
opts...,
)
downloadHandler := kithttp.NewServer(
makeDownloadEndpoint(ts),
decodeDownloadRequest,
makeStreamCARResponseEncoder(logger),
opts...,
)
uploadDataHandler := kithttp.NewServer(
makeUploadDataEndpoint(ts),
decodeUploadDataRequest,
makeJSONResponseEncoder(logger),
opts...,
)
webSocketHandler := newWebsocketHandler(ts, logger)
// fireproof AWS/s3 fake
r.Handle("/api/download/data/{name}/{car}", downloadHandler).Methods("GET")
r.Handle("/api/upload/data/{name}/{car}", uploadDataHandler).Methods("PUT")
r.Handle("/api/upload", uploadGetHandler).Methods("GET")
r.Handle("/api/upload", setMetaHandler).Methods("PUT")
r.Handle("/api/websocket", webSocketHandler)
// fireplace api
r.Handle("/api/who", whoHandler).Methods("GET")
r.Handle("/api/database", databasesHandler).Methods("GET")
r.Handle("/api/application", applicationsHandler).Methods("GET")
// static content, with SPA redirecting
var mySPAHandler SPAFileSystem
if filepath.Ext(app.LocalPath) == ".zip" {
zr, err := zip.OpenReader(app.LocalPath)
if err != nil {
logger.Log("msg", "unable to open zip reader", "path", app.LocalPath, "err", err)
return nil
}
logger.Log("msg", "app started with zip", "path", app.LocalPath)
mySPAHandler = NewSPAFileSystem(zr, "index.html")
} else {
logger.Log("msg", "app started with dir", "path", app.LocalPath)
mySPAHandler = NewSPAFileSystem(os.DirFS(app.LocalPath), "index.html")
}
r.PathPrefix("/").Handler(http.FileServerFS(mySPAHandler))
return r
}
// encode errors from business-logic
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch err {
default:
w.WriteHeader(http.StatusInternalServerError)
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
}
func encodeJSONResponse(ctx context.Context, w http.ResponseWriter, response any) error {
var err error
w.Header().Set("Content-Type", "application/json; charset=utf-8")
switch t := response.(type) {
case error:
w.WriteHeader(http.StatusInternalServerError)
err = json.NewEncoder(w).Encode(map[string]interface{}{
"error": t.Error(),
})
default:
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(t)
}
return err
}
func streamCAR(ctx context.Context, w http.ResponseWriter, response io.ReadCloser) error {
defer response.Close()
w.Header().Set("Content-Type", "application/car")
w.WriteHeader(http.StatusOK)
_, err := io.Copy(w, response)
return err
}