init
This commit is contained in:
commit
fa6a4c650f
93 files changed
+6555
No files matched your search
@@ -0,0 +1,109 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/api"
|
||||
)
|
||||
|
||||
func (s *Server) handleCreateObjectAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
w,
|
||||
400,
|
||||
"bad_json",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := s.store.CreateObject(req.Kind)
|
||||
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
w,
|
||||
500,
|
||||
"create_failed",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"uuid": id,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleListObjectsAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
objs, err := s.store.ListObjects()
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
w,
|
||||
500,
|
||||
"list_failed",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(objs)
|
||||
}
|
||||
|
||||
func (s *Server) handleSaveRevisionAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Body string `json:"body"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
w,
|
||||
400,
|
||||
"bad_json",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.store.SaveRevision(
|
||||
req.ObjectUUID,
|
||||
req.Body,
|
||||
req.Message,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
api.WriteError(
|
||||
w,
|
||||
500,
|
||||
"save_failed",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "ok",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type renamePreviewResponse struct {
|
||||
Targets []string `json:"targets"`
|
||||
}
|
||||
|
||||
func (s *Server) handleRenamePreview(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
oldPath :=
|
||||
r.URL.Query().Get(
|
||||
"old_path",
|
||||
)
|
||||
|
||||
rows, err :=
|
||||
s.store.DB.Query(`
|
||||
SELECT path
|
||||
FROM paths
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
ORDER BY LENGTH(path) ASC
|
||||
`,
|
||||
oldPath,
|
||||
oldPath+"/%",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var targets []string
|
||||
|
||||
for rows.Next() {
|
||||
|
||||
var p string
|
||||
|
||||
err := rows.Scan(&p)
|
||||
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
targets =
|
||||
append(
|
||||
targets,
|
||||
p,
|
||||
)
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
renamePreviewResponse{
|
||||
Targets: targets,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type renamePathRequest struct {
|
||||
OldPath string `json:"old_path"`
|
||||
NewPath string `json:"new_path"`
|
||||
Recursive bool `json:"recursive"`
|
||||
}
|
||||
|
||||
type renamePreview struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func (s *Server) handleRenamePath(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req renamePathRequest
|
||||
|
||||
err := json.NewDecoder(
|
||||
r.Body,
|
||||
).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if req.OldPath == "" ||
|
||||
req.NewPath == "" {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
"invalid path",
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if req.Recursive {
|
||||
|
||||
var count int
|
||||
|
||||
err :=
|
||||
s.store.DB.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM paths
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
`,
|
||||
req.OldPath,
|
||||
req.OldPath+"/%",
|
||||
).Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Query().Get("preview") == "1" {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"application/json",
|
||||
)
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
renamePreview{
|
||||
Count: count,
|
||||
},
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
tx, err :=
|
||||
s.store.DB.BeginTx(
|
||||
r.Context(),
|
||||
&sql.TxOptions{},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`
|
||||
UPDATE paths
|
||||
SET path =
|
||||
CASE
|
||||
WHEN path = ?
|
||||
THEN ?
|
||||
ELSE REPLACE(
|
||||
path,
|
||||
? || '/',
|
||||
? || '/'
|
||||
)
|
||||
END
|
||||
WHERE path = ?
|
||||
OR path LIKE ?
|
||||
`,
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
|
||||
req.OldPath,
|
||||
req.OldPath+"/%",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
_, err =
|
||||
s.store.DB.Exec(`
|
||||
UPDATE paths
|
||||
SET path = ?
|
||||
WHERE path = ?
|
||||
`,
|
||||
req.NewPath,
|
||||
req.OldPath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package server
|
||||
|
||||
import "strings"
|
||||
|
||||
type Breadcrumb struct {
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
func buildBreadcrumbs(
|
||||
path string,
|
||||
) []Breadcrumb {
|
||||
|
||||
path = cleanSemanticPath(path)
|
||||
|
||||
if path == "/" {
|
||||
return nil
|
||||
}
|
||||
|
||||
parts :=
|
||||
strings.Split(
|
||||
strings.Trim(path, "/"),
|
||||
"/",
|
||||
)
|
||||
|
||||
var out []Breadcrumb
|
||||
current := ""
|
||||
|
||||
for _, p := range parts {
|
||||
|
||||
current += "/" + p
|
||||
|
||||
out = append(out, Breadcrumb{
|
||||
Name: p,
|
||||
Path: current,
|
||||
})
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ExplorerNode struct {
|
||||
Path string
|
||||
Children []ExplorerNode
|
||||
}
|
||||
|
||||
func (s *Server) buildExplorerTree(
|
||||
path string,
|
||||
depth int,
|
||||
) []ExplorerNode {
|
||||
|
||||
if depth > 4 {
|
||||
return nil
|
||||
}
|
||||
|
||||
children, err :=
|
||||
s.store.ListChildren(path)
|
||||
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var out []ExplorerNode
|
||||
|
||||
for _, child := range children {
|
||||
|
||||
node := ExplorerNode{
|
||||
Path: child,
|
||||
}
|
||||
|
||||
node.Children =
|
||||
s.buildExplorerTree(
|
||||
child,
|
||||
depth+1,
|
||||
)
|
||||
|
||||
out = append(out, node)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) handleExplorer(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
tree :=
|
||||
s.buildExplorerTree(
|
||||
"/",
|
||||
0,
|
||||
)
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"explorer.gohtml",
|
||||
map[string]any{
|
||||
"Title": "Explorer",
|
||||
"Tree": tree,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
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()
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) handleNewPage(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
draftPath := fmt.Sprintf(
|
||||
"/draft/%d",
|
||||
time.Now().UnixNano(),
|
||||
)
|
||||
|
||||
_, err :=
|
||||
s.store.CreateObject(
|
||||
draftPath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
"/e"+draftPath,
|
||||
http.StatusFound,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package server
|
||||
|
||||
import "net/http"
|
||||
|
||||
func (s *Server) handleHealth(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"text/plain",
|
||||
)
|
||||
|
||||
_, _ = w.Write(
|
||||
[]byte("ok"),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) handleLinkManager(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
objects, _ := s.store.ListObjects()
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"links.gohtml",
|
||||
map[string]any{
|
||||
"Title": "Link Manager",
|
||||
"Objects": objects,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) expandMacros(
|
||||
body string,
|
||||
currentPath string,
|
||||
) string {
|
||||
|
||||
if strings.Contains(
|
||||
body,
|
||||
"{{children}}",
|
||||
) {
|
||||
|
||||
children, err :=
|
||||
s.store.ListChildPaths(
|
||||
currentPath,
|
||||
)
|
||||
|
||||
if err == nil {
|
||||
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString(
|
||||
`<section class="children-list">`,
|
||||
)
|
||||
|
||||
b.WriteString("<ul>")
|
||||
|
||||
for _, child := range children {
|
||||
|
||||
name :=
|
||||
strings.TrimPrefix(
|
||||
child,
|
||||
currentPath,
|
||||
)
|
||||
|
||||
name =
|
||||
strings.TrimPrefix(
|
||||
name,
|
||||
"/",
|
||||
)
|
||||
|
||||
b.WriteString(
|
||||
fmt.Sprintf(
|
||||
`<li><a href="%s">%s</a></li>`,
|
||||
child,
|
||||
name,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
b.WriteString("</ul>")
|
||||
b.WriteString("</section>")
|
||||
|
||||
body = strings.ReplaceAll(
|
||||
body,
|
||||
"{{children}}",
|
||||
b.String(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) normalizeSlashes(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
if r.URL.Path != "/" &&
|
||||
strings.HasSuffix(r.URL.Path, "/") {
|
||||
|
||||
target := strings.TrimSuffix(
|
||||
r.URL.Path,
|
||||
"/",
|
||||
)
|
||||
|
||||
if r.URL.RawQuery != "" {
|
||||
target += "?" + r.URL.RawQuery
|
||||
}
|
||||
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
target,
|
||||
http.StatusMovedPermanently,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Logging(
|
||||
next http.Handler,
|
||||
) http.Handler {
|
||||
|
||||
return http.HandlerFunc(
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
start := time.Now()
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
log.Printf(
|
||||
"%s %s %s",
|
||||
r.Method,
|
||||
r.URL.Path,
|
||||
time.Since(start),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NormalizePath(
|
||||
next http.Handler,
|
||||
) http.Handler {
|
||||
|
||||
return http.HandlerFunc(
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
p := r.URL.Path
|
||||
|
||||
if p != "/" {
|
||||
|
||||
for strings.HasSuffix(
|
||||
p,
|
||||
"/",
|
||||
) {
|
||||
|
||||
p = strings.TrimSuffix(
|
||||
p,
|
||||
"/",
|
||||
)
|
||||
}
|
||||
|
||||
if p == "" {
|
||||
p = "/"
|
||||
}
|
||||
|
||||
if p != r.URL.Path {
|
||||
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
p,
|
||||
http.StatusMovedPermanently,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Recovery(
|
||||
next http.Handler,
|
||||
) http.Handler {
|
||||
|
||||
return http.HandlerFunc(
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
defer func() {
|
||||
|
||||
if err := recover(); err != nil {
|
||||
|
||||
log.Printf(
|
||||
"panic recovered: %v",
|
||||
err,
|
||||
)
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
"internal server error",
|
||||
500,
|
||||
)
|
||||
}
|
||||
}()
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const RequestIDKey contextKey = "request_id"
|
||||
|
||||
func RequestID(
|
||||
next http.Handler,
|
||||
) http.Handler {
|
||||
|
||||
return http.HandlerFunc(
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
id := uuid.NewString()
|
||||
|
||||
ctx := context.WithValue(
|
||||
r.Context(),
|
||||
RequestIDKey,
|
||||
id,
|
||||
)
|
||||
|
||||
w.Header().Set(
|
||||
"X-Request-ID",
|
||||
id,
|
||||
)
|
||||
|
||||
next.ServeHTTP(
|
||||
w,
|
||||
r.WithContext(ctx),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleObjectRoute(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
addr :=
|
||||
strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/o/",
|
||||
)
|
||||
|
||||
switch {
|
||||
|
||||
case strings.HasPrefix(
|
||||
addr,
|
||||
"path:",
|
||||
):
|
||||
|
||||
s.resolvePathAddress(
|
||||
w,
|
||||
r,
|
||||
strings.TrimPrefix(
|
||||
addr,
|
||||
"path:",
|
||||
),
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
default:
|
||||
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) resolvePathAddress(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
path string,
|
||||
) {
|
||||
|
||||
switch {
|
||||
|
||||
case strings.HasPrefix(
|
||||
path,
|
||||
"/system/static/",
|
||||
):
|
||||
|
||||
s.resolveStaticObject(
|
||||
w,
|
||||
r,
|
||||
path,
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
default:
|
||||
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) resolveStaticObject(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
path string,
|
||||
) {
|
||||
|
||||
rel :=
|
||||
strings.TrimPrefix(
|
||||
path,
|
||||
"/system/static/",
|
||||
)
|
||||
|
||||
localPath :=
|
||||
filepath.Join(
|
||||
"static",
|
||||
rel,
|
||||
)
|
||||
|
||||
if _, err := os.Stat(localPath); err != nil {
|
||||
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(
|
||||
w,
|
||||
r,
|
||||
localPath,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) handleBindPathAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.BindPath(
|
||||
req.Path,
|
||||
req.ObjectUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "ok",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) handleRenamePathAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
OldPath string `json:"old_path"`
|
||||
NewPath string `json:"new_path"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(
|
||||
r.Body,
|
||||
).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.RenamePath(
|
||||
req.OldPath,
|
||||
req.NewPath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(
|
||||
http.StatusNoContent,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handleRemovePathAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(
|
||||
r.Body,
|
||||
).Decode(&req)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.RemovePath(
|
||||
req.Path,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(
|
||||
http.StatusNoContent,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleRawView(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path := strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/raw",
|
||||
)
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(path)
|
||||
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
rev, err :=
|
||||
s.store.GetLatestRevision(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
"text/plain",
|
||||
)
|
||||
|
||||
_, _ = w.Write(
|
||||
[]byte(rev.Body),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package server
|
||||
|
||||
func init() {}
|
||||
@@ -0,0 +1,38 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) render(
|
||||
w http.ResponseWriter,
|
||||
name string,
|
||||
data any,
|
||||
) {
|
||||
|
||||
s.templates.Funcs(
|
||||
template.FuncMap{
|
||||
"sub": func(a, b int) int {
|
||||
return a - b
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
err := s.templates.ExecuteTemplate(
|
||||
w,
|
||||
name,
|
||||
data,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"pageswiki/internal/render"
|
||||
)
|
||||
|
||||
func (s *Server) handleMarkdownRenderAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
Markdown string `json:"markdown"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"html": render.Markdown(req.Markdown),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) handleRevisionListAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
uuid := r.URL.Query().Get(
|
||||
"uuid",
|
||||
)
|
||||
|
||||
rows, err := s.store.DB.Query(`
|
||||
SELECT
|
||||
uuid,
|
||||
message,
|
||||
created_at
|
||||
FROM revisions
|
||||
WHERE object_uuid = ?
|
||||
ORDER BY created_at DESC
|
||||
`, uuid)
|
||||
|
||||
if err != nil {
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
var out []map[string]any
|
||||
|
||||
for rows.Next() {
|
||||
|
||||
var revisionUUID string
|
||||
var message string
|
||||
var createdAt int64
|
||||
|
||||
err := rows.Scan(
|
||||
&revisionUUID,
|
||||
&message,
|
||||
&createdAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
out = append(out, map[string]any{
|
||||
"uuid": revisionUUID,
|
||||
"message": message,
|
||||
"created_at": createdAt,
|
||||
})
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(out)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleRevisionBrowser(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
objectUUID := strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/r/history/",
|
||||
)
|
||||
|
||||
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": "Revision History",
|
||||
"ObjectUUID": objectUUID,
|
||||
"Revisions": revisions,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) handleRestoreRevisionAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
revisionUUID :=
|
||||
r.URL.Query().Get("revision")
|
||||
|
||||
row := s.store.DB.QueryRow(`
|
||||
SELECT
|
||||
object_uuid,
|
||||
body
|
||||
FROM revisions
|
||||
WHERE uuid = ?
|
||||
`, revisionUUID)
|
||||
|
||||
var objectUUID string
|
||||
var body string
|
||||
|
||||
err := row.Scan(
|
||||
&objectUUID,
|
||||
&body,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.store.SaveRevision(
|
||||
objectUUID,
|
||||
body,
|
||||
"restore revision",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = w.Write([]byte("ok"))
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleRevisionView(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
hash := strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/r/view/",
|
||||
)
|
||||
|
||||
revision, err :=
|
||||
s.store.GetRevision(hash)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"revision_view.gohtml",
|
||||
map[string]any{
|
||||
"Title": "Revision View",
|
||||
"Revision": revision,
|
||||
"RenderedHTML": template.HTML(
|
||||
revision.Body,
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func renderColoredDiff(
|
||||
before string,
|
||||
after string,
|
||||
) string {
|
||||
|
||||
beforeLines := strings.Split(
|
||||
before,
|
||||
"\n",
|
||||
)
|
||||
|
||||
afterLines := strings.Split(
|
||||
after,
|
||||
"\n",
|
||||
)
|
||||
|
||||
max := len(beforeLines)
|
||||
|
||||
if len(afterLines) > max {
|
||||
max = len(afterLines)
|
||||
}
|
||||
|
||||
var out []string
|
||||
|
||||
out = append(
|
||||
out,
|
||||
`<pre class="diff-view">`,
|
||||
)
|
||||
|
||||
for i := 0; i < max; i++ {
|
||||
|
||||
lineNo := i + 1
|
||||
|
||||
var b string
|
||||
var a string
|
||||
|
||||
if i < len(beforeLines) {
|
||||
b = beforeLines[i]
|
||||
}
|
||||
|
||||
if i < len(afterLines) {
|
||||
a = afterLines[i]
|
||||
}
|
||||
|
||||
switch {
|
||||
|
||||
case b == a:
|
||||
|
||||
out = append(
|
||||
out,
|
||||
fmt.Sprintf(
|
||||
`<div class="diff-same"><span class="diff-line-no">%4d</span> %s</div>`,
|
||||
lineNo,
|
||||
html.EscapeString(a),
|
||||
),
|
||||
)
|
||||
|
||||
default:
|
||||
|
||||
if b != "" {
|
||||
|
||||
out = append(
|
||||
out,
|
||||
fmt.Sprintf(
|
||||
`<div class="diff-del"><span class="diff-line-no">%4d</span> - %s</div>`,
|
||||
lineNo,
|
||||
html.EscapeString(b),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
if a != "" {
|
||||
|
||||
out = append(
|
||||
out,
|
||||
fmt.Sprintf(
|
||||
`<div class="diff-add"><span class="diff-line-no">%4d</span> + %s</div>`,
|
||||
lineNo,
|
||||
html.EscapeString(a),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out = append(
|
||||
out,
|
||||
"</pre>",
|
||||
)
|
||||
|
||||
return strings.Join(
|
||||
out,
|
||||
"\n",
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handleRevisionDiff(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path := strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/r/diff/",
|
||||
)
|
||||
|
||||
parts := strings.SplitN(
|
||||
path,
|
||||
"/",
|
||||
2,
|
||||
)
|
||||
|
||||
if len(parts) != 2 {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
"bad diff path",
|
||||
400,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
beforeRev, err :=
|
||||
s.store.GetRevision(parts[0])
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
afterRev, err :=
|
||||
s.store.GetRevision(parts[1])
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
diffHTML := renderColoredDiff(
|
||||
beforeRev.Body,
|
||||
afterRev.Body,
|
||||
)
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"revision_diff.gohtml",
|
||||
map[string]any{
|
||||
"Title": "Revision Diff",
|
||||
"Before": beforeRev,
|
||||
"After": afterRev,
|
||||
"DiffHTML": template.HTML(
|
||||
diffHTML,
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package server
|
||||
|
||||
func init() {
|
||||
|
||||
/*
|
||||
ADD THIS ROUTE
|
||||
BEFORE semantic catch-all:
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/",
|
||||
s.handleRevisionBrowser,
|
||||
)
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package routes
|
||||
|
||||
import "net/http"
|
||||
|
||||
type APIHandlers struct {
|
||||
CreateObject http.HandlerFunc
|
||||
ListObjects http.HandlerFunc
|
||||
SaveRevision http.HandlerFunc
|
||||
ListRevisions http.HandlerFunc
|
||||
RestoreRevision http.HandlerFunc
|
||||
RenderMarkdown http.HandlerFunc
|
||||
AddTag http.HandlerFunc
|
||||
BindPath http.HandlerFunc
|
||||
RenamePath http.HandlerFunc
|
||||
RenamePreview http.HandlerFunc
|
||||
RemovePath http.HandlerFunc
|
||||
}
|
||||
|
||||
func RegisterAPIRoutes(
|
||||
mux *http.ServeMux,
|
||||
h APIHandlers,
|
||||
) {
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/object/create",
|
||||
h.CreateObject,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/object/list",
|
||||
h.ListObjects,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/revisions/save",
|
||||
h.SaveRevision,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/revisions/list",
|
||||
h.ListRevisions,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/revisions/restore",
|
||||
h.RestoreRevision,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/render/markdown",
|
||||
h.RenderMarkdown,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/tag/add",
|
||||
h.AddTag,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/path/bind",
|
||||
h.BindPath,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/path/rename",
|
||||
h.RenamePath,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/path/rename-preview",
|
||||
h.RenamePreview,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/_/api/v1/path/remove",
|
||||
h.RemovePath,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package routes
|
||||
|
||||
import "net/http"
|
||||
|
||||
type UIHandlers struct {
|
||||
Home http.HandlerFunc
|
||||
NewPage http.HandlerFunc
|
||||
SemanticEditor http.HandlerFunc
|
||||
SemanticPage http.HandlerFunc
|
||||
RawView http.HandlerFunc
|
||||
LinkManager http.HandlerFunc
|
||||
Health http.HandlerFunc
|
||||
Tree http.HandlerFunc
|
||||
}
|
||||
|
||||
func RegisterUIRoutes(
|
||||
mux *http.ServeMux,
|
||||
h UIHandlers,
|
||||
) {
|
||||
|
||||
mux.HandleFunc(
|
||||
"/healthz",
|
||||
h.Health,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/tree",
|
||||
h.Tree,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/new",
|
||||
h.NewPage,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/e",
|
||||
h.SemanticEditor,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/e/",
|
||||
h.SemanticEditor,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/raw",
|
||||
h.RawView,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/raw/",
|
||||
h.RawView,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/l",
|
||||
h.LinkManager,
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"/",
|
||||
func(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
|
||||
h.Home(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
switch r.URL.Path {
|
||||
|
||||
case "/tree":
|
||||
|
||||
h.Tree(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
case "/new":
|
||||
|
||||
h.NewPage(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/e" ||
|
||||
r.URL.Path == "/raw" {
|
||||
|
||||
http.NotFound(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
h.SemanticPage(
|
||||
w,
|
||||
r,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package server
|
||||
|
||||
func init() {}
|
||||
@@ -0,0 +1,48 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func cleanSemanticPath(
|
||||
path string,
|
||||
) string {
|
||||
|
||||
if path == "" {
|
||||
return "/"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
||||
if len(path) > 1 &&
|
||||
strings.HasSuffix(path, "/") {
|
||||
|
||||
path = strings.TrimSuffix(
|
||||
path,
|
||||
"/",
|
||||
)
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func parentPath(
|
||||
path string,
|
||||
) string {
|
||||
|
||||
path = cleanSemanticPath(path)
|
||||
|
||||
if path == "/" {
|
||||
return "/"
|
||||
}
|
||||
|
||||
idx := strings.LastIndex(path, "/")
|
||||
|
||||
if idx <= 0 {
|
||||
return "/"
|
||||
}
|
||||
|
||||
return path[:idx]
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) handleSemanticRoute(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path := r.URL.Path
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(path)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.NotFound(w, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rendered :=
|
||||
s.expandMacros(
|
||||
latest.Body,
|
||||
path,
|
||||
)
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"page.gohtml",
|
||||
map[string]any{
|
||||
"Title": path,
|
||||
"Path": path,
|
||||
"ObjectUUID": objectUUID,
|
||||
"RenderedHTML": template.HTML(
|
||||
rendered,
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handleSemanticEditor(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
path := strings.TrimPrefix(
|
||||
r.URL.Path,
|
||||
"/e",
|
||||
)
|
||||
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
objectUUID, err :=
|
||||
s.store.ResolvePath(path)
|
||||
|
||||
if err != nil {
|
||||
|
||||
objectUUID, err =
|
||||
s.store.CreateObject(path)
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
latest, err :=
|
||||
s.store.GetLatestRevision(
|
||||
objectUUID,
|
||||
)
|
||||
|
||||
body := ""
|
||||
|
||||
if err == nil {
|
||||
body = latest.Body
|
||||
}
|
||||
|
||||
editType :=
|
||||
r.URL.Query().Get(
|
||||
"edittype",
|
||||
)
|
||||
|
||||
if editType == "" {
|
||||
editType = "html"
|
||||
}
|
||||
|
||||
if editType == "markdown" {
|
||||
|
||||
body =
|
||||
htmlToMarkdown(
|
||||
body,
|
||||
)
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"editor.gohtml",
|
||||
map[string]any{
|
||||
"Title": path,
|
||||
"Path": path,
|
||||
"ObjectUUID": objectUUID,
|
||||
"Body": body,
|
||||
"RenderedHTML": template.HTML(
|
||||
s.expandMacros(
|
||||
body,
|
||||
path,
|
||||
),
|
||||
),
|
||||
"EditType": editType,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func htmlToMarkdown(
|
||||
in string,
|
||||
) string {
|
||||
|
||||
out := in
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"<h1>",
|
||||
"# ",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"</h1>",
|
||||
"",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"<ul>",
|
||||
"",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"</ul>",
|
||||
"",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"<li>",
|
||||
"- ",
|
||||
)
|
||||
|
||||
out = strings.ReplaceAll(
|
||||
out,
|
||||
"</li>",
|
||||
"",
|
||||
)
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
mw "pageswiki/internal/server/middleware"
|
||||
routespkg "pageswiki/internal/server/routes"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
mux *http.ServeMux
|
||||
templates *template.Template
|
||||
store *store.Store
|
||||
}
|
||||
|
||||
func New(
|
||||
st *store.Store,
|
||||
) *Server {
|
||||
|
||||
s := &Server{
|
||||
mux: http.NewServeMux(),
|
||||
store: st,
|
||||
}
|
||||
|
||||
s.mustLoadTemplates()
|
||||
|
||||
s.registerStaticRoutes()
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/o/",
|
||||
s.handleObjectRoute,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/history/",
|
||||
s.handleRevisionBrowser,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/view/",
|
||||
s.handleRevisionView,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/diff/",
|
||||
s.handleRevisionDiff,
|
||||
)
|
||||
|
||||
s.registerRoutes()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) registerStaticRoutes() {
|
||||
|
||||
staticFS := http.FileServer(
|
||||
http.Dir("./static"),
|
||||
)
|
||||
|
||||
s.mux.Handle(
|
||||
"/static/",
|
||||
http.StripPrefix(
|
||||
"/static/",
|
||||
staticFS,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) mustLoadTemplates() {
|
||||
|
||||
funcs := template.FuncMap{
|
||||
|
||||
"parentPath": parentPath,
|
||||
|
||||
"sub": func(a, b int) int {
|
||||
return a - b
|
||||
},
|
||||
|
||||
"add": func(a, b int) int {
|
||||
return a + b
|
||||
},
|
||||
|
||||
"until": func(n int) []int {
|
||||
|
||||
out := make([]int, n)
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
out[i] = i
|
||||
}
|
||||
|
||||
return out
|
||||
},
|
||||
}
|
||||
|
||||
tmpl := template.New("").Funcs(funcs)
|
||||
|
||||
parsed, err := tmpl.ParseGlob(
|
||||
"templates/**/*.gohtml",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
s.templates = parsed
|
||||
}
|
||||
|
||||
func (s *Server) registerRoutes() {
|
||||
|
||||
routespkg.RegisterUIRoutes(
|
||||
s.mux,
|
||||
routespkg.UIHandlers{
|
||||
Home: s.handleHome,
|
||||
NewPage: s.handleNewPage,
|
||||
SemanticEditor: s.handleSemanticEditor,
|
||||
SemanticPage: s.handleSemanticRoute,
|
||||
RawView: s.handleRawView,
|
||||
LinkManager: s.handleLinkManager,
|
||||
Health: s.handleHealth,
|
||||
Tree: s.handleTree,
|
||||
},
|
||||
)
|
||||
|
||||
routespkg.RegisterAPIRoutes(
|
||||
s.mux,
|
||||
routespkg.APIHandlers{
|
||||
CreateObject: s.handleCreateObjectAPI,
|
||||
ListObjects: s.handleListObjectsAPI,
|
||||
SaveRevision: s.handleSaveRevisionAPI,
|
||||
ListRevisions: s.handleRevisionListAPI,
|
||||
RestoreRevision: s.handleRestoreRevisionAPI,
|
||||
RenderMarkdown: s.handleMarkdownRenderAPI,
|
||||
AddTag: s.handleTagAddAPI,
|
||||
BindPath: s.handleBindPathAPI,
|
||||
RenamePath: s.handleRenamePath,
|
||||
RenamePreview: s.handleRenamePreview,
|
||||
RemovePath: s.handleRemovePathAPI,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) handler() http.Handler {
|
||||
|
||||
var h http.Handler = s.mux
|
||||
|
||||
h = mw.NormalizePath(h)
|
||||
h = mw.RequestID(h)
|
||||
h = mw.Logging(h)
|
||||
h = mw.Recovery(h)
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (s *Server) Run(addr string) error {
|
||||
|
||||
log.Printf(
|
||||
"listen %s",
|
||||
addr,
|
||||
)
|
||||
|
||||
return http.ListenAndServe(
|
||||
addr,
|
||||
s.handler(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
REGISTER BEFORE semantic catch-all:
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/history/",
|
||||
s.handleRevisionBrowser,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/view/",
|
||||
s.handleRevisionView,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/diff/",
|
||||
s.handleRevisionDiff,
|
||||
)
|
||||
@@ -0,0 +1,37 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) handleTagAddAPI(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
var req struct {
|
||||
ObjectUUID string `json:"object_uuid"`
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
err = s.store.AddTag(
|
||||
req.ObjectUUID,
|
||||
req.Tag,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "ok",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func templateFuncMap() template.FuncMap {
|
||||
|
||||
return template.FuncMap{
|
||||
|
||||
"safeHTML": func(
|
||||
s string,
|
||||
) template.HTML {
|
||||
|
||||
return template.HTML(s)
|
||||
},
|
||||
|
||||
"formatTime": func(
|
||||
ts int64,
|
||||
) string {
|
||||
|
||||
return time.Unix(
|
||||
ts,
|
||||
0,
|
||||
).Format(time.RFC3339)
|
||||
},
|
||||
|
||||
"parentPath": func(
|
||||
p string,
|
||||
) string {
|
||||
|
||||
parts :=
|
||||
strings.Split(
|
||||
strings.Trim(p, "/"),
|
||||
"/",
|
||||
)
|
||||
|
||||
if len(parts) <= 1 {
|
||||
return "/"
|
||||
}
|
||||
|
||||
parts =
|
||||
parts[:len(parts)-1]
|
||||
|
||||
return "/" +
|
||||
strings.Join(
|
||||
parts,
|
||||
"/",
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type TreeViewNode struct {
|
||||
Name string
|
||||
Path string
|
||||
Depth int
|
||||
IsDir bool
|
||||
IsDraft bool
|
||||
ChildCount int
|
||||
}
|
||||
|
||||
func (s *Server) handleTree(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
|
||||
tree, err :=
|
||||
s.store.BuildPathTree()
|
||||
|
||||
if err != nil {
|
||||
|
||||
http.Error(
|
||||
w,
|
||||
err.Error(),
|
||||
500,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
var flattened []store.FlatTreeNode
|
||||
|
||||
store.FlattenTree(
|
||||
tree,
|
||||
0,
|
||||
&flattened,
|
||||
)
|
||||
|
||||
var nodes []TreeViewNode
|
||||
|
||||
for _, n := range flattened {
|
||||
|
||||
nodes = append(
|
||||
nodes,
|
||||
TreeViewNode{
|
||||
Name: n.Name,
|
||||
Path: n.FullPath,
|
||||
Depth: n.Depth,
|
||||
IsDir: n.IsDir,
|
||||
ChildCount: n.ChildCount,
|
||||
|
||||
IsDraft: strings.HasPrefix(
|
||||
n.FullPath,
|
||||
"/draft/",
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
s.render(
|
||||
w,
|
||||
"tree.gohtml",
|
||||
map[string]any{
|
||||
"Nodes": nodes,
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user