Refactor server sessions
This commit is contained in:
parent
e7a2494305
commit
9f36068c54
|
@ -9,23 +9,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
conn *sqlx.DB
|
db *sqlx.DB
|
||||||
)
|
)
|
||||||
|
|
||||||
func Connect(params string) (err error) {
|
func Connect(params string) (err error) {
|
||||||
conn, err = sqlx.Connect("mysql", params)
|
db, err = sqlx.Connect("mysql", params)
|
||||||
conn.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
|
db.Mapper = reflectx.NewMapperFunc("json", strings.ToLower)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustExecN(query string, arg interface{}) {
|
func mustExecN(query string, arg interface{}) {
|
||||||
if _, err := conn.NamedExec(query, arg); err != nil {
|
if _, err := db.NamedExec(query, arg); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustSelect(dest interface{}, query string, args ...interface{}) {
|
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)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func apiOrgsHandler(w http.ResponseWriter, r *http.Request) {
|
func apiOrgsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
login := currentUser(r)
|
login := sessionUser(r)
|
||||||
orgs := db.UserOrgs(login)
|
orgs := db.UserOrgs(login)
|
||||||
respondWith(w, orgs)
|
respondWith(w, orgs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{}{})
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
@ -9,11 +9,6 @@ import (
|
||||||
"github.com/localhots/empact/task"
|
"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) {
|
func authSigninHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Set("client_id", config.C().ClientID)
|
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) {
|
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.FormValue("error") != "" {
|
if r.FormValue("error") != "" {
|
||||||
w.Write([]byte(r.FormValue("error_description")))
|
w.Write([]byte(r.FormValue("error_description")))
|
||||||
} else {
|
return
|
||||||
code := r.FormValue("code")
|
}
|
||||||
fmt.Println("Got code: ", code)
|
|
||||||
|
|
||||||
|
code := r.FormValue("code")
|
||||||
|
log.Println("Got code: ", code)
|
||||||
if _, login, err := task.Authenticate(code); err == nil {
|
if _, login, err := task.Authenticate(code); err == nil {
|
||||||
authorize(r, login)
|
createSession(r, login)
|
||||||
} else {
|
} else {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func authHandler(w http.ResponseWriter, r *http.Request) {
|
func authHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if currentUser(r) == "" {
|
if sessionUser(r) == "" {
|
||||||
http.Redirect(w, r, "/auth/hello", 302)
|
http.Redirect(w, r, "/auth/hello", 302)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,13 +23,14 @@ func init() {
|
||||||
http.Handle("/app/", http.StripPrefix("/app/", http.FileServer(box.HTTPBox())))
|
http.Handle("/app/", http.StripPrefix("/app/", http.FileServer(box.HTTPBox())))
|
||||||
|
|
||||||
http.HandleFunc("/", sessionHandler)
|
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/", authHandler)
|
||||||
http.HandleFunc("/api/orgs", apiOrgsHandler)
|
http.HandleFunc("/api/orgs", apiOrgsHandler)
|
||||||
http.HandleFunc("/api/teams", apiTeamsHandler)
|
http.HandleFunc("/api/teams", apiTeamsHandler)
|
||||||
http.HandleFunc("/api/repos", apiReposHandler)
|
http.HandleFunc("/api/repos", apiReposHandler)
|
||||||
http.HandleFunc("/auth/hello", authHelloHandler)
|
|
||||||
http.HandleFunc("/auth/signin", authSigninHandler)
|
|
||||||
http.HandleFunc("/auth/callback", authCallbackHandler)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
|
|
|
@ -9,21 +9,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sessionCookie = "session_id"
|
cookieName = "session_id"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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) {
|
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{
|
cook = &http.Cookie{
|
||||||
Name: sessionCookie,
|
Name: cookieName,
|
||||||
Value: uuid.New(),
|
Value: uuid.New(),
|
||||||
Path: "/",
|
Path: "/",
|
||||||
Expires: time.Now().Add(365 * 24 * time.Hour),
|
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 {
|
func sessionID(r *http.Request) string {
|
||||||
cook, _ := r.Cookie(sessionCookie)
|
cook, _ := r.Cookie(cookieName)
|
||||||
return cook.Value
|
return cook.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentUser(r *http.Request) (login string) {
|
func sessionUser(r *http.Request) (login string) {
|
||||||
login, _ = redis.String(redisC.Get().Do("HGET", "sessions", sessionID(r)))
|
login, _ = redis.String(redisPool.Get().Do("HGET", "sessions", sessionID(r)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func authorize(r *http.Request, login string) {
|
func dialRedis() (redis.Conn, error) {
|
||||||
redisC.Get().Do("HSET", "sessions", sessionID(r), login)
|
return redis.Dial("tcp", ":6379")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue