-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (36 loc) · 1.07 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
45
46
47
package main
import (
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/codelympicsdev/api/endpoints/apiclients"
"github.com/codelympicsdev/api/endpoints/auth"
"github.com/codelympicsdev/api/endpoints/graphql"
)
func main() {
r := mux.NewRouter()
v0 := r.PathPrefix("/v0").Subrouter()
//v0.Use(HTTPRedirect)
apiclients.Route(v0.PathPrefix("/apiclients").Subrouter())
auth.Route(v0.PathPrefix("/auth").Subrouter())
v0.HandleFunc("/graphql", graphql.Handle)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
err := http.ListenAndServe(":"+port, cors.New(cors.Options{AllowedHeaders: []string{"Authorization", "Content-Type"}}).Handler(r))
if err != nil {
panic(err.Error())
}
}
// HTTPRedirect redirects http traffic to https
func HTTPRedirect(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header["X-Forwarded-Proto"][0] == "http" {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently)
return
}
next.ServeHTTP(w, r)
})
}