1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-15 02:37:27 +08:00
docker-labs/handlers/new_session.go
Marcos Nils a5d1e5c207 Allow to override session timeout. (#86)
* Allow to override session timeout.

This comes useful as it can be overriden from the SDK now

* Fix comments
2017-01-31 13:51:57 +02:00

36 lines
780 B
Go

package handlers
import (
"fmt"
"log"
"net/http"
"github.com/franela/play-with-docker/services"
)
func NewSession(rw http.ResponseWriter, req *http.Request) {
req.ParseForm()
if !services.IsHuman(req) {
// User it not a human
rw.WriteHeader(http.StatusConflict)
rw.Write([]byte("Only humans are allowed!"))
return
}
reqDur := req.Form.Get("session-duration")
duration := services.GetDuration(reqDur)
s, err := services.NewSession(duration)
if err != nil {
log.Println(err)
//TODO: Return some error code
} else {
// If request is not a form, return sessionId in the body
if req.Header.Get("X-Requested-With") == "XMLHttpRequest" {
rw.Write([]byte(s.Id))
return
}
http.Redirect(rw, req, fmt.Sprintf("/p/%s", s.Id), http.StatusFound)
}
}