1
0
Fork 0
empact/server/server.go

40 lines
802 B
Go
Raw Normal View History

2015-03-05 07:46:19 +00:00
package server
import (
2015-03-05 12:57:36 +00:00
"fmt"
2015-03-05 07:46:19 +00:00
"net/http"
2015-03-05 12:57:36 +00:00
"time"
"code.google.com/p/go-uuid/uuid"
)
const (
sessionCookie = "session_id"
2015-03-05 07:46:19 +00:00
)
func Start() {
2015-03-05 12:57:36 +00:00
fmt.Println("Starting server at http://localhost:8080")
http.HandleFunc("/", sessionHandler)
2015-03-05 07:46:19 +00:00
http.HandleFunc("/auth/signin", authSigninHandler)
http.HandleFunc("/auth/callback", authCallbackHandler)
http.ListenAndServe(":8080", nil)
}
2015-03-05 12:57:36 +00:00
func sessionHandler(w http.ResponseWriter, r *http.Request) {
if cook, err := r.Cookie(sessionCookie); err != nil {
cook = &http.Cookie{
Name: sessionCookie,
Value: uuid.New(),
Path: "/",
Expires: time.Now().Add(365 * 24 * time.Hour),
HttpOnly: true,
}
http.SetCookie(w, cook)
}
}
func sessionID(r *http.Request) string {
cook, _ := r.Cookie(sessionCookie)
return cook.Value
}