-
Notifications
You must be signed in to change notification settings - Fork 9
/
schema.go
45 lines (36 loc) · 954 Bytes
/
schema.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
package luddite
import (
"fmt"
"net/http"
"path"
"strconv"
"strings"
"github.com/julienschmidt/httprouter"
)
type SchemaHandler struct {
fileServer http.Handler
}
func NewSchemaHandler(filePath string) *SchemaHandler {
return &SchemaHandler{http.FileServer(http.Dir(filePath))}
}
func (h *SchemaHandler) ServeHTTP(rw http.ResponseWriter, req0 *http.Request, params httprouter.Params) {
versionStr := params.ByName("version")
version, err := strconv.Atoi(versionStr)
if err != nil || version < 1 {
rw.WriteHeader(http.StatusNotFound)
return
}
filepath := params.ByName("filepath")
file := fmt.Sprintf("/v%d/%s", version, filepath)
req1, err := http.NewRequest("GET", file, nil)
if err != nil {
panic(err)
}
switch strings.ToLower(path.Ext(filepath)) {
case ".yaml", ".yml":
rw.Header().Set(HeaderContentType, ContentTypeOctetStream)
default:
rw.Header().Del(HeaderContentType)
}
h.fileServer.ServeHTTP(rw, req1)
}