1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-05 18:03:21 +08:00
Make PWD scalable
This commit is contained in:
Marcos Nils
2017-03-13 18:07:20 -03:00
committed by Jonathan Leibiusky
parent 7df7a7c68f
commit a4b0a98df3
9 changed files with 103 additions and 21 deletions

View File

@@ -1,13 +1,20 @@
package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/franela/play-with-docker/config"
"github.com/franela/play-with-docker/services"
)
type NewSessionResponse struct {
SessionId string `json:"session_id"`
Hostname string `json:"hostname"`
}
func NewSession(rw http.ResponseWriter, req *http.Request) {
req.ParseForm()
if !services.IsHuman(req) {
@@ -25,11 +32,15 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
log.Println(err)
//TODO: Return some error code
} else {
hostname := fmt.Sprintf("%s.%s", config.PWDCName, req.Host)
// If request is not a form, return sessionId in the body
if req.Header.Get("X-Requested-With") == "XMLHttpRequest" {
rw.Write([]byte(s.Id))
resp := NewSessionResponse{SessionId: s.Id, Hostname: hostname}
rw.Header().Set("Content-Type", "application/json")
json.NewEncoder(rw).Encode(resp)
return
}
http.Redirect(rw, req, fmt.Sprintf("/p/%s", s.Id), http.StatusFound)
http.Redirect(rw, req, fmt.Sprintf("http://%s/p/%s", hostname, s.Id), http.StatusFound)
}
}