121 lines
4.1 KiB
Go
121 lines
4.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
gh "github.com/gorilla/handlers"
|
|
"github.com/gorilla/mux"
|
|
"github.com/play-with-docker/play-with-docker/config"
|
|
"github.com/play-with-docker/play-with-docker/event"
|
|
"github.com/play-with-docker/play-with-docker/pwd"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"github.com/urfave/negroni"
|
|
)
|
|
|
|
var core pwd.PWDApi
|
|
var e event.EventApi
|
|
|
|
type HandlerExtender func(h *mux.Router)
|
|
|
|
func Bootstrap(c pwd.PWDApi, ev event.EventApi) {
|
|
core = c
|
|
e = ev
|
|
}
|
|
|
|
func Register(extend HandlerExtender) {
|
|
r := mux.NewRouter()
|
|
corsRouter := mux.NewRouter()
|
|
|
|
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedMethods([]string{"GET", "POST", "HEAD", "DELETE"}), gh.AllowedOrigins([]string{"*"}))
|
|
|
|
// Specific routes
|
|
r.HandleFunc("/ping", Ping).Methods("GET")
|
|
corsRouter.HandleFunc("/instances/images", GetInstanceImages).Methods("GET")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}", GetSession).Methods("GET")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}", CloseSession).Methods("DELETE")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}/setup", SessionSetup).Methods("POST")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances", NewInstance).Methods("POST")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/uploads", FileUpload).Methods("POST")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", DeleteInstance).Methods("DELETE")
|
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", Exec).Methods("POST")
|
|
|
|
r.HandleFunc("/ooc", func(rw http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(rw, r, "./www/ooc.html")
|
|
}).Methods("GET")
|
|
r.HandleFunc("/503", func(rw http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(rw, r, "./www/503.html")
|
|
}).Methods("GET")
|
|
r.HandleFunc("/p/{sessionId}", Home).Methods("GET")
|
|
r.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./www")))
|
|
r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(rw, r, "www/robots.txt")
|
|
})
|
|
r.HandleFunc("/sdk.js", func(rw http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(rw, r, "www/sdk.js")
|
|
})
|
|
|
|
corsRouter.HandleFunc("/sessions/{sessionId}/ws/", WSH)
|
|
r.Handle("/metrics", promhttp.Handler())
|
|
|
|
// Generic routes
|
|
r.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(rw, r, "./www/landing.html")
|
|
}).Methods("GET")
|
|
|
|
r.HandleFunc("/users/me", LoggedInUser).Methods("GET")
|
|
r.HandleFunc("/users/{userId}", GetUser).Methods("GET")
|
|
r.HandleFunc("/oauth/providers", ListProviders).Methods("GET")
|
|
r.HandleFunc("/oauth/providers/{provider}/login", Login).Methods("GET")
|
|
r.HandleFunc("/oauth/providers/{provider}/callback", LoginCallback).Methods("GET")
|
|
|
|
corsRouter.HandleFunc("/", NewSession).Methods("POST")
|
|
|
|
if extend != nil {
|
|
extend(corsRouter)
|
|
}
|
|
|
|
n := negroni.Classic()
|
|
r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
|
|
n.UseHandler(r)
|
|
|
|
httpServer := http.Server{
|
|
Addr: "0.0.0.0:" + config.PortNumber,
|
|
Handler: n,
|
|
IdleTimeout: 30 * time.Second,
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
}
|
|
if config.UseLetsEncrypt {
|
|
certManager := autocert.Manager{
|
|
Prompt: autocert.AcceptTOS,
|
|
HostPolicy: autocert.HostWhitelist(config.LetsEncryptDomains...),
|
|
Cache: autocert.DirCache(config.LetsEncryptCertsDir),
|
|
}
|
|
|
|
httpServer.TLSConfig = &tls.Config{
|
|
GetCertificate: certManager.GetCertificate,
|
|
}
|
|
|
|
go func() {
|
|
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(rw, r, fmt.Sprintf("https://%s", r.Host), http.StatusMovedPermanently)
|
|
})
|
|
log.Println("Starting redirect server")
|
|
log.Fatal(http.ListenAndServe(":3001", nil))
|
|
log.Fatal(httpServer.ListenAndServe())
|
|
}()
|
|
|
|
log.Println("Listening on port " + config.PortNumber)
|
|
log.Fatal(httpServer.ListenAndServeTLS("", ""))
|
|
} else {
|
|
log.Println("Listening on port " + config.PortNumber)
|
|
log.Fatal(httpServer.ListenAndServe())
|
|
}
|
|
|
|
}
|