Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
4.9 KiB
Go
180 lines
4.9 KiB
Go
package web
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"supermarket/examples"
|
|
"supermarket/internal/inventory"
|
|
"supermarket/internal/profile"
|
|
)
|
|
|
|
func (s *Server) inventoryList(w http.ResponseWriter, r *http.Request) {
|
|
rel := r.URL.Query().Get("relationship")
|
|
entries, err := inventory.List(s.DB, rel)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
s.render(w, r, "inventory", page{Title: "Inventory", Nav: "inventory", Data: map[string]any{
|
|
"Entries": entries,
|
|
"Relationship": rel,
|
|
"Relationships": inventory.Relationships,
|
|
}})
|
|
}
|
|
|
|
type entryData struct {
|
|
E *inventory.Entry
|
|
Relationships []string
|
|
}
|
|
|
|
func (s *Server) inventoryEdit(w http.ResponseWriter, r *http.Request) {
|
|
e := &inventory.Entry{Relationship: inventory.Partner}
|
|
if idStr := r.PathValue("id"); idStr != "" {
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
e, err = inventory.Get(s.DB, id)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
if e == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
}
|
|
title := "New entry"
|
|
if e.ID != 0 {
|
|
title = e.Name
|
|
}
|
|
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Data: entryData{E: e, Relationships: inventory.Relationships}})
|
|
}
|
|
|
|
func (s *Server) inventorySave(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
f := r.PostForm
|
|
id, _ := strconv.ParseInt(f.Get("id"), 10, 64)
|
|
e := &inventory.Entry{
|
|
ID: id,
|
|
Name: f.Get("name"),
|
|
Relationship: f.Get("relationship"),
|
|
Description: f.Get("description"),
|
|
Websites: profile.Lines(f.Get("websites")),
|
|
TwitterHandle: f.Get("twitter_handle"),
|
|
YouTubeChannelID: f.Get("youtube_channel_id"),
|
|
InstagramHandle: f.Get("instagram_handle"),
|
|
LinkedIn: f.Get("linkedin"),
|
|
Subreddits: profile.Lines(f.Get("subreddits")),
|
|
RSS: profile.Lines(f.Get("rss")),
|
|
Hashtags: strings.Fields(strings.ReplaceAll(f.Get("hashtags"), ",", " ")),
|
|
Keywords: profile.Lines(f.Get("keywords")),
|
|
TalkingPoints: f.Get("talking_points"),
|
|
MentionPolicy: f.Get("mention_policy"),
|
|
}
|
|
if err := inventory.Save(s.DB, e); err != nil {
|
|
title := e.Name
|
|
if title == "" {
|
|
title = "New entry"
|
|
}
|
|
s.render(w, r, "entry", page{Title: title, Nav: "inventory", Error: err.Error(),
|
|
Data: entryData{E: e, Relationships: inventory.Relationships}})
|
|
return
|
|
}
|
|
redirect(w, r, "/inventory", "saved")
|
|
}
|
|
|
|
func (s *Server) inventoryDelete(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if err := inventory.Delete(s.DB, id); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
redirect(w, r, "/inventory", "deleted")
|
|
}
|
|
|
|
func (s *Server) inventoryImportPage(w http.ResponseWriter, r *http.Request) {
|
|
s.render(w, r, "import", page{Title: "Import inventory", Nav: "inventory", Data: map[string]any{
|
|
"Relationships": inventory.Relationships,
|
|
}})
|
|
}
|
|
|
|
const maxImportBytes = 4 << 20
|
|
|
|
func (s *Server) inventoryImport(w http.ResponseWriter, r *http.Request) {
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxImportBytes+64<<10)
|
|
if err := r.ParseMultipartForm(maxImportBytes); err != nil && !errors.Is(err, http.ErrNotMultipart) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
rel := r.FormValue("relationship")
|
|
overwrite := r.FormValue("overwrite") == "on"
|
|
show := func(errMsg string, res *inventory.ImportResult) {
|
|
s.render(w, r, "import", page{Title: "Import inventory", Nav: "inventory", Error: errMsg, Data: map[string]any{
|
|
"Relationships": inventory.Relationships,
|
|
"Result": res,
|
|
"YAML": r.FormValue("yaml"),
|
|
}})
|
|
}
|
|
|
|
var data []byte
|
|
switch {
|
|
case r.FormValue("example") == "federated":
|
|
data = examples.FederatedInventory
|
|
case r.MultipartForm != nil && len(r.MultipartForm.File["file"]) > 0:
|
|
f, err := r.MultipartForm.File["file"][0].Open()
|
|
if err != nil {
|
|
show(err.Error(), nil)
|
|
return
|
|
}
|
|
data, err = io.ReadAll(io.LimitReader(f, maxImportBytes))
|
|
f.Close()
|
|
if err != nil {
|
|
show(err.Error(), nil)
|
|
return
|
|
}
|
|
default:
|
|
data = []byte(r.FormValue("yaml"))
|
|
}
|
|
if len(strings.TrimSpace(string(data))) == 0 {
|
|
show("Paste a list, choose a file, or load the example.", nil)
|
|
return
|
|
}
|
|
|
|
entries, err := inventory.ParseYAML(data, rel)
|
|
if err != nil {
|
|
show(err.Error(), nil)
|
|
return
|
|
}
|
|
res := inventory.Import(s.DB, entries, overwrite)
|
|
show("", &res)
|
|
}
|
|
|
|
func (s *Server) inventoryExport(w http.ResponseWriter, r *http.Request) {
|
|
entries, err := inventory.List(s.DB, "")
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
data, err := inventory.ExportYAML(entries)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/yaml")
|
|
w.Header().Set("Content-Disposition", `attachment; filename="supermarket-inventory.yaml"`)
|
|
w.Write(data)
|
|
}
|