Compare commits
1 Commits
3f0d25f935
...
tmp
Author | SHA1 | Date | |
---|---|---|---|
|
66ab9ca7fc
|
1
go.work
1
go.work
@@ -4,6 +4,7 @@ use (
|
|||||||
./parser
|
./parser
|
||||||
./registry
|
./registry
|
||||||
./renderer
|
./renderer
|
||||||
|
./server
|
||||||
./server-new
|
./server-new
|
||||||
./template
|
./template
|
||||||
./testdata
|
./testdata
|
||||||
|
@@ -1,8 +1,4 @@
|
|||||||
git.max-richter.dev/max/marka/parser v0.0.0-20250819170608-69c2550f448e/go.mod h1:xQK6tsgr9BOoeFw8JxjBwDkVENlOqapmcRkYyf/L+SQ=
|
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
git.max-richter.dev/max/marka/registry v0.0.0-20250819170608-69c2550f448e/go.mod h1:n793S7TENIfgHpZLz0lm0qorM7eCx3zBLby3Fb++hZA=
|
|
||||||
git.max-richter.dev/max/marka/template v0.0.0-20250819170608-69c2550f448e/go.mod h1:Uxi5xcxtnjopsIZjjMlFaWJGuglB9JNL++FuaSbOf6U=
|
|
||||||
git.max-richter.dev/max/marka/testdata v0.0.0-20250819195334-b3c01bb43d9a/go.mod h1:88SkY5pTONkgfBy1FT10LoqRC8rt36iF1fk/rupjuJY=
|
|
||||||
git.max-richter.dev/max/marka/validator v0.0.0-20250819170608-69c2550f448e/go.mod h1:qdGfCFRzsGedmnd77vb7pu/EMx0W0DcQBMEfvNxMYsw=
|
|
||||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
29
server-new/.air.toml
Normal file
29
server-new/.air.toml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
root = "."
|
||||||
|
tmp_dir = "tmp"
|
||||||
|
|
||||||
|
[build]
|
||||||
|
# Command to build the application.
|
||||||
|
cmd = "go build -o ./tmp/marka-server ./cmd/marka-server"
|
||||||
|
|
||||||
|
# The binary to run.
|
||||||
|
bin = "./tmp/marka-server"
|
||||||
|
|
||||||
|
# Command to run the application with arguments.
|
||||||
|
full_bin = "./tmp/marka-server -root=../examples -addr=:8080"
|
||||||
|
|
||||||
|
# Watch these file extensions.
|
||||||
|
include_ext = ["go", "http"]
|
||||||
|
|
||||||
|
# Ignore these directories.
|
||||||
|
exclude_dir = ["tmp"]
|
||||||
|
|
||||||
|
# Log file for build errors.
|
||||||
|
log = "air_errors.log"
|
||||||
|
|
||||||
|
[log]
|
||||||
|
# Show time in logs.
|
||||||
|
time = true
|
||||||
|
|
||||||
|
[misc]
|
||||||
|
# Delete tmp directory on exit.
|
||||||
|
clean_on_exit = true
|
1
server-new/.gitignore
vendored
Normal file
1
server-new/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
tmp/
|
41
server-new/cmd/marka-server/main.go
Normal file
41
server-new/cmd/marka-server/main.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"git.max-richter.dev/max/marka/server-new/internal/adapters"
|
||||||
|
"git.max-richter.dev/max/marka/server-new/internal/handler"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
root := flag.String("root", ".", "filesystem root to serve")
|
||||||
|
addr := flag.String("addr", ":8080", "listen address")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
absRoot, err := filepath.Abs(*root)
|
||||||
|
must(err)
|
||||||
|
|
||||||
|
info, err := os.Stat(absRoot)
|
||||||
|
must(err)
|
||||||
|
if !info.IsDir() {
|
||||||
|
log.Fatal("root is not a directory")
|
||||||
|
}
|
||||||
|
|
||||||
|
fsAdapter, err := adapters.NewLocalFsAdapter(absRoot)
|
||||||
|
must(err)
|
||||||
|
|
||||||
|
http.Handle("/", handler.NewHandler(fsAdapter))
|
||||||
|
|
||||||
|
log.Printf("listening on %s, root=%s", *addr, absRoot)
|
||||||
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func must(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
3
server-new/go.mod
Normal file
3
server-new/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module git.max-richter.dev/max/marka/server-new
|
||||||
|
|
||||||
|
go 1.25.1
|
@@ -1,3 +1,6 @@
|
|||||||
|
# Config file for Air - https://github.com/air-verse/air
|
||||||
|
# This file is for the marka-server application.
|
||||||
|
|
||||||
root = "."
|
root = "."
|
||||||
tmp_dir = "tmp"
|
tmp_dir = "tmp"
|
||||||
|
|
||||||
|
@@ -7,8 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"git.max-richter.dev/max/marka/server-new/internal/adapters"
|
"git.max-richter.dev/max/marka/server/internal/handlers"
|
||||||
"git.max-richter.dev/max/marka/server-new/internal/handler"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -25,10 +24,7 @@ func main() {
|
|||||||
log.Fatal("root is not a directory")
|
log.Fatal("root is not a directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
fsAdapter, err := adapters.NewLocalFsAdapter(absRoot)
|
http.Handle("/", handlers.NewFile(absRoot))
|
||||||
must(err)
|
|
||||||
|
|
||||||
http.Handle("/", handler.NewHandler(fsAdapter))
|
|
||||||
|
|
||||||
log.Printf("listening on %s, root=%s", *addr, absRoot)
|
log.Printf("listening on %s, root=%s", *addr, absRoot)
|
||||||
log.Fatal(http.ListenAndServe(*addr, nil))
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
||||||
|
@@ -1,3 +1,16 @@
|
|||||||
module git.max-richter.dev/max/marka/server-new
|
module git.max-richter.dev/max/marka/server
|
||||||
|
|
||||||
go 1.25.1
|
go 1.25.1
|
||||||
|
|
||||||
|
require git.max-richter.dev/max/marka/parser v0.0.0-20250819170608-69c2550f448e
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.max-richter.dev/max/marka/registry v0.0.0-20250817132016-6db87db32567 // indirect
|
||||||
|
git.max-richter.dev/max/marka/renderer v0.0.0-20250819170608-69c2550f448e // indirect
|
||||||
|
git.max-richter.dev/max/marka/template v0.0.0-20250817132016-6db87db32567 // indirect
|
||||||
|
git.max-richter.dev/max/marka/testdata v0.0.0-20250819195334-b3c01bb43d9a // indirect
|
||||||
|
github.com/agext/levenshtein v1.2.3 // indirect
|
||||||
|
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirect
|
||||||
|
go.yaml.in/yaml/v4 v4.0.0-rc.1 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
)
|
||||||
|
33
server/internal/fsx/contenttype.go
Normal file
33
server/internal/fsx/contenttype.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package fsx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var textPlainExtensions = map[string]bool{
|
||||||
|
".txt": true,
|
||||||
|
".log": true,
|
||||||
|
".json": true,
|
||||||
|
".yaml": true,
|
||||||
|
".yml": true,
|
||||||
|
".toml": true,
|
||||||
|
".xml": true,
|
||||||
|
".csv": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
func ContentTypeFor(name string) string {
|
||||||
|
ext := strings.ToLower(filepath.Ext(name))
|
||||||
|
switch ext {
|
||||||
|
case ".md", ".markdown", ".mdown":
|
||||||
|
return "application/markdown"
|
||||||
|
}
|
||||||
|
if ct := mime.TypeByExtension(ext); ct != "" {
|
||||||
|
return ct
|
||||||
|
}
|
||||||
|
if textPlainExtensions[ext] {
|
||||||
|
return "text/plain; charset=utf-8"
|
||||||
|
}
|
||||||
|
return "application/octet-stream"
|
||||||
|
}
|
56
server/internal/fsx/path.go
Normal file
56
server/internal/fsx/path.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package fsx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CleanURLLike(p string) string {
|
||||||
|
p = strings.TrimSpace(p)
|
||||||
|
if p == "" || p == "/" {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
parts := []string{}
|
||||||
|
for seg := range strings.SplitSeq(p, "/") {
|
||||||
|
switch seg {
|
||||||
|
case "", ".":
|
||||||
|
continue
|
||||||
|
case "..":
|
||||||
|
if len(parts) > 0 {
|
||||||
|
parts = parts[:len(parts)-1]
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
parts = append(parts, seg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "/" + strings.Join(parts, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func SafeRel(root, requested string) (string, error) {
|
||||||
|
s := CleanURLLike(requested)
|
||||||
|
if after, ok := strings.CutPrefix(s, "/"); ok {
|
||||||
|
s = after
|
||||||
|
}
|
||||||
|
full := filepath.Join(root, filepath.FromSlash(s))
|
||||||
|
rel, err := filepath.Rel(root, full)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if rel == "." {
|
||||||
|
return "/", nil
|
||||||
|
}
|
||||||
|
sep := string(filepath.Separator)
|
||||||
|
if strings.HasPrefix(rel, "..") || strings.Contains(rel, ".."+sep) {
|
||||||
|
return "", errors.New("path escapes root")
|
||||||
|
}
|
||||||
|
return "/" + filepath.ToSlash(rel), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResponsePath(root, full string) string {
|
||||||
|
rel, err := filepath.Rel(root, full)
|
||||||
|
if err != nil || rel == "." {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
return "/" + filepath.ToSlash(rel)
|
||||||
|
}
|
38
server/internal/handlers/file.go
Normal file
38
server/internal/handlers/file.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Package handlers provides HTTP handlers for the file system.
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"git.max-richter.dev/max/marka/server/internal/fsx"
|
||||||
|
"git.max-richter.dev/max/marka/server/internal/httpx"
|
||||||
|
)
|
||||||
|
|
||||||
|
type File struct{ root string }
|
||||||
|
|
||||||
|
func NewFile(root string) http.Handler { return &File{root: root} }
|
||||||
|
|
||||||
|
func (h *File) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
reqPath := r.URL.Path
|
||||||
|
if reqPath == "" {
|
||||||
|
reqPath = "/"
|
||||||
|
}
|
||||||
|
cleanRel, err := fsx.SafeRel(h.root, reqPath)
|
||||||
|
if err != nil {
|
||||||
|
httpx.WriteError(w, http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target := filepath.Join(h.root, filepath.FromSlash(cleanRel))
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
h.get(w, r, target)
|
||||||
|
case http.MethodPost:
|
||||||
|
h.post(w, r, target)
|
||||||
|
default:
|
||||||
|
httpx.WriteError(w, http.StatusMethodNotAllowed, errors.New("method not allowed"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
93
server/internal/handlers/get.go
Normal file
93
server/internal/handlers/get.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.max-richter.dev/max/marka/server/internal/fsx"
|
||||||
|
"git.max-richter.dev/max/marka/server/internal/httpx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *File) get(w http.ResponseWriter, r *http.Request, target string) {
|
||||||
|
fi, err := os.Stat(target)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
httpx.WriteError(w, http.StatusNotFound, errors.New("not found"))
|
||||||
|
} else {
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
h.handleDir(w, r, target)
|
||||||
|
} else {
|
||||||
|
h.handleFile(w, r, target, fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *File) handleDir(w http.ResponseWriter, r *http.Request, dirPath string) {
|
||||||
|
entries, err := os.ReadDir(dirPath)
|
||||||
|
if err != nil {
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Content any `json:"content,omitempty"`
|
||||||
|
IsDir bool `json:"isDir"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
ModTime time.Time `json:"modTime"`
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make([]item, 0, len(entries))
|
||||||
|
for _, e := range entries {
|
||||||
|
info, _ := e.Info() // As in original, ignore error to get partial info
|
||||||
|
entryPath := filepath.Join(dirPath, e.Name())
|
||||||
|
|
||||||
|
var content any
|
||||||
|
if !e.IsDir() && fsx.ContentTypeFor(e.Name()) == "application/markdown" {
|
||||||
|
content, _ = parseMarkdownFile(entryPath) // As in original, ignore error
|
||||||
|
}
|
||||||
|
|
||||||
|
out = append(out, item{
|
||||||
|
Name: e.Name(),
|
||||||
|
Path: fsx.ResponsePath(h.root, entryPath),
|
||||||
|
IsDir: e.IsDir(),
|
||||||
|
Content: content,
|
||||||
|
Size: sizeOrZero(info),
|
||||||
|
ModTime: modTimeOrZero(info),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
httpx.WriteJSON(w, http.StatusOK, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *File) handleFile(w http.ResponseWriter, r *http.Request, target string, fi os.FileInfo) {
|
||||||
|
contentType := fsx.ContentTypeFor(target)
|
||||||
|
|
||||||
|
if contentType == "application/markdown" {
|
||||||
|
res, err := parseMarkdownFile(target)
|
||||||
|
if err != nil {
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, fmt.Errorf("failed to parse markdown: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
httpx.WriteJSON(w, http.StatusOK, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(target)
|
||||||
|
if err != nil {
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
http.ServeContent(w, r, filepath.Base(target), fi.ModTime(), f)
|
||||||
|
}
|
30
server/internal/handlers/helpers.go
Normal file
30
server/internal/handlers/helpers.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.max-richter.dev/max/marka/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseMarkdownFile(path string) (any, error) {
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return parser.ParseFile(string(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func sizeOrZero(fi os.FileInfo) int64 {
|
||||||
|
if fi == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return fi.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
func modTimeOrZero(fi os.FileInfo) time.Time {
|
||||||
|
if fi == nil {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
return fi.ModTime()
|
||||||
|
}
|
47
server/internal/handlers/post.go
Normal file
47
server/internal/handlers/post.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"git.max-richter.dev/max/marka/server/internal/httpx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *File) post(w http.ResponseWriter, r *http.Request, target string) {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpPath := target + ".tmp~"
|
||||||
|
f, err := os.Create(tmpPath)
|
||||||
|
if err != nil {
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, copyErr := io.Copy(f, r.Body)
|
||||||
|
closeErr := f.Close()
|
||||||
|
|
||||||
|
if copyErr != nil {
|
||||||
|
os.Remove(tmpPath)
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, copyErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if closeErr != nil {
|
||||||
|
os.Remove(tmpPath)
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, closeErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Rename(tmpPath, target); err != nil {
|
||||||
|
os.Remove(tmpPath)
|
||||||
|
httpx.WriteError(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Location", r.URL.Path)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
}
|
20
server/internal/httpx/error.go
Normal file
20
server/internal/httpx/error.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package httpx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ErrorResponse struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteJSON(w http.ResponseWriter, code int, v any) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(code)
|
||||||
|
_ = json.NewEncoder(w).Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteError(w http.ResponseWriter, code int, err error) {
|
||||||
|
WriteJSON(w, code, ErrorResponse{Error: err.Error()})
|
||||||
|
}
|
11
server/internal/httpx/router.go
Normal file
11
server/internal/httpx/router.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package httpx
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Router struct{ mux *http.ServeMux }
|
||||||
|
|
||||||
|
func NewRouter() *Router { return &Router{mux: http.NewServeMux()} }
|
||||||
|
func (r *Router) Handle(pattern string, h http.Handler) { r.mux.Handle(pattern, h) }
|
||||||
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
|
r.mux.ServeHTTP(w, req)
|
||||||
|
}
|
Reference in New Issue
Block a user