diff --git a/Dockerfile b/Dockerfile index 20d0991..c8c2f37 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM golang:1.22-alpine WORKDIR /src -RUN apk add --no-cache git +RUN apk add git COPY go.mod go.sum ./ RUN go mod download diff --git a/README.md b/README.md index ee42029..0a212ce 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -pageswiki +# pageswiki + +Simplest and powerful wiki architecture for personal diff --git a/deploy/nginx.conf b/deploy/nginx.conf index 50f533e..31e2088 100644 --- a/deploy/nginx.conf +++ b/deploy/nginx.conf @@ -2,7 +2,7 @@ server { listen 80; location / { - proxy_pass http://app:81; + proxy_pass http://app:3000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } diff --git a/docker-compose.yml b/docker-compose.yml index 9fdb133..772245e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,6 @@ services: nginx: + container_name: pageswiki-proxy image: nginx:1.27-alpine restart: unless-stopped ports: @@ -10,6 +11,7 @@ services: - app app: + container_name: pageswiki build: . volumes: - ./data:/data diff --git a/internal/server/gc_api.go b/internal/server/gc_api.go new file mode 100644 index 0000000..db9426c --- /dev/null +++ b/internal/server/gc_api.go @@ -0,0 +1,118 @@ +package server + +import ( + "encoding/json" + "net/http" + "strconv" +) + +func (s *Server) handleZombieListAPI( + w http.ResponseWriter, + r *http.Request, +) { + + graceSeconds := int64(86400) + + if v := r.URL.Query().Get("grace_seconds"); v != "" { + + n, err := + strconv.ParseInt( + v, + 10, + 64, + ) + + if err == nil { + graceSeconds = n + } + } + + zombies, err := + s.store.ListZombieObjects( + graceSeconds, + ) + + if err != nil { + + http.Error( + w, + err.Error(), + 500, + ) + + return + } + + w.Header().Set( + "Content-Type", + "application/json", + ) + + json.NewEncoder(w).Encode( + map[string]any{ + "zombies": zombies, + "count": len(zombies), + }, + ) +} + +func (s *Server) handleZombiePurgeAPI( + w http.ResponseWriter, + r *http.Request, +) { + + graceSeconds := int64(86400) + limit := 100 + + if v := r.URL.Query().Get("grace_seconds"); v != "" { + + n, err := + strconv.ParseInt( + v, + 10, + 64, + ) + + if err == nil { + graceSeconds = n + } + } + + if v := r.URL.Query().Get("limit"); v != "" { + + n, err := + strconv.Atoi(v) + + if err == nil { + limit = n + } + } + + deleted, err := + s.store.PurgeZombieObjects( + graceSeconds, + limit, + ) + + if err != nil { + + http.Error( + w, + err.Error(), + 500, + ) + + return + } + + w.Header().Set( + "Content-Type", + "application/json", + ) + + json.NewEncoder(w).Encode( + map[string]any{ + "deleted": deleted, + }, + ) +} diff --git a/internal/server/gc_worker.go b/internal/server/gc_worker.go new file mode 100644 index 0000000..8bcdca4 --- /dev/null +++ b/internal/server/gc_worker.go @@ -0,0 +1,45 @@ +package server + +import ( + "log" + "time" +) + +func (s *Server) startGCWorker() { + + go func() { + + ticker := + time.NewTicker( + 6 * time.Hour, + ) + + defer ticker.Stop() + + for { + + deleted, err := + s.store.PurgeZombieObjects( + 24*60*60, + 100, + ) + + if err != nil { + + log.Printf( + "zombie gc error: %v", + err, + ) + + } else if deleted > 0 { + + log.Printf( + "zombie gc purged=%d", + deleted, + ) + } + + <-ticker.C + } + }() +} diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 4cc2628..b681daa 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -2,22 +2,17 @@ package server import ( "net/http" - "strings" ) -type HomeObject struct { - UUID string - Path string - Kind string -} - func (s *Server) handleHome( w http.ResponseWriter, r *http.Request, ) { objects, err := - s.store.ListObjects() + s.store.ListRecentObjects( + 30, + ) if err != nil { @@ -30,46 +25,11 @@ func (s *Server) handleHome( return } - var semanticObjects []HomeObject - - for _, o := range objects { - - path := o.UUID - - if path == "" { - continue - } - - if strings.HasPrefix( - path, - "/system/", - ) { - continue - } - - if strings.HasPrefix( - path, - "/o/", - ) { - continue - } - - semanticObjects = append( - semanticObjects, - HomeObject{ - UUID: o.UUID, - Path: path, - Kind: o.Kind, - }, - ) - } - s.render( w, "home.gohtml", map[string]any{ - "Title": "Pages Wiki", - "Objects": semanticObjects, + "Objects": objects, }, ) } diff --git a/internal/server/object_delete_api.go b/internal/server/object_delete_api.go new file mode 100644 index 0000000..958a12f --- /dev/null +++ b/internal/server/object_delete_api.go @@ -0,0 +1,41 @@ +package server + +import ( + "encoding/json" + "net/http" +) + +func (s *Server) handleDeleteObjectAPI( + w http.ResponseWriter, + r *http.Request, +) { + + var req struct { + ObjectUUID string `json:"object_uuid"` + } + + err := json.NewDecoder( + r.Body, + ).Decode(&req) + + if err != nil { + http.Error(w, err.Error(), 400) + return + } + + if req.ObjectUUID == "" { + http.Error(w, "object_uuid is required", 400) + return + } + + err = s.store.DeleteObject( + req.ObjectUUID, + ) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.WriteHeader(http.StatusNoContent) +} diff --git a/internal/server/object_uri_handlers.go b/internal/server/object_uri_handlers.go new file mode 100644 index 0000000..501651c --- /dev/null +++ b/internal/server/object_uri_handlers.go @@ -0,0 +1,137 @@ +package server + +import ( + "html/template" + "net/http" + "strings" +) + +func objectUUIDFromURI(uri string) (string, bool) { + + if !strings.HasPrefix(uri, "uuid:") { + return "", false + } + + uuid := strings.TrimPrefix(uri, "uuid:") + + if uuid == "" { + return "", false + } + + return uuid, true +} + +func (s *Server) handleObjectView( + w http.ResponseWriter, + r *http.Request, +) { + + uri := strings.TrimPrefix(r.URL.Path, "/o/") + + objectUUID, ok := objectUUIDFromURI(uri) + + if !ok { + http.NotFound(w, r) + return + } + + latest, err := + s.store.GetLatestRevision(objectUUID) + + if err != nil { + + http.Error( + w, + err.Error(), + 500, + ) + + return + } + + s.render( + w, + "object_view.gohtml", + map[string]any{ + "Title": "Object View", + "ObjectUUID": objectUUID, + "ObjectURI": uri, + "RenderedHTML": template.HTML(latest.Body), + }, + ) +} + +func (s *Server) handleObjectEdit( + w http.ResponseWriter, + r *http.Request, +) { + + uri := strings.TrimPrefix(r.URL.Path, "/o/e/") + + objectUUID, ok := objectUUIDFromURI(uri) + + if !ok { + http.NotFound(w, r) + return + } + + latest, err := + s.store.GetLatestRevision(objectUUID) + + body := "" + + if err == nil && latest != nil { + body = latest.Body + } + + s.render( + w, + "object_editor.gohtml", + map[string]any{ + "Title": "Object Edit", + "ObjectUUID": objectUUID, + "ObjectURI": uri, + "Body": body, + "EditType": "html", + }, + ) +} + +func (s *Server) handleObjectRevisionHistory( + w http.ResponseWriter, + r *http.Request, +) { + + uri := strings.TrimPrefix(r.URL.Path, "/o/r/") + + objectUUID, ok := objectUUIDFromURI(uri) + + if !ok { + http.NotFound(w, r) + return + } + + revisions, err := + s.store.ListRevisions(objectUUID) + + if err != nil { + + http.Error( + w, + err.Error(), + 500, + ) + + return + } + + s.render( + w, + "revisions.gohtml", + map[string]any{ + "Title": "Object Revision History", + "ObjectUUID": objectUUID, + "Revisions": revisions, + }, + ) +} diff --git a/internal/server/routes/api.go b/internal/server/routes/api.go index ee391f8..27f5b4d 100644 --- a/internal/server/routes/api.go +++ b/internal/server/routes/api.go @@ -5,6 +5,7 @@ import "net/http" type APIHandlers struct { CreateObject http.HandlerFunc ListObjects http.HandlerFunc + DeleteObject http.HandlerFunc SaveRevision http.HandlerFunc ListRevisions http.HandlerFunc RestoreRevision http.HandlerFunc @@ -14,6 +15,8 @@ type APIHandlers struct { RenamePath http.HandlerFunc RenamePreview http.HandlerFunc RemovePath http.HandlerFunc + ZombieList http.HandlerFunc + ZombiePurge http.HandlerFunc } func RegisterAPIRoutes( @@ -31,6 +34,11 @@ func RegisterAPIRoutes( h.ListObjects, ) + mux.HandleFunc( + "/_/api/v1/object/delete", + h.DeleteObject, + ) + mux.HandleFunc( "/_/api/v1/revisions/save", h.SaveRevision, @@ -75,4 +83,14 @@ func RegisterAPIRoutes( "/_/api/v1/path/remove", h.RemovePath, ) + + mux.HandleFunc( + "/_/api/v1/gc/zombies/list", + h.ZombieList, + ) + + mux.HandleFunc( + "/_/api/v1/gc/zombies/purge", + h.ZombiePurge, + ) } diff --git a/internal/server/server.go b/internal/server/server.go index 809b993..0bfeb27 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,9 +1,12 @@ package server import ( + "fmt" "html/template" "log" "net/http" + "strings" + "time" mw "pageswiki/internal/server/middleware" routespkg "pageswiki/internal/server/routes" @@ -30,9 +33,27 @@ func New( s.registerStaticRoutes() + s.mux.HandleFunc( + "/o/e/", + s.handleObjectEdit, + ) + + s.mux.HandleFunc( + "/o/r/", + s.handleObjectRevisionHistory, + ) + s.mux.HandleFunc( "/o/", - s.handleObjectRoute, + func(w http.ResponseWriter, r *http.Request) { + + if strings.HasPrefix(r.URL.Path, "/o/uuid:") { + s.handleObjectView(w, r) + return + } + + s.handleObjectRoute(w, r) + }, ) s.mux.HandleFunc( @@ -52,6 +73,8 @@ func New( s.registerRoutes() + s.startGCWorker() + return s } @@ -94,6 +117,28 @@ func (s *Server) mustLoadTemplates() { return out }, + + "formatBytes": func(n int) string { + + if n < 1024 { + return fmt.Sprintf("%d B", n) + } + + if n < 1024*1024 { + return fmt.Sprintf("%.1f KiB", float64(n)/1024.0) + } + + return fmt.Sprintf("%.1f MiB", float64(n)/(1024.0*1024.0)) + }, + + "formatUnix": func(ts int64) string { + + if ts <= 0 { + return "-" + } + + return time.Unix(ts, 0).Format("2006-01-02 15:04") + }, } tmpl := template.New("").Funcs(funcs) @@ -130,6 +175,7 @@ func (s *Server) registerRoutes() { routespkg.APIHandlers{ CreateObject: s.handleCreateObjectAPI, ListObjects: s.handleListObjectsAPI, + DeleteObject: s.handleDeleteObjectAPI, SaveRevision: s.handleSaveRevisionAPI, ListRevisions: s.handleRevisionListAPI, RestoreRevision: s.handleRestoreRevisionAPI, @@ -139,6 +185,8 @@ func (s *Server) registerRoutes() { RenamePath: s.handleRenamePath, RenamePreview: s.handleRenamePreview, RemovePath: s.handleRemovePathAPI, + ZombieList: s.handleZombieListAPI, + ZombiePurge: s.handleZombiePurgeAPI, }, ) } diff --git a/internal/store/delete_object.go b/internal/store/delete_object.go new file mode 100644 index 0000000..1d4deba --- /dev/null +++ b/internal/store/delete_object.go @@ -0,0 +1,55 @@ +package store + +func (s *Store) DeleteObject( + objectUUID string, +) error { + + tx, err := + s.DB.Begin() + + if err != nil { + return err + } + + _, err = tx.Exec(` + DELETE FROM paths + WHERE object_uuid = ? + `, objectUUID) + + if err != nil { + tx.Rollback() + return err + } + + _, err = tx.Exec(` + DELETE FROM tags + WHERE object_uuid = ? + `, objectUUID) + + if err != nil { + tx.Rollback() + return err + } + + _, err = tx.Exec(` + DELETE FROM revisions + WHERE object_uuid = ? + `, objectUUID) + + if err != nil { + tx.Rollback() + return err + } + + _, err = tx.Exec(` + DELETE FROM objects + WHERE uuid = ? + `, objectUUID) + + if err != nil { + tx.Rollback() + return err + } + + return tx.Commit() +} diff --git a/internal/store/recent_objects.go b/internal/store/recent_objects.go new file mode 100644 index 0000000..a3c2a64 --- /dev/null +++ b/internal/store/recent_objects.go @@ -0,0 +1,82 @@ +package store + +type RecentObject struct { + UUID string + Path string + Size int + UpdatedAt int64 +} + +func (s *Store) ListRecentObjects( + limit int, +) ([]RecentObject, error) { + + if limit <= 0 { + limit = 20 + } + + rows, err := s.DB.Query(` + WITH latest AS ( + SELECT + r.object_uuid, + r.body, + r.created_at, + ROW_NUMBER() OVER ( + PARTITION BY r.object_uuid + ORDER BY r.created_at DESC + ) AS rn + FROM revisions r + ), + primary_path AS ( + SELECT + p.object_uuid, + MIN(p.path) AS path + FROM paths p + GROUP BY p.object_uuid + ) + SELECT + o.uuid, + COALESCE(primary_path.path, ''), + COALESCE(LENGTH(latest.body), 0), + COALESCE(latest.created_at, o.created_at) + FROM objects o + LEFT JOIN latest + ON latest.object_uuid = o.uuid + AND latest.rn = 1 + LEFT JOIN primary_path + ON primary_path.object_uuid = o.uuid + ORDER BY + COALESCE(latest.created_at, o.created_at) DESC + LIMIT ? + `, + limit, + ) + + if err != nil { + return nil, err + } + + defer rows.Close() + + var out []RecentObject + + for rows.Next() { + + var ro RecentObject + + err := rows.Scan( + &ro.UUID, + &ro.Path, + &ro.Size, + &ro.UpdatedAt, + ) + + if err != nil { + return nil, err + } + + out = append(out, ro) + } + + return out, nil +} diff --git a/internal/store/zombie_gc.go b/internal/store/zombie_gc.go new file mode 100644 index 0000000..7b69912 --- /dev/null +++ b/internal/store/zombie_gc.go @@ -0,0 +1,141 @@ +package store + +type ZombieObject struct { + UUID string + CreatedAt int64 + RevisionCount int + MaxBodyLength int +} + +func (s *Store) ListZombieObjects( + graceSeconds int64, +) ([]ZombieObject, error) { + + rows, err := s.DB.Query(` + SELECT + o.uuid, + o.created_at, + COUNT(r.uuid) AS revision_count, + COALESCE(MAX(LENGTH(r.body)), 0) AS max_body_length + FROM objects o + LEFT JOIN paths p + ON p.object_uuid = o.uuid + LEFT JOIN revisions r + ON r.object_uuid = o.uuid + WHERE p.object_uuid IS NULL + AND o.created_at < strftime('%s','now') - ? + GROUP BY + o.uuid, + o.created_at + HAVING + revision_count = 0 + OR max_body_length = 0 + ORDER BY + o.created_at ASC + `, + graceSeconds, + ) + + if err != nil { + return nil, err + } + + defer rows.Close() + + var out []ZombieObject + + for rows.Next() { + + var z ZombieObject + + err := rows.Scan( + &z.UUID, + &z.CreatedAt, + &z.RevisionCount, + &z.MaxBodyLength, + ) + + if err != nil { + return nil, err + } + + out = append(out, z) + } + + return out, nil +} + +func (s *Store) PurgeZombieObjects( + graceSeconds int64, + limit int, +) (int, error) { + + zombies, err := + s.ListZombieObjects( + graceSeconds, + ) + + if err != nil { + return 0, err + } + + if limit > 0 && + len(zombies) > limit { + + zombies = zombies[:limit] + } + + tx, err := + s.DB.Begin() + + if err != nil { + return 0, err + } + + for _, z := range zombies { + + _, err = tx.Exec(` + DELETE FROM tags + WHERE object_uuid = ? + `, + z.UUID, + ) + + if err != nil { + tx.Rollback() + return 0, err + } + + _, err = tx.Exec(` + DELETE FROM revisions + WHERE object_uuid = ? + `, + z.UUID, + ) + + if err != nil { + tx.Rollback() + return 0, err + } + + _, err = tx.Exec(` + DELETE FROM objects + WHERE uuid = ? + `, + z.UUID, + ) + + if err != nil { + tx.Rollback() + return 0, err + } + } + + err = tx.Commit() + + if err != nil { + return 0, err + } + + return len(zombies), nil +} diff --git a/static/css/main.css b/static/css/main.css index 360e00c..4970703 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -5,6 +5,10 @@ body { margin: 0; } +a { + color:lime; +} + .topbar { display: flex; justify-content: space-between; diff --git a/templates/pages/home.gohtml b/templates/pages/home.gohtml index c7c9673..05a6ccc 100644 --- a/templates/pages/home.gohtml +++ b/templates/pages/home.gohtml @@ -44,7 +44,7 @@ Draft
-
+

