1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-04 17:33: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

@@ -2,7 +2,6 @@ package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/franela/play-with-docker/services"
@@ -12,7 +11,6 @@ import (
func GetSession(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
sessionId := vars["sessionId"]
log.Println(sessionId)
session := services.GetSession(sessionId)

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)
}
}

View File

@@ -1,6 +1,23 @@
package handlers
import "net/http"
import (
"log"
"net/http"
"github.com/franela/play-with-docker/config"
"github.com/shirou/gopsutil/load"
)
func Ping(rw http.ResponseWriter, req *http.Request) {
// Get system load average of the last 5 minutes and compare it against a threashold.
a, err := load.Avg()
if err != nil {
log.Println("Cannot get system load average!", err)
} else {
if a.Load5 > config.MaxLoadAvg {
log.Printf("System load average is too high [%f]\n", a.Load5)
rw.WriteHeader(http.StatusInsufficientStorage)
}
}
}