further simplifications
This commit is contained in:
parent
7f3be2caec
commit
b3a5c0b19e
6 changed files with 1 additions and 86 deletions
|
|
@ -1,8 +0,0 @@
|
||||||
package models
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
type Token struct {
|
|
||||||
User string
|
|
||||||
Expiration time.Time
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
package models
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
Password string
|
|
||||||
Salt string
|
|
||||||
}
|
|
||||||
|
|
@ -22,6 +22,7 @@ type route struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Server) Close() {
|
func (h *Server) Close() {
|
||||||
|
h.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed static/*
|
//go:embed static/*
|
||||||
|
|
|
||||||
|
|
@ -24,16 +24,3 @@ create table ordered_cake (
|
||||||
amount integer not null,
|
amount integer not null,
|
||||||
primary key (customer_order, cake)
|
primary key (customer_order, cake)
|
||||||
);
|
);
|
||||||
|
|
||||||
create table user (
|
|
||||||
login text primary key,
|
|
||||||
password text,
|
|
||||||
role text,
|
|
||||||
salt text
|
|
||||||
);
|
|
||||||
|
|
||||||
create table session (
|
|
||||||
token text primary key,
|
|
||||||
user text,
|
|
||||||
expiration text
|
|
||||||
);
|
|
||||||
|
|
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
package store
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Store) AddSession(token, userName string, expiration time.Time) error {
|
|
||||||
query := "insert into session (token, user, expiration) values(?, ?, ?)"
|
|
||||||
_, err := s.db.Exec(query, token, userName, expiration.Format("2006-01-02 15:04"))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) RevokeSession(token string) error {
|
|
||||||
query := "delete from session where token = ?;"
|
|
||||||
_, err := s.db.Exec(query, token)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) CleanSessions() error {
|
|
||||||
now := time.Now().Format("2006-01-02 15:04")
|
|
||||||
query := "delete from session where expiration < ?;"
|
|
||||||
_, err := s.db.Exec(query, now)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) GetSessions() (map[string]models.Token, error) {
|
|
||||||
out := make(map[string]models.Token)
|
|
||||||
err := s.CleanSessions()
|
|
||||||
if err != nil {
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
query := "select token, user, expiration from session;"
|
|
||||||
rows, err := s.db.Query(query)
|
|
||||||
if err != nil {
|
|
||||||
return out, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
for rows.Next() {
|
|
||||||
var current models.Token
|
|
||||||
var token string
|
|
||||||
var expiration string
|
|
||||||
err = rows.Scan(&token, ¤t.User, &expiration)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
current.Expiration, err = time.Parse("2006-01-02 15:04", expiration)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if token == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
out[token] = current
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
@ -11,7 +11,6 @@ import (
|
||||||
type Store struct {
|
type Store struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
cakes []models.Cake
|
cakes []models.Cake
|
||||||
users map[string]models.User
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func OpenStore(filename string) *Store {
|
func OpenStore(filename string) *Store {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue