This commit is contained in:
Chris Sexton 2019-10-30 11:37:45 -04:00
parent 18d3c08ee1
commit b3fdb858a7
5 changed files with 7 additions and 113 deletions

View File

@ -15,19 +15,9 @@ func New(path string) (*Database, error) {
} }
func (d *Database) MakeDB() error { func (d *Database) MakeDB() error {
q := `create table if not exists users ( q := `create table if not exists tags (
id integer primary key, id integer primary key,
email text unique, tag text
verification integer not null,
dateCreated integer,
lastLogin integer
)`
if _, err := d.Exec(q); err != nil {
return err
}
q = `create table if not exists tags (
id integer primary key,
tag text,
)` )`
if _, err := d.Exec(q); err != nil { if _, err := d.Exec(q); err != nil {
return err return err
@ -38,24 +28,12 @@ func (d *Database) MakeDB() error {
return err return err
} }
if count == 0 { if count == 0 {
return d.populateMoods() return d.populate()
} }
return nil return nil
} }
func (d *Database) populateMoods() error { func (d *Database) populate() error {
tx := d.MustBegin() tx := d.MustBegin()
res := tx.MustExec(`insert into mood_categories (name) values ('happy')`)
id, err := res.LastInsertId()
if err != nil {
tx.Rollback()
return err
}
tx.MustExec(`insert into mood_category_texts (mood_category_id,key,value) values (?,?,?)`,
id, "😄", 1)
tx.MustExec(`insert into mood_category_texts (mood_category_id,key,value) values (?,?,?)`,
id, "😐", 0)
tx.MustExec(`insert into mood_category_texts (mood_category_id,key,value) values (?,?,?)`,
id, "😟", -1)
return tx.Commit() return tx.Commit()
} }

BIN
idea Executable file

Binary file not shown.

View File

@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"os" "os"
"strconv"
"code.chrissexton.org/cws/idea/web" "code.chrissexton.org/cws/idea/web"
"code.chrissexton.org/cws/idea/db" "code.chrissexton.org/cws/idea/db"
@ -39,6 +38,6 @@ func main() {
Msg("could not connect to database") Msg("could not connect to database")
} }
s := web.New(*httpAddr, "pub", db, *salt, h, box) s := web.New(*httpAddr, "pub", db, box)
s.Serve() s.Serve()
} }

View File

@ -3,7 +3,6 @@ package web
import ( import (
"time" "time"
"code.chrissexton.org/cws/idea/user"
"code.chrissexton.org/cws/idea/db" "code.chrissexton.org/cws/idea/db"
packr "github.com/gobuffalo/packr/v2" packr "github.com/gobuffalo/packr/v2"
@ -22,36 +21,26 @@ type Web struct {
salt string salt string
h *hashids.HashID h *hashids.HashID
box *packr.Box box *packr.Box
uf *user.UserFactory
mood *mood.Mood
} }
func New(addr, assetPath string, db *db.Database, salt string, h *hashids.HashID, box *packr.Box, email *email.EMailClient) *Web { func New(addr, assetPath string, db *db.Database, box *packr.Box) *Web {
w := &Web{ w := &Web{
addr: addr, addr: addr,
assetPath: assetPath, assetPath: assetPath,
db: db, db: db,
box: box, box: box,
uf: user.NewFactory(db, salt, h),
mood: mood.New(db),
} }
if err := db.MakeDB(); err != nil { if err := db.MakeDB(); err != nil {
log.Fatal(). log.Fatal().
Err(err). Err(err).
Msg("could not create database") Msg("could not create database")
} }
if email != nil {
u, _ := w.uf.NewUser()
w.email.SendNewUserMail("chris@chrissexton.org", u, "http://happy.chrissexton.org")
}
return w return w
} }
func (web *Web) routeSetup() *mux.Router { func (web *Web) routeSetup() *mux.Router {
r := mux.NewRouter() r := mux.NewRouter()
api := r.PathPrefix("/v1/").Subrouter() //api := r.PathPrefix("/v1/").Subrouter()
api.HandleFunc("/user/code", web.handlerRegisterCode).Methods("GET")
api.HandleFunc("/user/info", web.checkUser).Methods("GET")
r.PathPrefix("/").HandlerFunc(web.indexHandler("/index.html")) r.PathPrefix("/").HandlerFunc(web.indexHandler("/index.html"))
return r return r
} }

View File

@ -1,72 +0,0 @@
package web
import (
"encoding/json"
"fmt"
"net/http"
"github.com/rs/zerolog/log"
)
var Develop = false
type RegisterResponse struct {
ID string
DateCreated int64
Verification string `json:",omitempty"`
}
func (web *Web) handlerRegisterCode(w http.ResponseWriter, r *http.Request) {
uid, err := web.uf.NewUser()
if err != nil {
w.WriteHeader(500)
log.Error().Err(err).Msg("error from NewUserID")
fmt.Fprintf(w, "Error registering user: %s", err)
return
}
if err != nil {
w.WriteHeader(500)
log.Error().Err(err).Msg("error converting date")
fmt.Fprintf(w, "Error registering user: %s", err)
return
}
resp := RegisterResponse{
ID: uid.String(),
DateCreated: uid.DateCreated.Int64,
}
if Develop {
resp.Verification, _ = web.h.EncodeInt64([]int64{uid.Verification})
}
out, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(500)
log.Error().Err(err).Msg("error from json.Marshal")
fmt.Fprintf(w, "Error registering user: %s", err)
return
}
fmt.Fprintf(w, "%s", string(out))
}
func (web *Web) checkUser(w http.ResponseWriter, r *http.Request) {
uid := r.Header.Get("X-user-id")
verify := r.Header.Get("X-user-verification")
log.Debug().
Str("uid", uid).
Str("verify", verify).
Msg("checkUser")
user, err := web.uf.FromStr(uid, verify)
if err != nil {
log.Error().Err(err).Msg("user not known")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "User not known")
return
}
j, err := json.Marshal(user)
if err != nil {
w.WriteHeader(500)
log.Error().Err(err).Msg("could not marshal user")
fmt.Fprintf(w, "%s", err)
return
}
w.Write(j)
}