made changes to server to incoporate db
This commit is contained in:
parent
4d5077d72d
commit
4d533d295c
@ -20,6 +20,8 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
|
DBHost string
|
||||||
|
DBName string
|
||||||
ClientID string
|
ClientID string
|
||||||
ClientSecret string
|
ClientSecret string
|
||||||
CookieSecret string
|
CookieSecret string
|
||||||
@ -38,7 +40,10 @@ func main() {
|
|||||||
Short: "run command",
|
Short: "run command",
|
||||||
Long: `run chipd with given options`,
|
Long: `run chipd with given options`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
config := &Config{}
|
config := &Config{
|
||||||
|
DBHost: "localhost",
|
||||||
|
DBName: "postgres",
|
||||||
|
}
|
||||||
err := envconfig.Process("chipd", config)
|
err := envconfig.Process("chipd", config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -81,13 +86,18 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
sm := http.NewServeMux()
|
sm := http.NewServeMux()
|
||||||
_ = chipmunk.NewServer(
|
_, err = chipmunk.NewServer(
|
||||||
sm,
|
sm,
|
||||||
config.ClientID,
|
config.ClientID,
|
||||||
config.ClientSecret,
|
config.ClientSecret,
|
||||||
config.CookieSecret,
|
config.CookieSecret,
|
||||||
|
config.DBHost,
|
||||||
|
config.DBName,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("problem initializing Chipd server: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
hostname := "localhost"
|
hostname := "localhost"
|
||||||
if config.Host == "" {
|
if config.Host == "" {
|
||||||
|
10
routes.go
10
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["protected"], server.plist)
|
||||||
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["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["list"], server.listUsers)
|
|
||||||
sm.HandleFunc(prefix["tranx"], server.tranx)
|
|
||||||
sm.HandleFunc(prefix["cost"], server.costPerMonth)
|
|
||||||
sm.HandleFunc(prefix["fake"], server.fakeSetup)
|
sm.HandleFunc(prefix["fake"], server.fakeSetup)
|
||||||
|
sm.HandleFunc(prefix["health"], server.health)
|
||||||
}
|
}
|
||||||
|
242
server.go
242
server.go
@ -48,9 +48,10 @@ func NewFailure(msg string) *failure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
ClientID string
|
clientID string
|
||||||
ClientSecret string
|
clientSecret string
|
||||||
CookieSecret string
|
cookieSecret string
|
||||||
|
db *DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -58,14 +59,21 @@ func init() {
|
|||||||
start = time.Now()
|
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{
|
server := &Server{
|
||||||
ClientID: clientId,
|
clientID: clientId,
|
||||||
ClientSecret: clientSecret,
|
clientSecret: clientSecret,
|
||||||
CookieSecret: cookieSecret,
|
cookieSecret: cookieSecret,
|
||||||
|
db: db,
|
||||||
}
|
}
|
||||||
addRoutes(sm, server, static)
|
addRoutes(sm, server, static)
|
||||||
return server
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) fakeSetup(w http.ResponseWriter, r *http.Request) {
|
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)
|
addUser(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
|
//func (s *Server) tranx(w http.ResponseWriter, r *http.Request) {
|
||||||
//TODO add back in oauth
|
// //TODO add back in oauth
|
||||||
//w.Header().Set("Content-Type", "application/json")
|
// //w.Header().Set("Content-Type", "application/json")
|
||||||
//session, err := store.Get(r, "creds")
|
// //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 {
|
// if err != nil {
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
//if loggedIn := session.Values["authenticated"]; loggedIn != true {
|
// case "POST":
|
||||||
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
// 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
|
// return
|
||||||
// }
|
// }
|
||||||
switch r.Method {
|
//
|
||||||
default:
|
// t := tranx{}
|
||||||
b, _ := json.Marshal(NewFailure("Allowed method: POST and GET"))
|
// err = json.NewDecoder(r.Body).Decode(&t)
|
||||||
http.Error(w, string(b), http.StatusBadRequest)
|
// if err != nil {
|
||||||
return
|
// http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
case "GET":
|
// return
|
||||||
u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session
|
// }
|
||||||
if err != nil {
|
// defer r.Body.Close()
|
||||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
//
|
||||||
http.Error(w, string(b), http.StatusInternalServerError)
|
// users[u].txs = append(users[u].txs,
|
||||||
return
|
// tranx{
|
||||||
}
|
// Cost: t.Cost,
|
||||||
json.NewEncoder(w).Encode(users[u].txs)
|
// Store: t.Store,
|
||||||
if err != nil {
|
// Info: t.Info,
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
// Month: t.Month,
|
||||||
return
|
// },
|
||||||
}
|
// )
|
||||||
case "POST":
|
// }
|
||||||
u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session
|
//}
|
||||||
if err != nil {
|
//
|
||||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
//func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, string(b), http.StatusInternalServerError)
|
// //TODO add back in oauth
|
||||||
return
|
// //w.Header().Set("Content-Type", "application/json")
|
||||||
}
|
// //session, err := store.Get(r, "creds")
|
||||||
|
// //if err != nil {
|
||||||
t := tranx{}
|
// // http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
err = json.NewDecoder(r.Body).Decode(&t)
|
// // return
|
||||||
if err != nil {
|
// //}
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
// //if loggedIn := session.Values["authenticated"]; loggedIn != true {
|
||||||
return
|
// // http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
}
|
// // return
|
||||||
defer r.Body.Close()
|
// //}
|
||||||
|
// switch r.Method {
|
||||||
users[u].txs = append(users[u].txs,
|
// default:
|
||||||
tranx{
|
// b, _ := json.Marshal(NewFailure("Allowed method: GET"))
|
||||||
Cost: t.Cost,
|
// http.Error(w, string(b), http.StatusBadRequest)
|
||||||
Store: t.Store,
|
// return
|
||||||
Info: t.Info,
|
// case "GET":
|
||||||
Month: t.Month,
|
// 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
|
||||||
|
// }
|
||||||
func (s *Server) costPerMonth(w http.ResponseWriter, r *http.Request) {
|
// monthCost := make(map[time.Month]float32)
|
||||||
//TODO add back in oauth
|
// for _, t := range users[u].txs {
|
||||||
//w.Header().Set("Content-Type", "application/json")
|
// c, ok := monthCost[t.Month]
|
||||||
//session, err := store.Get(r, "creds")
|
// if !ok {
|
||||||
|
// monthCost[t.Month] = t.Cost
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
// monthCost[t.Month] = t.Cost + c
|
||||||
|
// }
|
||||||
|
// err = json.NewEncoder(w).Encode(monthCost)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
// 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"))
|
//func (s *Server) listUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, string(b), http.StatusBadRequest)
|
// //TODO add back in oauth
|
||||||
return
|
// //w.Header().Set("Content-Type", "application/json")
|
||||||
case "GET":
|
// //session, err := store.Get(r, "creds")
|
||||||
u, err := getUser("derekmcquay@gmail.com") //TODO will grab this from session
|
// //if err != nil {
|
||||||
if err != nil {
|
// // http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
b, _ := json.Marshal(NewFailure(err.Error()))
|
// // return
|
||||||
http.Error(w, string(b), http.StatusInternalServerError)
|
// //}
|
||||||
return
|
// //if loggedIn := session.Values["authenticated"]; loggedIn != true {
|
||||||
}
|
// // http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
monthCost := make(map[time.Month]float32)
|
// // return
|
||||||
for _, t := range users[u].txs {
|
// //}
|
||||||
c, ok := monthCost[t.Month]
|
// switch r.Method {
|
||||||
if !ok {
|
// default:
|
||||||
monthCost[t.Month] = t.Cost
|
// b, _ := json.Marshal(NewFailure("Allowed method: GET"))
|
||||||
continue
|
// http.Error(w, string(b), http.StatusBadRequest)
|
||||||
}
|
// return
|
||||||
monthCost[t.Month] = t.Cost + c
|
// case "GET":
|
||||||
}
|
// err := json.NewEncoder(w).Encode(users)
|
||||||
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 {
|
// if err != nil {
|
||||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
// return
|
// 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) {
|
func (s *Server) login(w http.ResponseWriter, r *http.Request) {
|
||||||
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, "/static/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/static/", http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
oauthConf.ClientID = s.ClientID
|
oauthConf.ClientID = s.clientID
|
||||||
oauthConf.ClientSecret = s.ClientSecret
|
oauthConf.ClientSecret = s.clientSecret
|
||||||
url := oauthConf.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline)
|
url := oauthConf.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline)
|
||||||
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user