Move handler initialization into the handlers package so it can be

reused
This commit is contained in:
Marcos Lilljedahl
2017-08-07 17:44:25 -03:00
parent ed7cefcf9c
commit 8afb922f6d
2 changed files with 80 additions and 78 deletions

79
api.go
View File

@@ -1,89 +1,12 @@
package main package main
import ( import (
"log"
"net/http"
"os"
"time"
"github.com/googollee/go-socket.io"
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/config"
"github.com/play-with-docker/play-with-docker/handlers" "github.com/play-with-docker/play-with-docker/handlers"
"github.com/play-with-docker/play-with-docker/templates"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
) )
func main() { func main() {
config.ParseFlags() config.ParseFlags()
handlers.Bootstrap() handlers.Bootstrap()
handlers.Register()
bypassCaptcha := len(os.Getenv("GOOGLE_RECAPTCHA_DISABLED")) > 0
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", handlers.WS)
server.On("error", handlers.WSError)
handlers.RegisterEvents(server)
r := mux.NewRouter()
corsRouter := mux.NewRouter()
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"}))
// Specific routes
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
corsRouter.HandleFunc("/instances/images", handlers.GetInstanceImages).Methods("GET")
corsRouter.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
corsRouter.HandleFunc("/sessions/{sessionId}/setup", handlers.SessionSetup).Methods("POST")
corsRouter.HandleFunc("/sessions/{sessionId}/instances", handlers.NewInstance).Methods("POST")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/uploads", handlers.FileUpload).Methods("POST")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE")
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/exec", handlers.Exec).Methods("POST")
r.HandleFunc("/p/{sessionId}", handlers.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.Handle("/sessions/{sessionId}/ws/", server)
r.Handle("/metrics", promhttp.Handler())
// Generic routes
r.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
if bypassCaptcha {
http.ServeFile(rw, r, "./www/bypass.html")
} else {
welcome, tmplErr := templates.GetWelcomeTemplate()
if tmplErr != nil {
log.Fatal(tmplErr)
}
rw.Write(welcome)
}
}).Methods("GET")
corsRouter.HandleFunc("/", handlers.NewSession).Methods("POST")
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,
}
log.Println("Listening on port " + config.PortNumber)
log.Fatal(httpServer.ListenAndServe())
} }

View File

@@ -2,9 +2,13 @@ package handlers
import ( import (
"log" "log"
"net/http"
"os" "os"
"time"
"github.com/googollee/go-socket.io" "github.com/googollee/go-socket.io"
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/config"
"github.com/play-with-docker/play-with-docker/docker" "github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/event" "github.com/play-with-docker/play-with-docker/event"
@@ -12,6 +16,9 @@ import (
"github.com/play-with-docker/play-with-docker/scheduler" "github.com/play-with-docker/play-with-docker/scheduler"
"github.com/play-with-docker/play-with-docker/scheduler/task" "github.com/play-with-docker/play-with-docker/scheduler/task"
"github.com/play-with-docker/play-with-docker/storage" "github.com/play-with-docker/play-with-docker/storage"
"github.com/play-with-docker/play-with-docker/templates"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
) )
var core pwd.PWDApi var core pwd.PWDApi
@@ -40,6 +47,78 @@ func Bootstrap() {
sch.AddTask(task.NewCollectStats(e, f)) sch.AddTask(task.NewCollectStats(e, f))
sch.Start() sch.Start()
Register()
}
func Register() {
bypassCaptcha := len(os.Getenv("GOOGLE_RECAPTCHA_DISABLED")) > 0
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", WS)
server.On("error", WSError)
RegisterEvents(server)
r := mux.NewRouter()
corsRouter := mux.NewRouter()
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), 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}/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("/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.Handle("/sessions/{sessionId}/ws/", server)
r.Handle("/metrics", promhttp.Handler())
// Generic routes
r.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
if bypassCaptcha {
http.ServeFile(rw, r, "./www/bypass.html")
} else {
welcome, tmplErr := templates.GetWelcomeTemplate()
if tmplErr != nil {
log.Fatal(tmplErr)
}
rw.Write(welcome)
}
}).Methods("GET")
corsRouter.HandleFunc("/", NewSession).Methods("POST")
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,
}
log.Println("Listening on port " + config.PortNumber)
log.Fatal(httpServer.ListenAndServe())
} }
func RegisterEvents(s *socketio.Server) { func RegisterEvents(s *socketio.Server) {