working on adding tranx to users

This commit is contained in:
Derek McQuay 2016-08-22 15:17:25 -07:00
parent cb1f5f0464
commit 6b2f97632d
2 changed files with 126 additions and 70 deletions

View File

@ -19,10 +19,12 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
"protected": "/static/", "protected": "/static/",
"login": "/api/v0/login/", "login": "/api/v0/login/",
"logout": "/api/v0/logout/", "logout": "/api/v0/logout/",
"list": "/api/v0/list/",
"oauth": "/api/v0/oauth_cb/", "oauth": "/api/v0/oauth_cb/",
"auth": "/api/v0/auth/", "auth": "/api/v0/auth/",
"health": "/healthz", "health": "/healthz",
"list": "/api/v0/list/",
"tranx": "/api/v0/tranx/",
"fake": "/fake/",
} }
if staticFiles == "" { if staticFiles == "" {
@ -69,8 +71,10 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) {
sm.HandleFunc(prefix["info"], server.serverInfo) sm.HandleFunc(prefix["info"], server.serverInfo)
sm.HandleFunc(prefix["login"], server.login) sm.HandleFunc(prefix["login"], server.login)
sm.HandleFunc(prefix["logout"], server.logout) sm.HandleFunc(prefix["logout"], server.logout)
sm.HandleFunc(prefix["list"], server.list)
sm.HandleFunc(prefix["oauth"], server.oauthCallback) sm.HandleFunc(prefix["oauth"], server.oauthCallback)
sm.HandleFunc(prefix["auth"], server.auth) sm.HandleFunc(prefix["auth"], server.auth)
sm.HandleFunc(prefix["health"], server.health) sm.HandleFunc(prefix["health"], server.health)
sm.HandleFunc(prefix["list"], server.listUsers)
sm.HandleFunc(prefix["tranx"], server.tranx)
sm.HandleFunc(prefix["fake"], server.fakeSetup)
} }

102
server.go
View File

@ -3,6 +3,7 @@ package chipmunk
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -29,19 +30,8 @@ var (
) )
var Version string = "dev" var Version string = "dev"
var start time.Time var start time.Time
var users []user
type userInfo struct {
Sub string `json:"sub"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Profile string `json:"profile"`
Picture string `json:"picture"`
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
}
type failure struct { type failure struct {
Success bool `json:"success"` Success bool `json:"success"`
@ -113,34 +103,96 @@ func (s *Server) oauthCallback(w http.ResponseWriter, r *http.Request) {
u := userInfo{} u := userInfo{}
err = json.Unmarshal(data, &u) err = json.Unmarshal(data, &u)
if err != nil { if err != nil {
log.Printf("failed to unmarshal userinfo: '%s'\n", err) b, _ := json.Marshal(NewFailure(err.Error()))
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Error(w, string(b), http.StatusInternalServerError)
return return
} }
if authorizedEmail(u.Email) {
session, _ := store.Get(r, "creds") session, _ := store.Get(r, "creds")
session.Values["authenticated"] = true session.Values["authenticated"] = true
session.Values["uname"] = u.Email session.Values["uname"] = u.Email
if err := session.Save(r, w); err != nil { if err := session.Save(r, w); err != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
} }
addUser(u)
http.Redirect(w, r, "/static/", http.StatusTemporaryRedirect) http.Redirect(w, r, "/static/", http.StatusTemporaryRedirect)
return
}
b, _ := json.Marshal(NewFailure("Not a authorized user"))
http.Error(w, string(b), http.StatusForbidden)
return
}
func (s *Server) fakeSetup(w http.ResponseWriter, r *http.Request) {
u := userInfo{
Email: "derekmcquay@gmail.com",
}
addUser(u)
}
func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
//TODO add back in oauth
//w.Header().Set("Content-Type", "application/json")
//session, _ := store.Get(r, "creds")
//if loggedIn := session.Values["authenticated"]; loggedIn != true {
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
// return
//}
switch r.Method {
default:
b, _ := json.Marshal(NewFailure("Allowed method: GET"))
http.Error(w, string(b), http.StatusBadRequest)
return
case "GET":
u, err := getUser("derekmcquay@gmail.com")
if err != nil {
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusInternalServerError)
return
}
u.addTranx(
tranx{
Cost: 1.99,
Store: "target",
Info: "just because",
},
)
}
}
func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) {
//TODO add back in oauth
//w.Header().Set("Content-Type", "application/json")
//session, _ := store.Get(r, "creds")
//if loggedIn := session.Values["authenticated"]; loggedIn != true {
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
// return
//}
switch r.Method {
default:
b, _ := json.Marshal(NewFailure("Allowed method: GET"))
http.Error(w, string(b), http.StatusBadRequest)
return
case "GET":
j, _ := json.Marshal(users)
fmt.Fprintf(w, string(j))
}
} }
func (s *Server) list(w http.ResponseWriter, r *http.Request) { func (s *Server) list(w http.ResponseWriter, r *http.Request) {
return //w.Header().Set("Content-Type", "application/json")
// w.Header().Set("Content-Type", "application/json") //session, _ := store.Get(r, "creds")
// session, _ := store.Get(r, "creds") //if loggedIn := session.Values["authenticated"]; loggedIn != true {
// if loggedIn := session.Values["authenticated"]; loggedIn != true {
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect) // http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
// return // return
// } //}
// switch r.Method { //switch r.Method {
// default: //default:
// b, _ := json.Marshal(NewFailure("Allowed method: GET")) // b, _ := json.Marshal(NewFailure("Allowed method: POST"))
// http.Error(w, string(b), http.StatusBadRequest) // http.Error(w, string(b), http.StatusBadRequest)
// return // return
// case "GET": //case "POST":
// searchreq := r.URL.Path[len(prefix["list"]):] // searchreq := r.URL.Path[len(prefix["list"]):]
// if len(searchreq) == 0 { // if len(searchreq) == 0 {
// b, _ := json.Marshal(NewFailure("url could not be parsed")) // b, _ := json.Marshal(NewFailure("url could not be parsed"))
@ -171,14 +223,14 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) {
// for _, i := range repos { // for _, i := range repos {
// items = append(items, Item{*i.Name, *i.StargazersCount}) // items = append(items, Item{*i.Name, *i.StargazersCount})
// } // }
//
// err = json.NewEncoder(w).Encode(items) // err = json.NewEncoder(w).Encode(items)
// if err != nil { // if err != nil {
// b, _ := json.Marshal(NewFailure(err.Error())) // b, _ := json.Marshal(NewFailure(err.Error()))
// http.Error(w, string(b), http.StatusInternalServerError) // http.Error(w, string(b), http.StatusInternalServerError)
// return // return
// } // }
// } //}
} }
func (s *Server) auth(w http.ResponseWriter, r *http.Request) { func (s *Server) auth(w http.ResponseWriter, r *http.Request) {