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)
} }

188
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,72 +103,134 @@ 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
} }
session, _ := store.Get(r, "creds") if authorizedEmail(u.Email) {
session.Values["authenticated"] = true session, _ := store.Get(r, "creds")
session.Values["uname"] = u.Email session.Values["authenticated"] = true
if err := session.Save(r, w); err != nil { session.Values["uname"] = u.Email
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) if err := session.Save(r, w); err != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
addUser(u)
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))
} }
http.Redirect(w, r, "/static/", http.StatusTemporaryRedirect)
} }
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: POST"))
// b, _ := json.Marshal(NewFailure("Allowed method: GET")) // http.Error(w, string(b), http.StatusBadRequest)
// return
//case "POST":
// searchreq := r.URL.Path[len(prefix["list"]):]
// if len(searchreq) == 0 {
// b, _ := json.Marshal(NewFailure("url could not be parsed"))
// http.Error(w, string(b), http.StatusBadRequest) // http.Error(w, string(b), http.StatusBadRequest)
// return // return
// case "GET":
// searchreq := r.URL.Path[len(prefix["list"]):]
// if len(searchreq) == 0 {
// b, _ := json.Marshal(NewFailure("url could not be parsed"))
// http.Error(w, string(b), http.StatusBadRequest)
// return
// }
// if searchreq[len(searchreq)-1] != '/' {
// http.Redirect(w, r, prefix["list"]+searchreq+"/", http.StatusMovedPermanently)
// return
// }
// searchReqParsed := strings.Split(searchreq, "/")
// client := github.NewClient(nil)
// if s.ApiToken != "" {
// ts := oauth2.StaticTokenSource(
// &oauth2.Token{AccessToken: s.ApiToken},
// )
// tc := oauth2.NewClient(oauth2.NoContext, ts)
// client = github.NewClient(tc)
// }
// opt := &github.RepositoryListOptions{}
// repos, _, err := client.Repositories.List(searchReqParsed[0], opt)
// if err != nil {
// b, _ := json.Marshal(NewFailure("user could not be found"))
// http.Error(w, string(b), http.StatusBadRequest)
// return
// }
// var items []Item
// for _, i := range repos {
// items = append(items, Item{*i.Name, *i.StargazersCount})
// }
//
// err = json.NewEncoder(w).Encode(items)
// if err != nil {
// b, _ := json.Marshal(NewFailure(err.Error()))
// http.Error(w, string(b), http.StatusInternalServerError)
// return
// }
// } // }
// if searchreq[len(searchreq)-1] != '/' {
// http.Redirect(w, r, prefix["list"]+searchreq+"/", http.StatusMovedPermanently)
// return
// }
// searchReqParsed := strings.Split(searchreq, "/")
// client := github.NewClient(nil)
// if s.ApiToken != "" {
// ts := oauth2.StaticTokenSource(
// &oauth2.Token{AccessToken: s.ApiToken},
// )
// tc := oauth2.NewClient(oauth2.NoContext, ts)
// client = github.NewClient(tc)
// }
// opt := &github.RepositoryListOptions{}
// repos, _, err := client.Repositories.List(searchReqParsed[0], opt)
// if err != nil {
// b, _ := json.Marshal(NewFailure("user could not be found"))
// http.Error(w, string(b), http.StatusBadRequest)
// return
// }
// var items []Item
// for _, i := range repos {
// items = append(items, Item{*i.Name, *i.StargazersCount})
// }
// err = json.NewEncoder(w).Encode(items)
// if err != nil {
// b, _ := json.Marshal(NewFailure(err.Error()))
// http.Error(w, string(b), http.StatusInternalServerError)
// return
// }
//}
} }
func (s *Server) auth(w http.ResponseWriter, r *http.Request) { func (s *Server) auth(w http.ResponseWriter, r *http.Request) {