-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
44 lines (35 loc) · 1.05 KB
/
main.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
package main
import (
"github.com/rs/cors"
"github.com/spaceapi/validator/v1"
"github.com/spaceapi/validator/v2"
"goji.io"
"goji.io/pat"
"log"
"net/http"
)
func main() {
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
root := goji.NewMux()
root.Use(c.Handler)
root.HandleFunc(pat.Get("/"), versionRedirect)
root.HandleFunc(pat.Get("/openapi.json"), openAPI)
root.HandleFunc(pat.Get("/v1"), func(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, "/v1/", 302)
})
root.HandleFunc(pat.Get("/v2"), func(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, "/v2/", 302)
})
root.Handle(pat.New("/v1/*"), v1.GetSubMux())
root.Handle(pat.New("/v2/*"), v2.GetSubMux())
log.Println("starting validator...")
log.Fatal(http.ListenAndServe(":8080", root))
}
func versionRedirect(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, "/v1/", 302)
}
func openAPI(writer http.ResponseWriter, _ *http.Request) {
_, _ = writer.Write([]byte(openapi))
}