1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 18:27:25 +08:00
docker-labs/api.go
Jonathan Leibiusky @xetorthio ec9d34ffda Add reverse proxy endpoint.
It works by using the Host of the request. When it receives something in
the form of: `<node>.<session>.play-with-docker.com` it does a reverse
proxy http request to `node`, validating that the `node` actually belongs
to the `session`.
If the node has a prefix `ip` and continues with a valid IP address
where the dots where replaces by underscores (like `ip10_0_0_1`) then it
will remove the `ip` prefix and and replace the underscores by dots, and
assume it is an ip address.
2016-11-23 11:52:59 -03:00

72 lines
2.1 KiB
Go

package main
import (
"log"
"net/http"
"os"
"github.com/franela/play-with-docker/handlers"
"github.com/franela/play-with-docker/services"
"github.com/franela/play-with-docker/templates"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"flag"
"strconv"
)
func main() {
var portNumber int
flag.IntVar(&portNumber, "port", 3000, "Give a TCP port to run the application")
flag.Parse()
welcome, tmplErr := templates.GetWelcomeTemplate()
if tmplErr != nil {
log.Fatal(tmplErr)
}
server := services.CreateWSServer()
server.On("connection", handlers.WS)
server.On("error", handlers.WSError)
err := services.LoadSessionsFromDisk()
if err != nil && !os.IsNotExist(err) {
log.Fatal("Error decoding sessions from disk ", err)
}
r := mux.NewRouter()
r.StrictSlash(false)
r.HandleFunc("/ping", http.HandlerFunc(handlers.Ping)).Methods("GET")
r.HandleFunc("/", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write(welcome)
})).Methods("GET")
r.HandleFunc("/", http.HandlerFunc(handlers.NewSession)).Methods("POST")
r.HandleFunc("/sessions/{sessionId}", http.HandlerFunc(handlers.GetSession)).Methods("GET")
r.HandleFunc("/sessions/{sessionId}/instances", http.HandlerFunc(handlers.NewInstance)).Methods("POST")
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", http.HandlerFunc(handlers.DeleteInstance)).Methods("DELETE")
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./www/index.html")
})
r.HandleFunc("/p/{sessionId}", h).Methods("GET")
r.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./www")))
r.HandleFunc("/robots.txt", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/robots.txt")
}))
r.Handle("/sessions/{sessionId}/ws/", server)
// Reverse proxy
r.Host(`{node}.{session}.play-with-docker.com`).Handler(handlers.NewMultipleHostReverseProxy())
n := negroni.Classic()
n.UseHandler(r)
log.Println("Listening on port "+ strconv.Itoa(portNumber))
log.Fatal(http.ListenAndServe("0.0.0.0:"+strconv.Itoa(portNumber), n))
}