switch to local store

This commit is contained in:
Aaron Bieber 2024-03-31 13:43:35 -06:00
parent e9a40cc1bb
commit c7782f038c
No known key found for this signature in database
5 changed files with 76 additions and 34 deletions

4
go.mod
View File

@ -1,7 +1,3 @@
module suah.dev/kogs module suah.dev/kogs
go 1.22.1 go 1.22.1
require github.com/peterbourgon/diskv/v3 v3.0.1
require github.com/google/btree v1.0.0 // indirect

4
go.sum
View File

@ -1,4 +0,0 @@
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/peterbourgon/diskv/v3 v3.0.1 h1:x06SQA46+PKIUftmEujdwSEpIx8kR+M9eLYsUxeYveU=
github.com/peterbourgon/diskv/v3 v3.0.1/go.mod h1:kJ5Ny7vLdARGU3WUuy6uzO6T0nb/2gWcT1JiBvRmb5o=

51
main.go
View File

@ -7,10 +7,9 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"os"
"strconv" "strconv"
"time" "time"
"github.com/peterbourgon/diskv/v3"
) )
type User struct { type User struct {
@ -32,14 +31,14 @@ func (u *User) Created() []byte {
return j return j
} }
func authUserFromHeader(d *diskv.Diskv, r *http.Request) (*User, error) { func authUserFromHeader(d *Store, r *http.Request) (*User, error) {
un := r.Header.Get("x-auth-user") un := r.Header.Get("x-auth-user")
uk := r.Header.Get("x-auth-key") uk := r.Header.Get("x-auth-key")
u := &User{ u := &User{
Username: un, Username: un,
} }
storedKey, err := d.Read(u.Key()) storedKey, err := d.Get(u.Key())
if err != nil { if err != nil {
// No user // No user
return nil, err return nil, err
@ -67,43 +66,43 @@ func (p *Progress) DocKey() string {
return fmt.Sprintf("user:%s:document:%s", p.User.Username, p.Document) return fmt.Sprintf("user:%s:document:%s", p.User.Username, p.Document)
} }
func (p *Progress) Save(d *diskv.Diskv) { func (p *Progress) Save(d *Store) {
d.Write(p.DocKey()+"_percent", []byte(fmt.Sprintf("%f", p.Percentage))) d.Set(p.DocKey()+"_percent", fmt.Sprintf("%f", p.Percentage))
d.Write(p.DocKey()+"_progress", []byte(p.Progress)) d.Set(p.DocKey()+"_progress", p.Progress)
d.Write(p.DocKey()+"_device", []byte(p.Device)) d.Set(p.DocKey()+"_device", p.Device)
d.Write(p.DocKey()+"_device_id", []byte(p.DeviceID)) d.Set(p.DocKey()+"_device_id", p.DeviceID)
d.Write(p.DocKey()+"_timestamp", []byte(fmt.Sprintf("%d", (time.Now().Unix())))) d.Set(p.DocKey()+"_timestamp", fmt.Sprintf("%d", (time.Now().Unix())))
} }
func (p *Progress) Get(d *diskv.Diskv) error { func (p *Progress) Get(d *Store) error {
if p.Document == "" { if p.Document == "" {
return fmt.Errorf("invalid document") return fmt.Errorf("invalid document")
} }
pct, err := d.Read(p.DocKey() + "_percent") pct, err := d.Get(p.DocKey() + "_percent")
if err != nil { if err != nil {
return err return err
} }
p.Percentage, _ = strconv.ParseFloat(string(pct), 64) p.Percentage, _ = strconv.ParseFloat(string(pct), 64)
prog, err := d.Read(p.DocKey() + "_progress") prog, err := d.Get(p.DocKey() + "_progress")
if err != nil { if err != nil {
return err return err
} }
p.Progress = string(prog) p.Progress = string(prog)
dev, err := d.Read(p.DocKey() + "_device") dev, err := d.Get(p.DocKey() + "_device")
if err != nil { if err != nil {
return err return err
} }
p.Device = string(dev) p.Device = string(dev)
devID, err := d.Read(p.DocKey() + "_device_id") devID, err := d.Get(p.DocKey() + "_device_id")
if err != nil { if err != nil {
return err return err
} }
p.DeviceID = string(devID) p.DeviceID = string(devID)
ts, err := d.Read(p.DocKey() + "_timestamp") ts, err := d.Get(p.DocKey() + "_timestamp")
if err != nil { if err != nil {
return err return err
} }
@ -132,12 +131,18 @@ func httpLog(r *http.Request) {
func main() { func main() {
reg := flag.Bool("reg", true, "enable user registration") reg := flag.Bool("reg", true, "enable user registration")
listen := flag.String("listen", ":8383", "interface and port to listen on") listen := flag.String("listen", ":8383", "interface and port to listen on")
dbDir := flag.String("db", "db", "full path to database directory")
flag.Parse() flag.Parse()
d := diskv.New(diskv.Options{
BasePath: "db", err := os.MkdirAll(*dbDir, 0750)
Transform: func(s string) []string { return []string{} }, if err != nil {
CacheSizeMax: 1024 * 1024, log.Fatal(err)
}) }
d, err := NewStore(*dbDir)
if err != nil {
log.Fatalln(err)
}
if !*reg { if !*reg {
log.Println("registration disabled") log.Println("registration disabled")
@ -161,9 +166,9 @@ func main() {
return return
} }
_, err = d.Read(u.Key()) _, err = d.Get(u.Key())
if err != nil { if err != nil {
d.Write(u.Key(), []byte(u.Password)) d.Set(u.Key(), u.Password)
} else { } else {
log.Println("user exists") log.Println("user exists")
http.Error(w, "Username is already registered", http.StatusPaymentRequired) http.Error(w, "Username is already registered", http.StatusPaymentRequired)

View File

@ -39,8 +39,14 @@ in {
dataDir = mkOption { dataDir = mkOption {
type = types.path; type = types.path;
default = "/var/lib/kogs"; default = "/var/lib/kogs/";
description = "Path kogs will use to store the sqlite database"; description = "Path kogs will use to store the database";
};
dbDir = mkOption {
type = types.path;
default = "${cfg.dataDir}/db";
description = "Path kogs will use to store the database files";
}; };
package = mkOption { package = mkOption {
@ -75,7 +81,7 @@ in {
Group = cfg.group; Group = cfg.group;
ExecStart = '' ExecStart = ''
${cfg.package}/bin/kogs -listen ${cfg.listen} ${lib.optionalString (!cfg.registration) "-reg=false"} ${cfg.package}/bin/kogs -listen ${cfg.listen} -db ${cfg.dbDir} ${lib.optionalString (cfg.registration == false) "-reg=false"}
''; '';
}; };
}; };

39
store.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"fmt"
"log"
"os"
"path"
"strings"
)
func NewStore(s string) (*Store, error) {
fi, err := os.Lstat(s)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf("not a directory")
}
fstore := Store(s)
return &fstore, nil
}
type Store string
func (s Store) Set(key string, value string) {
err := os.WriteFile(path.Join(string(s), key), []byte(value), 0600)
if err != nil {
log.Println(err)
}
}
func (s Store) Get(key string) (string, error) {
data, err := os.ReadFile(path.Join(string(s), key))
if err != nil {
return "", nil
}
return strings.TrimSpace(string(data)), nil
}