1
0
Fork 0
empact/server/request.go

118 lines
2.4 KiB
Go
Raw Normal View History

2015-03-07 14:56:02 +00:00
package server
import (
"encoding/json"
"net/http"
"strconv"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/garyburd/redigo/redis"
)
type (
request struct {
2015-03-07 15:04:28 +00:00
r *http.Request
w http.ResponseWriter
sid string
token string
2015-03-07 15:04:28 +00:00
login string
2015-03-07 14:56:02 +00:00
}
statRequest struct {
2015-03-09 13:31:51 +00:00
Org string `structs:"org"`
Team string `structs:"team"`
User string `structs:"user"`
Repo string `structs:"repo"`
From int64 `structs:"from"`
To int64 `structs:"to"`
Item string `structs:"item"`
2015-03-07 14:56:02 +00:00
}
)
func parseRequest(w http.ResponseWriter, r *http.Request) (*request, *statRequest) {
2015-03-07 15:02:15 +00:00
sid := sessionID(w, r)
login, _ := redis.String(redisPool.Get().Do("HGET", "sessions", sid))
token, _ := redis.String(redisPool.Get().Do("HGET", "tokens", sid))
2015-03-07 14:56:02 +00:00
req := &request{
2015-03-07 15:04:28 +00:00
r: r,
w: w,
sid: sid,
token: token,
2015-03-07 15:04:28 +00:00
login: login,
2015-03-07 14:56:02 +00:00
}
return req, parseStatRequest(r)
}
func (r *request) authorize(token, login string) {
redisPool.Get().Do("HSET", "tokens", r.sid, token)
2015-03-07 15:04:28 +00:00
redisPool.Get().Do("HSET", "sessions", r.sid, login)
2015-03-07 14:56:02 +00:00
}
func (r *request) respondWith(resp interface{}) {
b, err := json.Marshal(resp)
if err != nil {
panic(err)
}
r.w.Header().Set("Access-Control-Allow-Origin", "*")
r.w.Header().Set("Content-Type", "application/json; charset=utf8")
r.w.Write(b)
}
func parseStatRequest(r *http.Request) *statRequest {
var err error
var from, to int64
if r.FormValue("from") != "" {
if from, err = strconv.ParseInt(r.FormValue("from"), 10, 64); err != nil {
panic(err)
}
2015-03-08 08:42:40 +00:00
} else {
from = 0
2015-03-07 14:56:02 +00:00
}
if r.FormValue("to") != "" {
if to, err = strconv.ParseInt(r.FormValue("to"), 10, 64); err != nil {
panic(err)
}
2015-03-08 08:42:40 +00:00
} else {
to = time.Now().Unix()
2015-03-07 14:56:02 +00:00
}
2015-03-08 11:17:56 +00:00
var item string
switch val := r.FormValue("item"); val {
2015-03-09 13:31:51 +00:00
case "author", "user":
2015-03-08 11:17:56 +00:00
item = "c.author"
case "team":
item = "t.name"
default:
item = "c.repo"
}
2015-03-07 14:56:02 +00:00
return &statRequest{
2015-03-09 13:31:51 +00:00
Org: r.FormValue("org"),
Team: r.FormValue("team"),
User: r.FormValue("user"),
Repo: r.FormValue("repo"),
From: from,
To: to,
Item: item,
2015-03-07 14:56:02 +00:00
}
}
2015-03-07 15:02:15 +00:00
func sessionID(w http.ResponseWriter, r *http.Request) string {
var cook *http.Cookie
var err error
if cook, err = r.Cookie(cookieName); err != nil {
2015-03-07 14:56:02 +00:00
cook = &http.Cookie{
Name: cookieName,
Value: uuid.New(),
Path: "/",
Expires: time.Now().Add(365 * 24 * time.Hour),
HttpOnly: true,
}
http.SetCookie(w, cook)
r.AddCookie(cook)
}
2015-03-07 15:02:15 +00:00
return cook.Value
2015-03-07 14:56:02 +00:00
}