From 4d533d295c6b5439f21796f838e174c9ebbe3b0e Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Sat, 4 Feb 2017 19:48:13 -0800 Subject: [PATCH] made changes to server to incoporate db --- cmd/chipd/main.go | 14 ++- routes.go | 10 +- server.go | 274 ++++++++++++++++++++++++---------------------- 3 files changed, 158 insertions(+), 140 deletions(-) diff --git a/cmd/chipd/main.go b/cmd/chipd/main.go index 2fff5d8..9effc8f 100644 --- a/cmd/chipd/main.go +++ b/cmd/chipd/main.go @@ -20,6 +20,8 @@ import ( type Config struct { Host string Port int + DBHost string + DBName string ClientID string ClientSecret string CookieSecret string @@ -38,7 +40,10 @@ func main() { Short: "run command", Long: `run chipd with given options`, Run: func(cmd *cobra.Command, args []string) { - config := &Config{} + config := &Config{ + DBHost: "localhost", + DBName: "postgres", + } err := envconfig.Process("chipd", config) if err != nil { log.Fatal(err) @@ -81,13 +86,18 @@ func main() { }() sm := http.NewServeMux() - _ = chipmunk.NewServer( + _, err = chipmunk.NewServer( sm, config.ClientID, config.ClientSecret, config.CookieSecret, + config.DBHost, + config.DBName, "", ) + if err != nil { + log.Fatalf("problem initializing Chipd server: %+v", err) + } hostname := "localhost" if config.Host == "" { diff --git a/routes.go b/routes.go index d6177b0..c6f62d9 100644 --- a/routes.go +++ b/routes.go @@ -68,16 +68,16 @@ func addRoutes(sm *http.ServeMux, server *Server, staticFiles string) { ) } - store = sessions.NewCookieStore([]byte(server.CookieSecret)) + store = sessions.NewCookieStore([]byte(server.cookieSecret)) + //sm.HandleFunc(prefix["list"], server.listUsers) + //sm.HandleFunc(prefix["tranx"], server.tranx) + //sm.HandleFunc(prefix["cost"], server.costPerMonth) sm.HandleFunc(prefix["protected"], server.plist) sm.HandleFunc(prefix["info"], server.serverInfo) sm.HandleFunc(prefix["login"], server.login) sm.HandleFunc(prefix["logout"], server.logout) sm.HandleFunc(prefix["oauth"], server.oauthCallback) sm.HandleFunc(prefix["auth"], server.auth) - sm.HandleFunc(prefix["health"], server.health) - sm.HandleFunc(prefix["list"], server.listUsers) - sm.HandleFunc(prefix["tranx"], server.tranx) - sm.HandleFunc(prefix["cost"], server.costPerMonth) sm.HandleFunc(prefix["fake"], server.fakeSetup) + sm.HandleFunc(prefix["health"], server.health) } diff --git a/server.go b/server.go index 299c562..dfcf840 100644 --- a/server.go +++ b/server.go @@ -48,9 +48,10 @@ func NewFailure(msg string) *failure { } type Server struct { - ClientID string - ClientSecret string - CookieSecret string + clientID string + clientSecret string + cookieSecret string + db *DB } func init() { @@ -58,14 +59,21 @@ func init() { start = time.Now() } -func NewServer(sm *http.ServeMux, clientId, clientSecret, cookieSecret, static string) *Server { +func NewServer(sm *http.ServeMux, clientId, clientSecret, cookieSecret, dbhost, dbname, static string) (*Server, error) { + db, err := NewDB(dbhost, dbname) + if err != nil { + return nil, err + } + db.db.SetMaxOpenConns(32) + server := &Server{ - ClientID: clientId, - ClientSecret: clientSecret, - CookieSecret: cookieSecret, + clientID: clientId, + clientSecret: clientSecret, + cookieSecret: cookieSecret, + db: db, } addRoutes(sm, server, static) - return server + return server, nil } func (s *Server) fakeSetup(w http.ResponseWriter, r *http.Request) { @@ -75,137 +83,137 @@ func (s *Server) fakeSetup(w http.ResponseWriter, r *http.Request) { 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, err := store.Get(r, "creds") - //if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - //} - //if loggedIn := session.Values["authenticated"]; loggedIn != true { - // http.Redirect(w, r, "/", http.StatusTemporaryRedirect) - // return - //} - switch r.Method { - default: - b, _ := json.Marshal(NewFailure("Allowed method: POST and GET")) - http.Error(w, string(b), http.StatusBadRequest) - return - case "GET": - u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session - if err != nil { - b, _ := json.Marshal(NewFailure(err.Error())) - http.Error(w, string(b), http.StatusInternalServerError) - return - } - json.NewEncoder(w).Encode(users[u].txs) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - case "POST": - u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session - if err != nil { - b, _ := json.Marshal(NewFailure(err.Error())) - http.Error(w, string(b), http.StatusInternalServerError) - return - } - - t := tranx{} - err = json.NewDecoder(r.Body).Decode(&t) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - defer r.Body.Close() - - users[u].txs = append(users[u].txs, - tranx{ - Cost: t.Cost, - Store: t.Store, - Info: t.Info, - Month: t.Month, - }, - ) - } -} - -func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) { - //TODO add back in oauth - //w.Header().Set("Content-Type", "application/json") - //session, err := store.Get(r, "creds") - //if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - //} - //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") //TODO will grab this from session - if err != nil { - b, _ := json.Marshal(NewFailure(err.Error())) - http.Error(w, string(b), http.StatusInternalServerError) - return - } - monthCost := make(map[time.Month]float32) - for _, t := range users[u].txs { - c, ok := monthCost[t.Month] - if !ok { - monthCost[t.Month] = t.Cost - continue - } - monthCost[t.Month] = t.Cost + c - } - err = json.NewEncoder(w).Encode(monthCost) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } -} - -func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) { - //TODO add back in oauth - //w.Header().Set("Content-Type", "application/json") - //session, err := store.Get(r, "creds") - //if err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - //} - //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": - err := json.NewEncoder(w).Encode(users) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } -} - +//func (s *Server) tranx(w http.ResponseWriter, r *http.Request) { +// //TODO add back in oauth +// //w.Header().Set("Content-Type", "application/json") +// //session, err := store.Get(r, "creds") +// //if err != nil { +// // http.Error(w, err.Error(), http.StatusInternalServerError) +// // return +// //} +// //if loggedIn := session.Values["authenticated"]; loggedIn != true { +// // http.Redirect(w, r, "/", http.StatusTemporaryRedirect) +// // return +// //} +// switch r.Method { +// default: +// b, _ := json.Marshal(NewFailure("Allowed method: POST and GET")) +// http.Error(w, string(b), http.StatusBadRequest) +// return +// case "GET": +// u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session +// if err != nil { +// b, _ := json.Marshal(NewFailure(err.Error())) +// http.Error(w, string(b), http.StatusInternalServerError) +// return +// } +// json.NewEncoder(w).Encode(users[u].txs) +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } +// case "POST": +// u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session +// if err != nil { +// b, _ := json.Marshal(NewFailure(err.Error())) +// http.Error(w, string(b), http.StatusInternalServerError) +// return +// } +// +// t := tranx{} +// err = json.NewDecoder(r.Body).Decode(&t) +// if err != nil { +// http.Error(w, err.Error(), http.StatusBadRequest) +// return +// } +// defer r.Body.Close() +// +// users[u].txs = append(users[u].txs, +// tranx{ +// Cost: t.Cost, +// Store: t.Store, +// Info: t.Info, +// Month: t.Month, +// }, +// ) +// } +//} +// +//func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) { +// //TODO add back in oauth +// //w.Header().Set("Content-Type", "application/json") +// //session, err := store.Get(r, "creds") +// //if err != nil { +// // http.Error(w, err.Error(), http.StatusInternalServerError) +// // return +// //} +// //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") //TODO will grab this from session +// if err != nil { +// b, _ := json.Marshal(NewFailure(err.Error())) +// http.Error(w, string(b), http.StatusInternalServerError) +// return +// } +// monthCost := make(map[time.Month]float32) +// for _, t := range users[u].txs { +// c, ok := monthCost[t.Month] +// if !ok { +// monthCost[t.Month] = t.Cost +// continue +// } +// monthCost[t.Month] = t.Cost + c +// } +// err = json.NewEncoder(w).Encode(monthCost) +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } +// } +//} +// +//func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) { +// //TODO add back in oauth +// //w.Header().Set("Content-Type", "application/json") +// //session, err := store.Get(r, "creds") +// //if err != nil { +// // http.Error(w, err.Error(), http.StatusInternalServerError) +// // return +// //} +// //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": +// err := json.NewEncoder(w).Encode(users) +// if err != nil { +// http.Error(w, err.Error(), http.StatusInternalServerError) +// return +// } +// } +//} +// func (s *Server) login(w http.ResponseWriter, r *http.Request) { session, _ := store.Get(r, "creds") if loggedIn := session.Values["authenticated"]; loggedIn == true { http.Redirect(w, r, "/static/", http.StatusTemporaryRedirect) return } - oauthConf.ClientID = s.ClientID - oauthConf.ClientSecret = s.ClientSecret + oauthConf.ClientID = s.clientID + oauthConf.ClientSecret = s.clientSecret url := oauthConf.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline) http.Redirect(w, r, url, http.StatusTemporaryRedirect) }