From 08333f71b07dd69cdf95f0397e319457b0fe7369 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 15 Aug 2024 00:12:01 -0700 Subject: [PATCH 1/3] checkpoing --- api/serve.go | 24 +++++++- database/kennel.go | 137 ++++++++++++++++++++++++++++++++++++++++++ database/migrate.go | 28 +++++++++ static/css/styles.css | 26 -------- templates/about.html | 10 ++- templates/kennel.html | 81 +++++++++++++++++++++++++ 6 files changed, 271 insertions(+), 35 deletions(-) create mode 100644 database/kennel.go create mode 100644 templates/kennel.html diff --git a/api/serve.go b/api/serve.go index ca8142b..2078cdc 100644 --- a/api/serve.go +++ b/api/serve.go @@ -172,10 +172,28 @@ func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server { LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(hcaptcha.CaptchaVerificationContinuation, hcaptcha.CaptchaVerificationContinuation)(guestbook.SignGuestbookContinuation, FailurePassingContinuation)(guestbook.ListGuestbookContinuation, guestbook.ListGuestbookContinuation)(hcaptcha.CaptchaArgsContinuation, hcaptcha.CaptchaArgsContinuation)(template.TemplateContinuation("guestbook.html", true), template.TemplateContinuation("guestbook.html", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) - mux.HandleFunc("GET /{name}", func(w http.ResponseWriter, r *http.Request) { + /* + mux.HandleFunc("GET /kennel", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(kennel.GetKennelStateContinuation, kennel.GetKennelStateContinuation)(template.TemplateContinuation("kennel.json", true), template.TemplateContinuation("kennel.json", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) + + mux.HandleFunc("GET /kennel/cats", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(kennel.ListUserCats, auth.GoLoginContinuation)(template.TemplateContinuation("kennel_cats.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) + + + mux.HandleFunc("POST /kennel", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(hcaptcha.CaptchaVerificationContinuation, hcaptcha.CaptchaVerificationContinuation)(guestbook.SignGuestbookContinuation, FailurePassingContinuation)(guestbook.ListGuestbookContinuation, guestbook.ListGuestbookContinuation)(hcaptcha.CaptchaArgsContinuation, hcaptcha.CaptchaArgsContinuation)(template.TemplateContinuation("guestbook.html", true), template.TemplateContinuation("guestbook.html", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) + */ + + mux.HandleFunc("GET /{template}", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() - name := r.PathValue("name") - LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(IdContinuation, IdContinuation)(template.TemplateContinuation(name+".html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + templateFile := r.PathValue("template") + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(IdContinuation, IdContinuation)(template.TemplateContinuation(templateFile+".html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) return &http.Server{ diff --git a/database/kennel.go b/database/kennel.go new file mode 100644 index 0000000..39d664c --- /dev/null +++ b/database/kennel.go @@ -0,0 +1,137 @@ +package database + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "log" + "time" +) + +type KennelCat struct { + ID string `json:"id"` + UserID string `json:"user_id"` + Link string `json:"link"` + Description string `json:"description"` + Spritesheet string `json:"spritesheet"` + CreatedAt time.Time `json:"created_at"` +} + +type KennelState struct { + At time.Time `json:"at"` + EncodedState string `json:"state"` +} + +func CountUserKennelCats(db *sql.DB, userID string) (int, error) { + log.Println("counting kennel cats for user", userID) + + row := db.QueryRow("SELECT COUNT(*) FROM kennel_cat WHERE user_id = ?", userID) + var count int + err := row.Scan(&count) + if err != nil { + return 0, err + } + return count, nil +} + +func GetUserKennelCats(db *sql.DB, userID string) ([]KennelCat, error) { + log.Println("getting kennel cats for user", userID) + + rows, err := db.Query("SELECT * FROM kennel_cat WHERE user_id = ?", userID) + if err != nil { + return nil, err + } + defer rows.Close() + + var cats []KennelCat + for rows.Next() { + var cat KennelCat + err := rows.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + if err != nil { + return nil, err + } + cats = append(cats, cat) + } + + return cats, nil +} + +func SaveKennelCat(db *sql.DB, cat *KennelCat) (*KennelCat, error) { + log.Println("saving kennel cat", cat.ID) + + if (cat.CreatedAt == time.Time{}) { + cat.CreatedAt = time.Now() + } + + _, err := db.Exec("INSERT OR REPLACE INTO kennel_cat (id, user_id, link, description, spritesheet, created_at) VALUES (?, ?, ?, ?, ?, ?)", cat.ID, cat.UserID, cat.Link, cat.Description, cat.Spritesheet, cat.CreatedAt) + + if err != nil { + return nil, err + } + return cat, nil +} + +func GetKennelCat(db *sql.DB, catID string) (*KennelCat, error) { + log.Println("getting kennel cat", catID) + + row := db.QueryRow("SELECT * FROM kennel_cat WHERE id = ?", catID) + var cat KennelCat + err := row.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + if err != nil { + return nil, err + } + return &cat, nil +} + +func DeleteKennelCat(db *sql.DB, catID string) error { + log.Println("deleting kennel cat", catID) + + _, err := db.Exec("DELETE FROM kennel_cat WHERE id = ?", catID) + if err != nil { + return err + } + return nil +} + +func GetKennel(dbConn *sql.DB) ([]KennelCat, error) { + log.Println("getting kennel") + + rows, err := dbConn.Query("SELECT * FROM kennel_cat") + if err != nil { + return nil, err + } + defer rows.Close() + + var cats []KennelCat + for rows.Next() { + var cat KennelCat + err := rows.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + if err != nil { + return nil, err + } + cats = append(cats, cat) + } + + return cats, nil +} + +func GetKennelState(dbConn *sql.DB) (*KennelState, error) { + log.Println("getting kennel state") + + row := dbConn.QueryRow("SELECT * FROM kennel_state") + var state KennelState + err := row.Scan(&state.At, &state.EncodedState) + if err != nil { + return nil, err + } + return &state, nil +} + +func SaveKennelState(dbConn *sql.DB, state *KennelState) (*KennelState, error) { + log.Println("saving kennel state") + + _, err := dbConn.Exec("INSERT OR REPLACE INTO kennel_state (at, state) VALUES (?, ?)", state.At, state.EncodedState) + if err != nil { + return nil, err + } + return state, nil +} diff --git a/database/migrate.go b/database/migrate.go index e9e21b7..363f022 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -162,6 +162,33 @@ func MigrateProfiles(dbConn *sql.DB) (*sql.DB, error) { return dbConn, nil } +func MigrateKennel(dbConn *sql.DB) (*sql.DB, error) { + log.Println("migrating kennel tables") + + _, err := dbConn.Exec(`CREATE TABLE IF NOT EXISTS kennel_cat ( + id TEXT PRIMARY KEY, + user_id INTEGER NOT NULL, + link TEXT NOT NULL, + description TEXT NOT NULL, + spritesheet TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE + );`) + if err != nil { + return dbConn, err + } + + _, err = dbConn.Exec(`CREATE TABLE IF NOT EXISTS kennel_state ( + at LONG INTEGER PRIMARY KEY, + encoded_state TEXT NOT NULL + );`) + if err != nil { + return dbConn, err + } + + return dbConn, nil +} + func Migrate(dbConn *sql.DB) (*sql.DB, error) { log.Println("migrating database") @@ -173,6 +200,7 @@ func Migrate(dbConn *sql.DB) (*sql.DB, error) { MigrateDNSRecords, MigrateGuestBook, MigrateProfiles, + MigrateKennel, } for _, migration := range migrations { diff --git a/static/css/styles.css b/static/css/styles.css index 886052e..24a76b9 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -18,32 +18,6 @@ font-family: "ComicSans", sans-serif; cursor: url("/static/img/cursor-1.png"), auto; - -webkit-animation: cursor 400ms infinite; - animation: cursor 400ms infinite; -} - -@-webkit-keyframes cursor { - 0% { - cursor: url("/static/img/cursor-2.png"), auto; - } - 50% { - cursor: url("/static/img/cursor-1.png"), auto; - } - 100% { - cursor: url("/static/img/cursor-2.png"), auto; - } -} - -@keyframes cursor { - 0% { - cursor: url("/static/img/cursor-2.png"), auto; - } - 50% { - cursor: url("/static/img/cursor-1.png"), auto; - } - 100% { - cursor: url("/static/img/cursor-2.png"), auto; - } } body { diff --git a/templates/about.html b/templates/about.html index 3c4c265..9e96bfd 100644 --- a/templates/about.html +++ b/templates/about.html @@ -2,18 +2,16 @@

hatecomputers.club is a club for those that, well, hate computers!


-

we maintain a little homelab mesh for our own little computing community! as a member you'll have access to split-zone dns for our intranet as well as ownership to "*.USER.hatecomputers.club." for which you can do almost anything. we host, and plan to host, all kinds of stuff for each other :).

+

we maintain a little homelab mesh for our own little computing community! as a member you'll have access to split-zone dns for our intranet as well as ownership to "*.USER.hatecomputers.club." for which you can do almost anything. we host, and plan to host, all kinds of stuff for each other :)


you may align with us if you believe any (or all) of the following:


{{ end }} diff --git a/templates/kennel.html b/templates/kennel.html new file mode 100644 index 0000000..2f3f0a7 --- /dev/null +++ b/templates/kennel.html @@ -0,0 +1,81 @@ +{{ define "content" }} + + + + + + + + + + + {{ if (eq (len .DNSRecords) 0) }} + + + + {{ end }} + {{ range $record := .DNSRecords }} + + + + + + + + + + {{ end }} +
type.name.content.ttl.internal.created.delete.
no dns records found.
{{ $record.Type }}{{ $record.Name }}{{ $record.Content }}{{ $record.TTL }}{{ $record.Internal }}{{ $record.CreatedAt }} +
+ + +
+
+
+
+

add dns records.

+

note that the name must be a subdomain of {{ .User.Username }}

+
+ + + + + + + + + + +
+{{ end }} -- 2.40.1 From ba05cd52a61db42faa32721e894df01c7e74af46 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 15 Aug 2024 23:24:43 -0700 Subject: [PATCH 2/3] kennel POC --- adapters/files/files_adapter.go | 1 + adapters/files/filesystem/filesystem.go | 5 + api/kennel/kennel.go | 195 ++++++++++++++++++++++++ api/serve.go | 34 +++-- database/kennel.go | 25 ++- database/migrate.go | 1 + static/css/colors.css | 4 + static/css/styles.css | 101 ++++++++++++ static/css/table.css | 2 +- static/img/cat_spritesheets/default.gif | Bin 0 -> 6512 bytes templates/base.html | 2 + templates/dns.html | 2 +- templates/kennel.html | 81 ---------- templates/kennel_cats.html | 66 ++++++++ templates/kennel_enc.json | 1 + 15 files changed, 417 insertions(+), 103 deletions(-) create mode 100644 api/kennel/kennel.go create mode 100644 static/img/cat_spritesheets/default.gif delete mode 100644 templates/kennel.html create mode 100644 templates/kennel_cats.html create mode 100644 templates/kennel_enc.json diff --git a/adapters/files/files_adapter.go b/adapters/files/files_adapter.go index bf3ea5f..44853e1 100644 --- a/adapters/files/files_adapter.go +++ b/adapters/files/files_adapter.go @@ -4,5 +4,6 @@ import "io" type FilesAdapter interface { CreateFile(path string, content io.Reader) (string, error) + FileExists(path string) bool DeleteFile(path string) error } diff --git a/adapters/files/filesystem/filesystem.go b/adapters/files/filesystem/filesystem.go index 726a588..e7e671f 100644 --- a/adapters/files/filesystem/filesystem.go +++ b/adapters/files/filesystem/filesystem.go @@ -35,3 +35,8 @@ func (f *FilesystemAdapter) CreateFile(path string, content io.Reader) (string, func (f *FilesystemAdapter) DeleteFile(path string) error { return os.Remove(f.BasePath + path) } + +func (f *FilesystemAdapter) FileExists(path string) bool { + _, err := os.Stat(f.BasePath + path) + return err == nil +} diff --git a/api/kennel/kennel.go b/api/kennel/kennel.go new file mode 100644 index 0000000..60a898e --- /dev/null +++ b/api/kennel/kennel.go @@ -0,0 +1,195 @@ +package kennel + +import ( + "log" + "net/http" + "strconv" + "strings" + + "git.hatecomputers.club/hatecomputers/hatecomputers.club/adapters/files" + "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/types" + "git.hatecomputers.club/hatecomputers/hatecomputers.club/database" + "git.hatecomputers.club/hatecomputers/hatecomputers.club/utils" +) + +const MaxCatSize = 1024 * 100 // 60KB +const CatsPath = "cats/" +const CatsPrefix = "/uploads/cats/" +const DefaultCatSpritesheet = "/static/img/cat_spritesheets/default.gif" +const MaxUserCats = 15 + +func ListUserCatsContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, failure types.Continuation) types.ContinuationChain { + userID := context.User.ID + + cats, err := database.GetUserKennelCats(context.DBConn, userID) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + + (*context.TemplateData)["Cats"] = cats + return success(context, req, resp) + } +} + +func CreateCatContinuation(fileAdapter files.FilesAdapter, maxUserCats int, maxCatSize int, catsPath string, catsPrefix string, defaultCatSpritesheet string) func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, failure types.Continuation) types.ContinuationChain { + formErrors := types.BannerMessages{ + Messages: []string{}, + } + + numCats, err := database.CountUserKennelCats(context.DBConn, context.User.ID) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + if numCats >= maxUserCats { + formErrors.Messages = append(formErrors.Messages, "max cats reached for user") + } + + err = req.ParseMultipartForm(int64(maxCatSize)) + if err != nil { + formErrors.Messages = append(formErrors.Messages, "cat spritesheet too large") + } + + catID := utils.RandomId() + spritesheetPath := catsPrefix + catID + + if len(formErrors.Messages) == 0 { + file, _, err := req.FormFile("spritesheet") + if file != nil && err != nil { + formErrors.Messages = append(formErrors.Messages, "error uploading spritesheet") + } else if file != nil { + defer file.Close() + reader := http.MaxBytesReader(resp, file, int64(maxCatSize)) + defer reader.Close() + + _, err = fileAdapter.CreateFile(catsPath+catID, reader) + if err != nil { + log.Println(err) + formErrors.Messages = append(formErrors.Messages, "error saving spritesheet (is it too big?)") + } + } else if file == nil && err != nil { + spritesheetPath = defaultCatSpritesheet + } + } + + link := req.FormValue("link") + description := req.FormValue("description") + name := req.FormValue("name") + + cat := &database.KennelCat{ + ID: catID, + UserID: context.User.ID, + Name: name, + Link: link, + Description: description, + Spritesheet: spritesheetPath, + } + formErrors.Messages = append(formErrors.Messages, validateCat(cat)...) + if len(formErrors.Messages) == 0 { + _, err := database.SaveKennelCat(context.DBConn, cat) + if err != nil { + log.Println(err) + formErrors.Messages = append(formErrors.Messages, "failed to save cat") + } + } + + if len(formErrors.Messages) > 0 { + (*context.TemplateData)["Error"] = formErrors + (*context.TemplateData)["CatForm"] = cat + resp.WriteHeader(http.StatusBadRequest) + + return failure(context, req, resp) + } + + formSuccess := types.BannerMessages{ + Messages: []string{"cat added."}, + } + (*context.TemplateData)["Success"] = formSuccess + return success(context, req, resp) + } + } +} + +func RemoveCatContinuation(fileAdapter files.FilesAdapter, catsPath string) func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, failure types.Continuation) types.ContinuationChain { + catID := req.FormValue("id") + + cat, err := database.GetKennelCat(context.DBConn, catID) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + if cat == nil || cat.UserID != context.User.ID { + resp.WriteHeader(http.StatusUnauthorized) + return failure(context, req, resp) + } + + err = database.DeleteKennelCat(context.DBConn, catID) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + + err = fileAdapter.DeleteFile(catsPath + catID) + if err != nil && fileAdapter.FileExists(catsPath+catID) { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + + return success(context, req, resp) + } + } +} + +func GetCatStateAtContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, failure types.Continuation) types.ContinuationChain { + atLong, err := strconv.ParseInt(req.FormValue("at"), 10, 64) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusBadRequest) + return failure(context, req, resp) + } + + cats, err := database.GetKennelStateAt(context.DBConn, atLong) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + + (*context.TemplateData)["EncodedState"] = cats.EncodedState + return success(context, req, resp) + } +} + +func validateCat(cat *database.KennelCat) []string { + errors := []string{} + + if cat.Name == "" { + errors = append(errors, "name is required") + } + if cat.Link == "" { + errors = append(errors, "link is required") + } + if !strings.HasPrefix(cat.Link, "http://") && !strings.HasPrefix(cat.Link, "https://") { + errors = append(errors, "link must be a valid URL") + } + if cat.Description == "" { + errors = append(errors, "description is required") + } + if len(cat.Description) > 100 { + errors = append(errors, "description must be less than 100 characters") + } + + return errors +} diff --git a/api/serve.go b/api/serve.go index 2078cdc..2f08cf9 100644 --- a/api/serve.go +++ b/api/serve.go @@ -13,6 +13,7 @@ import ( "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/dns" "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/guestbook" "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/hcaptcha" + "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/kennel" "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/keys" "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/profiles" "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/template" @@ -172,23 +173,28 @@ func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server { LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(hcaptcha.CaptchaVerificationContinuation, hcaptcha.CaptchaVerificationContinuation)(guestbook.SignGuestbookContinuation, FailurePassingContinuation)(guestbook.ListGuestbookContinuation, guestbook.ListGuestbookContinuation)(hcaptcha.CaptchaArgsContinuation, hcaptcha.CaptchaArgsContinuation)(template.TemplateContinuation("guestbook.html", true), template.TemplateContinuation("guestbook.html", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) - /* - mux.HandleFunc("GET /kennel", func(w http.ResponseWriter, r *http.Request) { - requestContext := makeRequestContext() - LogRequestContinuation(requestContext, r, w)(kennel.GetKennelStateContinuation, kennel.GetKennelStateContinuation)(template.TemplateContinuation("kennel.json", true), template.TemplateContinuation("kennel.json", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) - }) + mux.HandleFunc("GET /kennel", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(kennel.GetCatStateAtContinuation, FailurePassingContinuation)(template.TemplateContinuation("kennel_enc.json", false), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) - mux.HandleFunc("GET /kennel/cats", func(w http.ResponseWriter, r *http.Request) { - requestContext := makeRequestContext() - LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(kennel.ListUserCats, auth.GoLoginContinuation)(template.TemplateContinuation("kennel_cats.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) - }) + mux.HandleFunc("GET /kennel/cats", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(kennel.ListUserCatsContinuation, auth.GoLoginContinuation)(template.TemplateContinuation("kennel_cats.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) + mux.HandleFunc("POST /kennel/cats", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + createCatContinuation := kennel.CreateCatContinuation(uploadAdapter, kennel.MaxUserCats, kennel.MaxCatSize, kennel.CatsPath, kennel.CatsPrefix, kennel.DefaultCatSpritesheet) + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(createCatContinuation, FailurePassingContinuation)(kennel.ListUserCatsContinuation, kennel.ListUserCatsContinuation)(template.TemplateContinuation("kennel_cats.html", true), template.TemplateContinuation("kennel_cats.html", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) - mux.HandleFunc("POST /kennel", func(w http.ResponseWriter, r *http.Request) { - requestContext := makeRequestContext() - LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(hcaptcha.CaptchaVerificationContinuation, hcaptcha.CaptchaVerificationContinuation)(guestbook.SignGuestbookContinuation, FailurePassingContinuation)(guestbook.ListGuestbookContinuation, guestbook.ListGuestbookContinuation)(hcaptcha.CaptchaArgsContinuation, hcaptcha.CaptchaArgsContinuation)(template.TemplateContinuation("guestbook.html", true), template.TemplateContinuation("guestbook.html", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) - }) - */ + mux.HandleFunc("POST /kennel/cats/delete", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + deleteCatContinuation := kennel.RemoveCatContinuation(uploadAdapter, kennel.CatsPath) + + LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(deleteCatContinuation, FailurePassingContinuation)(kennel.ListUserCatsContinuation, FailurePassingContinuation)(template.TemplateContinuation("kennel_cats.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) mux.HandleFunc("GET /{template}", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() diff --git a/database/kennel.go b/database/kennel.go index 39d664c..7e9a557 100644 --- a/database/kennel.go +++ b/database/kennel.go @@ -10,6 +10,7 @@ import ( type KennelCat struct { ID string `json:"id"` UserID string `json:"user_id"` + Name string `json:"name"` Link string `json:"link"` Description string `json:"description"` Spritesheet string `json:"spritesheet"` @@ -45,7 +46,7 @@ func GetUserKennelCats(db *sql.DB, userID string) ([]KennelCat, error) { var cats []KennelCat for rows.Next() { var cat KennelCat - err := rows.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + err := rows.Scan(&cat.ID, &cat.Name, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) if err != nil { return nil, err } @@ -62,7 +63,7 @@ func SaveKennelCat(db *sql.DB, cat *KennelCat) (*KennelCat, error) { cat.CreatedAt = time.Now() } - _, err := db.Exec("INSERT OR REPLACE INTO kennel_cat (id, user_id, link, description, spritesheet, created_at) VALUES (?, ?, ?, ?, ?, ?)", cat.ID, cat.UserID, cat.Link, cat.Description, cat.Spritesheet, cat.CreatedAt) + _, err := db.Exec("INSERT OR REPLACE INTO kennel_cat (id, user_id, name, link, description, spritesheet, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)", cat.ID, cat.UserID, cat.Name, cat.Link, cat.Description, cat.Spritesheet, cat.CreatedAt) if err != nil { return nil, err @@ -75,7 +76,7 @@ func GetKennelCat(db *sql.DB, catID string) (*KennelCat, error) { row := db.QueryRow("SELECT * FROM kennel_cat WHERE id = ?", catID) var cat KennelCat - err := row.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + err := row.Scan(&cat.ID, &cat.Name, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) if err != nil { return nil, err } @@ -104,7 +105,7 @@ func GetKennel(dbConn *sql.DB) ([]KennelCat, error) { var cats []KennelCat for rows.Next() { var cat KennelCat - err := rows.Scan(&cat.ID, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + err := rows.Scan(&cat.ID, &cat.Name, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) if err != nil { return nil, err } @@ -114,10 +115,22 @@ func GetKennel(dbConn *sql.DB) ([]KennelCat, error) { return cats, nil } -func GetKennelState(dbConn *sql.DB) (*KennelState, error) { +func GetLatestKennelState(dbConn *sql.DB) (*KennelState, error) { log.Println("getting kennel state") - row := dbConn.QueryRow("SELECT * FROM kennel_state") + row := dbConn.QueryRow("SELECT * FROM kennel_state ORDER BY at DESC LIMIT 1") + var state KennelState + err := row.Scan(&state.At, &state.EncodedState) + if err != nil { + return nil, err + } + return &state, nil +} + +func GetKennelStateAt(dbConn *sql.DB, at int64) (*KennelState, error) { + log.Println("getting kennel state at", at) + + row := dbConn.QueryRow("SELECT * FROM kennel_state WHERE at = ?", at) var state KennelState err := row.Scan(&state.At, &state.EncodedState) if err != nil { diff --git a/database/migrate.go b/database/migrate.go index 363f022..14766b6 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -167,6 +167,7 @@ func MigrateKennel(dbConn *sql.DB) (*sql.DB, error) { _, err := dbConn.Exec(`CREATE TABLE IF NOT EXISTS kennel_cat ( id TEXT PRIMARY KEY, + name TEXT NOT NULL, user_id INTEGER NOT NULL, link TEXT NOT NULL, description TEXT NOT NULL, diff --git a/static/css/colors.css b/static/css/colors.css index 46357d9..69b97db 100644 --- a/static/css/colors.css +++ b/static/css/colors.css @@ -7,6 +7,7 @@ --container-bg-light: #fff7f87a; --border-color-light: #692fcc; --error-color-light: #a83254; + --tr-color-light: #8bcefa; --background-color-dark: #333; --background-color-dark-2: #2c2c2c; @@ -16,6 +17,7 @@ --container-bg-dark: #424242ea; --border-color-dark: #956ade; --error-color-dark: #851736; + --tr-color-dark: #212a6a; } [data-theme="DARK"] { @@ -27,6 +29,7 @@ --border-color: var(--border-color-dark); --error-color: var(--error-color-dark); --confirm-color: var(--confirm-color-dark); + --tr-color: var(--tr-color-dark); } [data-theme="LIGHT"] { @@ -38,6 +41,7 @@ --border-color: var(--border-color-light); --error-color: var(--error-color-light); --confirm-color: var(--confirm-color-light); + --tr-color: var(--tr-color-light); } .error { diff --git a/static/css/styles.css b/static/css/styles.css index 24a76b9..9c82d6a 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -68,3 +68,104 @@ hr { .info:hover { opacity: 0.8; } +@import "/static/css/colors.css"; +@import "/static/css/blinky.css"; +@import "/static/css/table.css"; +@import "/static/css/form.css"; +@import "/static/css/guestbook.css"; +@import "/static/css/club.css"; + +@font-face { + font-family: "ComicSans"; + src: url("/static/font/comicsans.ttf"); +} + +* { + box-sizing: border-box; + margin: 0; + padding: 0; + color: var(--text-color); + font-family: "ComicSans", sans-serif; +} + +/* i just cannot get this to look good on firefox... */ +@supports not (-moz-appearance: none) { + * { + cursor: url("/static/img/cursor-2.png"), auto; + -webkit-animation: cursor 400ms infinite; + animation: cursor 400ms infinite; + } + + @-webkit-keyframes cursor { + 0% { + cursor: url("/static/img/cursor-2.png"), auto; + } + 50% { + cursor: url("/static/img/cursor-1.png"), auto; + } + 100% { + cursor: url("/static/img/cursor-2.png"), auto; + } + } + + @keyframes cursor { + 0% { + cursor: url("/static/img/cursor-2.png"), auto; + } + 50% { + cursor: url("/static/img/cursor-1.png"), auto; + } + 100% { + cursor: url("/static/img/cursor-2.png"), auto; + } + } +} + +body { + background-color: var(--background-color); + background-image: url("/static/img/stars.gif"); + min-height: 100vh; +} + +a { + color: var(--link-color); + text-decoration: none; + font-weight: bold; +} + +a:hover { + text-decoration: underline; +} + +.container { + max-width: 1600px; + margin: auto; + background-color: var(--container-bg); + padding: 1rem; +} + +hr { + border: 0; + border-top: 1px solid var(--text-color); + + margin: 20px 0; +} + +.blinkies { + display: flex; + justify-content: left; + flex-wrap: wrap; + max-width: 900px; + gap: 10px 10px; +} + +.info { + margin-bottom: 1rem; + max-width: 600px; + + transition: opacity 0.3s; +} + +.info:hover { + opacity: 0.8; +} diff --git a/static/css/table.css b/static/css/table.css index 75a961d..854f591 100644 --- a/static/css/table.css +++ b/static/css/table.css @@ -26,6 +26,6 @@ tbody tr { } tbody tr:hover { - background-color: #ff47daa0; + background-color: var(--tr-color); color: #2a2a2a; } diff --git a/static/img/cat_spritesheets/default.gif b/static/img/cat_spritesheets/default.gif new file mode 100644 index 0000000000000000000000000000000000000000..0d264d87d0b9106914df8d1475d80b978a87e609 GIT binary patch literal 6512 zcmV-$8IR^iNk%w1VE_Su0Q3L=0000B2nReS8CE_mT|YZ>1#@drNo`h2fNETdd1;u4 zdB=)z+N+QMvj6tn$@JjT_wC;G>+Apd@c;bq^#A_=0000000000000000000000000 z000000000000000A^8LW00000EC2ui0004i000I5ARvw;5S8SKu59bRa4gSsZQppl z3c`J=qn~a_EEb1q{1l9Gi0 zT|Ip+IM4|b%a(W+1fXD)!by3k6v=SL?4(JnbAtM`0-*p*jU@yGcp^~>zEg)5?o0%M zf!amc8YLw~p&x{I<+PHhsWgCHyjjI?eEWn!RjerQ(wspm;(~;R8;=c}R-)Up!#YX> z%hv6t$&`g5m^76rV53h7t2w{`aaNXs58+`z6q8qrt|pM0Nw&3Bi=-yF^;wjMWoE%n zO&~-#2{q3543QQAdM~-rZ&&y8YBI`neMGnCwK{$9D%zbQk5k;drrpHxhVLLyye8F3 zRLjY=S4O8ThMI6h(QZ?{ z1W!Deq{Kr%RxDFUVo)`A#EJg>0SrQ}OhF)bu7Sf?gc}}WmM4Tcw&8mS;6n#GG?3zq zUxuCN8FT58o43q_dB>`rI;fuU*#6})hZkY`n1d5nYkYe%JPP*+AvlMm>(mXl6+2Gv%0JG!hUr zP~c`Mv6)8kQ~?B1a%N7IwxDYWmxmyL(?3>>HV;k>FgN2MyZH3pJbBI2DLHOMyGU){jJ98hSuRlOGlL+w zNEzaiDj-pxEsQHEHYnYqennUE`sa-9f${zy>{r8RpAFpJIn zf%QOlzGv~KVfFV7S2mk$6IB);e6~J&i>#*AX;|?#1X|w>_`<^2OfbDr+jYsvB}VEq zl_*b5X(#Yvf^VV>L@mbuwRYd_c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1W|mD?w1U^QFLfvuS=8DPLq00s0^EpOV-n!M6} zfd6-(1vA!|vUX385(GDpDO*;g8G&bCtbq}OkD3k~inI|D)CUeg7BmL`jzhuepvZ)= zWZCja@Kyi@{$%VMp=bw#mJV0^b2U@POinvTiYs`M*g1s;4j@Cx2nZ5GkZ{~?hgI@*A(#m5D-Gc;PA=-zkn(v>hBnbjc(>nU6!+O*V%xV8PJ_*3!25j{>E3 z$ny7R@fIKXhIiE+4EvkI+o2eV4IFcO^y)vW|7#jCPCYbq*C{gX^Z;KL0@X!bfn@n1 zT|ny*Ql3Wxq9+PBesD8XZ802}!W#buRdjidF(zIFu28fR>Ft(f-;WK5u}Q}K&B!MFz(mKVx0^ZRtzVI zrC))=A$496sHG)Si>u9W$@j0028r!J^;* literal 0 HcmV?d00001 diff --git a/templates/base.html b/templates/base.html index 036a748..7486f45 100644 --- a/templates/base.html +++ b/templates/base.html @@ -35,6 +35,8 @@ | api keys. | + kennel. + | {{ .User.DisplayName }}. | logout. diff --git a/templates/dns.html b/templates/dns.html index 2f3f0a7..e04cbfa 100644 --- a/templates/dns.html +++ b/templates/dns.html @@ -37,7 +37,7 @@

note that the name must be a subdomain of {{ .User.Username }}


- - - type. - name. - content. - ttl. - internal. - created. - delete. - - {{ if (eq (len .DNSRecords) 0) }} - - no dns records found. - - {{ end }} - {{ range $record := .DNSRecords }} - - {{ $record.Type }} - {{ $record.Name }} - {{ $record.Content }} - {{ $record.TTL }} - {{ $record.Internal }} - {{ $record.CreatedAt }} - -
- - -
- - - {{ end }} - -
-
-

add dns records.

-

note that the name must be a subdomain of {{ .User.Username }}

-
- - - - - - - - - - -
-{{ end }} diff --git a/templates/kennel_cats.html b/templates/kennel_cats.html new file mode 100644 index 0000000..ce31738 --- /dev/null +++ b/templates/kennel_cats.html @@ -0,0 +1,66 @@ +{{ define "content" }} + + + + + + + + + + {{ if (eq (len .Cats) 0) }} + + + + {{ end }} + {{ range $cat := .Cats }} + + + + + + + + + {{ end }} +
name.link.description.spritesheet.created at.remove.
no cats found
{{ $cat.Name }}{{ $cat.Link }}{{ $cat.Description }}{{ $cat.CreatedAt }} +
+ + +
+
+
+
+

add cat.

+
+ + + + + + + + +
if not specified, will use the default. check it out for the format we expect. max 50KB.
+ + + +
+{{ end }} diff --git a/templates/kennel_enc.json b/templates/kennel_enc.json new file mode 100644 index 0000000..602dad1 --- /dev/null +++ b/templates/kennel_enc.json @@ -0,0 +1 @@ +{{ define "content" }}{{ .EncodedState }}{{ end }} -- 2.40.1 From 67bf0199e1f0ed7dacda51ef624d6f548eec61ae Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 17 Aug 2024 15:23:22 -0700 Subject: [PATCH 3/3] make it simpler and add Ord --- api/kennel/kennel.go | 57 +++++++++++++++++++++++++---- api/serve.go | 9 ++++- database/kennel.go | 77 ++++++++++++++++++++++----------------- database/migrate.go | 8 ---- templates/kennel_enc.json | 1 - 5 files changed, 100 insertions(+), 52 deletions(-) delete mode 100644 templates/kennel_enc.json diff --git a/api/kennel/kennel.go b/api/kennel/kennel.go index 60a898e..a68388d 100644 --- a/api/kennel/kennel.go +++ b/api/kennel/kennel.go @@ -1,9 +1,9 @@ package kennel import ( + "encoding/json" "log" "net/http" - "strconv" "strings" "git.hatecomputers.club/hatecomputers/hatecomputers.club/adapters/files" @@ -151,23 +151,66 @@ func RemoveCatContinuation(fileAdapter files.FilesAdapter, catsPath string) func } } -func GetCatStateAtContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { +func RingContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { return func(success types.Continuation, failure types.Continuation) types.ContinuationChain { - atLong, err := strconv.ParseInt(req.FormValue("at"), 10, 64) - if err != nil { - log.Println(err) + order := req.URL.Query().Get("order") + + if order == "random" { + kennelCat, err := database.GetRandomKennelCat(context.DBConn) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + http.Redirect(resp, req, kennelCat.Link, http.StatusFound) + return success(context, req, resp) + } + + id := req.URL.Query().Get("id") + if id == "" { resp.WriteHeader(http.StatusBadRequest) return failure(context, req, resp) } + if order != "random" && order != "next" && order != "prev" { + kennelCat, err := database.GetKennelCat(context.DBConn, id) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusNotFound) + return failure(context, req, resp) + } + http.Redirect(resp, req, kennelCat.Link, http.StatusFound) + return success(context, req, resp) + } - cats, err := database.GetKennelStateAt(context.DBConn, atLong) + nextCat, err := database.GetNextKennelCat(context.DBConn, id, order == "next") if err != nil { log.Println(err) resp.WriteHeader(http.StatusInternalServerError) return failure(context, req, resp) } - (*context.TemplateData)["EncodedState"] = cats.EncodedState + http.Redirect(resp, req, nextCat.Link, http.StatusFound) + return success(context, req, resp) + } +} + +func GetKennelContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, failure types.Continuation) types.ContinuationChain { + cats, err := database.GetKennel(context.DBConn) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + json, err := json.Marshal(cats) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + + resp.Header().Set("Content-Type", "application/json") + resp.Write(json) return success(context, req, resp) } } diff --git a/api/serve.go b/api/serve.go index 2f08cf9..e205ce5 100644 --- a/api/serve.go +++ b/api/serve.go @@ -173,9 +173,14 @@ func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server { LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(hcaptcha.CaptchaVerificationContinuation, hcaptcha.CaptchaVerificationContinuation)(guestbook.SignGuestbookContinuation, FailurePassingContinuation)(guestbook.ListGuestbookContinuation, guestbook.ListGuestbookContinuation)(hcaptcha.CaptchaArgsContinuation, hcaptcha.CaptchaArgsContinuation)(template.TemplateContinuation("guestbook.html", true), template.TemplateContinuation("guestbook.html", true))(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) - mux.HandleFunc("GET /kennel", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /kennel/", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() - LogRequestContinuation(requestContext, r, w)(auth.VerifySessionContinuation, FailurePassingContinuation)(kennel.GetCatStateAtContinuation, FailurePassingContinuation)(template.TemplateContinuation("kennel_enc.json", false), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + LogRequestContinuation(requestContext, r, w)(kennel.GetKennelContinuation, FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) + + mux.HandleFunc("GET /kennel/cat", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(kennel.RingContinuation, FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) mux.HandleFunc("GET /kennel/cats", func(w http.ResponseWriter, r *http.Request) { diff --git a/database/kennel.go b/database/kennel.go index 7e9a557..91525db 100644 --- a/database/kennel.go +++ b/database/kennel.go @@ -93,6 +93,49 @@ func DeleteKennelCat(db *sql.DB, catID string) error { return nil } +func GetRandomKennelCat(dbConn *sql.DB) (*KennelCat, error) { + log.Println("getting random kennel cat") + + row := dbConn.QueryRow("SELECT * FROM kennel_cat ORDER BY RANDOM() LIMIT 1") + var cat KennelCat + err := row.Scan(&cat.ID, &cat.Name, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + if err != nil { + return nil, err + } + return &cat, nil +} + +func GetNextKennelCat(dbConn *sql.DB, lastID string, next bool) (*KennelCat, error) { + log.Println("getting next kennel cat") + + operation := ">" + sorted := "ASC" + if !next { + operation = "<" + sorted = "DESC" + } + + row := dbConn.QueryRow("SELECT * FROM kennel_cat WHERE id "+operation+" ? ORDER BY id "+sorted+" LIMIT 1", lastID) + + var cat KennelCat + err := row.Scan(&cat.ID, &cat.Name, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + if err != nil { + if next { + // loop "back" to the first in the ring + row = dbConn.QueryRow("SELECT * FROM kennel_cat ORDER BY id ASC LIMIT 1") + } else { + // loop "forward" to the first in the ring + row = dbConn.QueryRow("SELECT * FROM kennel_cat ORDER BY id DESC LIMIT 1") + } + err = row.Scan(&cat.ID, &cat.Name, &cat.UserID, &cat.Link, &cat.Description, &cat.Spritesheet, &cat.CreatedAt) + } + if err != nil { + return nil, err + } + + return &cat, nil +} + func GetKennel(dbConn *sql.DB) ([]KennelCat, error) { log.Println("getting kennel") @@ -114,37 +157,3 @@ func GetKennel(dbConn *sql.DB) ([]KennelCat, error) { return cats, nil } - -func GetLatestKennelState(dbConn *sql.DB) (*KennelState, error) { - log.Println("getting kennel state") - - row := dbConn.QueryRow("SELECT * FROM kennel_state ORDER BY at DESC LIMIT 1") - var state KennelState - err := row.Scan(&state.At, &state.EncodedState) - if err != nil { - return nil, err - } - return &state, nil -} - -func GetKennelStateAt(dbConn *sql.DB, at int64) (*KennelState, error) { - log.Println("getting kennel state at", at) - - row := dbConn.QueryRow("SELECT * FROM kennel_state WHERE at = ?", at) - var state KennelState - err := row.Scan(&state.At, &state.EncodedState) - if err != nil { - return nil, err - } - return &state, nil -} - -func SaveKennelState(dbConn *sql.DB, state *KennelState) (*KennelState, error) { - log.Println("saving kennel state") - - _, err := dbConn.Exec("INSERT OR REPLACE INTO kennel_state (at, state) VALUES (?, ?)", state.At, state.EncodedState) - if err != nil { - return nil, err - } - return state, nil -} diff --git a/database/migrate.go b/database/migrate.go index 14766b6..0c8318c 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -179,14 +179,6 @@ func MigrateKennel(dbConn *sql.DB) (*sql.DB, error) { return dbConn, err } - _, err = dbConn.Exec(`CREATE TABLE IF NOT EXISTS kennel_state ( - at LONG INTEGER PRIMARY KEY, - encoded_state TEXT NOT NULL - );`) - if err != nil { - return dbConn, err - } - return dbConn, nil } diff --git a/templates/kennel_enc.json b/templates/kennel_enc.json deleted file mode 100644 index 602dad1..0000000 --- a/templates/kennel_enc.json +++ /dev/null @@ -1 +0,0 @@ -{{ define "content" }}{{ .EncodedState }}{{ end }} -- 2.40.1