Pages Wiki

@@ -70,35 +70,70 @@ Create / Edit -

-Examples: -

- -
    -
  • /pageswiki/network/bgp
  • -
  • /notes/todo
  • -
  • /dc/cooling
  • -
-
-
+

Recent Objects

-
@@ -160,6 +195,14 @@ document diff --git a/templates/pages/object_editor.gohtml b/templates/pages/object_editor.gohtml new file mode 100644 index 0000000..6d9b8b7 --- /dev/null +++ b/templates/pages/object_editor.gohtml @@ -0,0 +1,79 @@ +{{ define "object_editor.gohtml" }} + + + + +{{ .Title }} + + + + + + +
+ + + +
+ +
+ +
+ +
+Object: +{{ .ObjectURI }} +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + + + +
+ +
+ + + + + +{{ end }} diff --git a/templates/pages/object_view.gohtml b/templates/pages/object_view.gohtml new file mode 100644 index 0000000..94a1333 --- /dev/null +++ b/templates/pages/object_view.gohtml @@ -0,0 +1,71 @@ +{{ define "object_view.gohtml" }} + + + + +{{ .Title }} + + + + + + +
+ + + +
+ +
+ +
+
+Object: +{{ .ObjectURI }} +
+ +
+{{ .RenderedHTML }} +
+
+ +
+ + + + + +{{ end }}