1
0
Fork 0

Refactor server sessions

This commit is contained in:
Gregory Eremin 2015-03-06 19:36:35 +07:00
parent e7a2494305
commit 9f36068c54
6 changed files with 48 additions and 37 deletions

View File

@ -9,23 +9,23 @@ import (
)
var (
conn *sqlx.DB
db *sqlx.DB
)
func Connect(params string) (err error) {
conn, err = sqlx.Connect("mysql", params)
conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
db, err = sqlx.Connect("mysql", params)
db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
return
}
func mustExecN(query string, arg interface{}) {
if _, err := conn.NamedExec(query, arg); err != nil {
if _, err := db.NamedExec(query, arg); err != nil {
panic(err)
}
}
func mustSelect(dest interface{}, query string, args ...interface{}) {
if err := conn.Select(dest, query, args...); err != nil {
if err := db.Select(dest, query, args...); err != nil {
panic(err)
}
}

View File

@ -7,7 +7,7 @@ import (
)
func apiOrgsHandler(w http.ResponseWriter, r *http.Request) {
login := currentUser(r)
login := sessionUser(r)
orgs := db.UserOrgs(login)
respondWith(w, orgs)
}

15
server/app.go Normal file
View File

@ -0,0 +1,15 @@
package server
import (
"net/http"
)
func appHelloHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
helloTmpl.ExecuteTemplate(w, "hello", map[string]interface{}{})
}
func appAppHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
appTmpl.ExecuteTemplate(w, "app", map[string]interface{}{})
}

View File

@ -1,7 +1,7 @@
package server
import (
"fmt"
"log"
"net/http"
"net/url"
@ -9,11 +9,6 @@ import (
"github.com/localhots/empact/task"
)
func authHelloHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
helloTmpl.ExecuteTemplate(w, "hello", map[string]interface{}{})
}
func authSigninHandler(w http.ResponseWriter, r *http.Request) {
params := url.Values{}
params.Set("client_id", config.C().ClientID)
@ -25,20 +20,20 @@ func authSigninHandler(w http.ResponseWriter, r *http.Request) {
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("error") != "" {
w.Write([]byte(r.FormValue("error_description")))
} else {
code := r.FormValue("code")
fmt.Println("Got code: ", code)
return
}
code := r.FormValue("code")
log.Println("Got code: ", code)
if _, login, err := task.Authenticate(code); err == nil {
authorize(r, login)
createSession(r, login)
} else {
panic(err)
}
}
}
func authHandler(w http.ResponseWriter, r *http.Request) {
if currentUser(r) == "" {
if sessionUser(r) == "" {
http.Redirect(w, r, "/auth/hello", 302)
}
}

View File

@ -23,13 +23,14 @@ func init() {
http.Handle("/app/", http.StripPrefix("/app/", http.FileServer(box.HTTPBox())))
http.HandleFunc("/", sessionHandler)
http.HandleFunc("/hello", appHelloHandler)
http.HandleFunc("/app", appAppHandler)
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
http.HandleFunc("/api/", authHandler)
http.HandleFunc("/api/orgs", apiOrgsHandler)
http.HandleFunc("/api/teams", apiTeamsHandler)
http.HandleFunc("/api/repos", apiReposHandler)
http.HandleFunc("/auth/hello", authHelloHandler)
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
}
func Start() {

View File

@ -9,21 +9,17 @@ import (
)
const (
sessionCookie = "session_id"
cookieName = "session_id"
)
var (
redisC = redis.NewPool(dialRedis, 10)
redisPool = redis.NewPool(dialRedis, 10)
)
func dialRedis() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
}
func sessionHandler(w http.ResponseWriter, r *http.Request) {
if cook, err := r.Cookie(sessionCookie); err != nil {
if cook, err := r.Cookie(cookieName); err != nil {
cook = &http.Cookie{
Name: sessionCookie,
Name: cookieName,
Value: uuid.New(),
Path: "/",
Expires: time.Now().Add(365 * 24 * time.Hour),
@ -33,16 +29,20 @@ func sessionHandler(w http.ResponseWriter, r *http.Request) {
}
}
func createSession(r *http.Request, login string) {
redisPool.Get().Do("HSET", "sessions", sessionID(r), login)
}
func sessionID(r *http.Request) string {
cook, _ := r.Cookie(sessionCookie)
cook, _ := r.Cookie(cookieName)
return cook.Value
}
func currentUser(r *http.Request) (login string) {
login, _ = redis.String(redisC.Get().Do("HGET", "sessions", sessionID(r)))
func sessionUser(r *http.Request) (login string) {
login, _ = redis.String(redisPool.Get().Do("HGET", "sessions", sessionID(r)))
return
}
func authorize(r *http.Request, login string) {
redisC.Get().Do("HSET", "sessions", sessionID(r), login)
func dialRedis() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
}