Compare commits

...

1 Commits

Author SHA1 Message Date
Max Richter
66ab9ca7fc WIP 2025-09-24 21:32:27 +02:00
21 changed files with 450 additions and 9 deletions

View File

@@ -1,10 +1,11 @@
go 1.24.5
go 1.25.1
use (
./parser
./registry
./renderer
./server
./server-new
./template
./testdata
./validator

View File

@@ -1,6 +1,6 @@
module git.max-richter.dev/max/marka/parser
go 1.24.5
go 1.25.1
require (
git.max-richter.dev/max/marka/registry v0.0.0-20250817132016-6db87db32567

View File

@@ -23,6 +23,8 @@ func DetectType(markdownContent string) (string, error) {
return "", fmt.Errorf("failed to compile template: %w", err)
}
fmt.Println("Content:", markdownContent)
blocks := matcher.MatchBlocksFuzzy(markdownContent, defaultSchema, 0.3)
result, err := decoders.Parse(blocks)
@@ -30,6 +32,8 @@ func DetectType(markdownContent string) (string, error) {
return "", fmt.Errorf("failed to parse blocks: %w", err)
}
fmt.Println("Result: ", result)
if result, ok := result.(map[string]any); ok {
if contentType, ok := result["@type"]; ok {
return contentType.(string), nil

View File

@@ -1,3 +1,3 @@
module git.max-richter.dev/max/marka/registry
go 1.24.5
go 1.25.1

View File

@@ -1,6 +1,6 @@
module git.max-richter.dev/max/marka/renderer
go 1.24.5
go 1.25.1
require (
git.max-richter.dev/max/marka/parser v0.0.0-20250819170608-69c2550f448e

29
server-new/.air.toml Normal file
View 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
View File

@@ -0,0 +1 @@
tmp/

View 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
View File

@@ -0,0 +1,3 @@
module git.max-richter.dev/max/marka/server-new
go 1.25.1

View File

@@ -0,0 +1,5 @@
package adapters
import "errors"
var ErrNotFound = errors.New("not found")

View File

@@ -0,0 +1,97 @@
package adapters
import (
"os"
"path/filepath"
)
type LocalFsAdapter struct {
root string
}
func (l LocalFsAdapter) readDir(path string) (FsResponse, error) {
dirInfo, _ := os.Stat(path)
entries, err := os.ReadDir(path)
if err != nil {
return FsResponse{}, err
}
out := make([]FsDirEntry, 0, len(entries))
for _, e := range entries {
info, _ := e.Info()
entryType := "dir"
if !e.IsDir() {
entryType = contentTypeFor(e.Name())
}
out = append(out, FsDirEntry{
Name: e.Name(),
Type: entryType,
ModTime: info.ModTime(),
})
}
return FsResponse{
Dir: &FsDir{
Files: out,
Name: ResponsePath(l.root, path),
ModTime: dirInfo.ModTime(),
},
}, nil
}
func (l LocalFsAdapter) readFile(path string) (FsResponse, error) {
fi, err := os.Stat(path)
if err != nil {
return FsResponse{}, err
}
data, err := os.ReadFile(path)
if err != nil {
return FsResponse{}, err
}
return FsResponse{
File: &FsFile{
Name: ResponsePath(l.root, path),
Type: contentTypeFor(path),
ModTime: fi.ModTime(),
Content: data,
},
}, nil
}
func (l LocalFsAdapter) Read(path string) (FsResponse, error) {
cleanRel, err := SafeRel(l.root, path)
if err != nil {
return FsResponse{}, err
}
target := filepath.Join(l.root, filepath.FromSlash(cleanRel))
fi, err := os.Stat(target)
if err != nil {
if os.IsNotExist(err) {
return FsResponse{}, ErrNotFound
}
return FsResponse{}, err
}
if fi.IsDir() {
return l.readDir(target)
}
return l.readFile(target)
}
func (LocalFsAdapter) Write(path string, content []byte) error {
return nil
}
func NewLocalFsAdapter(root string) (FileAdapter, error) {
return LocalFsAdapter{
root: root,
}, nil
}

View File

@@ -0,0 +1,78 @@
package adapters
import (
"errors"
"mime"
"os"
"path/filepath"
"strings"
"time"
)
func SafeRel(root, requested string) (string, error) {
s := 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)
}
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()
}
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"
}

View File

@@ -0,0 +1,34 @@
// Package adapters are the backend to that connects the marka server to a storage
package adapters
import "time"
type FileAdapter interface {
Read(path string) (FsResponse, error)
Write(path string, content []byte) error
}
type FsFile struct {
Name string `json:"name"`
Type string `json:"type"`
Content []byte `json:"content"`
ModTime time.Time `json:"modTime"`
}
type FsDirEntry struct {
Name string `json:"name"`
Type string `json:"type"`
IsDir bool `json:"isDir,omitempty"`
ModTime time.Time `json:"modTime"`
}
type FsDir struct {
Files []FsDirEntry `json:"files"`
Name string `json:"name"`
ModTime time.Time `json:"modTime"`
}
type FsResponse struct {
Dir *FsDir `json:"dir,omitempty"`
File *FsFile `json:"file,omitempty"`
}

View File

@@ -0,0 +1,20 @@
package handler
import (
"encoding/json"
"net/http"
)
type ErrorResponse struct {
Error string `json:"error"`
}
func writeError(w http.ResponseWriter, code int, err error) {
writeJSON(w, code, ErrorResponse{Error: err.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)
}

View File

@@ -0,0 +1,104 @@
// Package handler provides the HTTP handler for the marka server
package handler
import (
"errors"
"net/http"
"time"
"git.max-richter.dev/max/marka/parser"
"git.max-richter.dev/max/marka/server-new/internal/adapters"
)
type ResponseItem struct {
Name string `json:"name"`
Content any `json:"content,omitempty"`
Type string `json:"type,omitempty"`
IsDir bool `json:"isDir"`
Size int64 `json:"size,omitempty"`
ModTime time.Time `json:"modTime"`
}
type Handler struct {
adapter adapters.FileAdapter
}
func (h *Handler) get(w http.ResponseWriter, target string) {
fsEntry, err := h.adapter.Read(target)
if err != nil {
writeError(w, 500, err)
return
}
if fsEntry.File != nil {
if fsEntry.File.Content != nil && fsEntry.File.Type == "application/markdown" {
data, err := parser.ParseFile(string(fsEntry.File.Content))
if err != nil {
writeError(w, 500, err)
return
}
res := ResponseItem{
Name: fsEntry.File.Name,
Type: fsEntry.File.Type,
Content: data,
IsDir: false,
Size: int64(len(fsEntry.File.Content)),
ModTime: fsEntry.File.ModTime,
}
writeJSON(w, 200, res)
return
}
res := ResponseItem{
Name: fsEntry.File.Name,
Content: fsEntry.File.Content,
Type: fsEntry.File.Type,
IsDir: false,
Size: int64(len(fsEntry.File.Content)),
ModTime: fsEntry.File.ModTime,
}
writeJSON(w, 200, res)
return
}
if fsEntry.Dir != nil {
res := ResponseItem{
Name: fsEntry.Dir.Name,
Content: fsEntry.Dir.Files,
IsDir: true,
ModTime: fsEntry.Dir.ModTime,
}
writeJSON(w, 200, res)
return
}
}
func (h *Handler) post(w http.ResponseWriter, target string) {
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reqPath := r.URL.Path
if reqPath == "" {
reqPath = "/"
}
target := cleanURLLike(reqPath)
switch r.Method {
case http.MethodGet:
h.get(w, target)
case http.MethodPost:
h.post(w, target)
default:
writeError(w, http.StatusMethodNotAllowed, errors.New("method not allowed"))
}
}
func NewHandler(adapter adapters.FileAdapter) http.Handler {
return &Handler{
adapter: adapter,
}
}

View File

@@ -0,0 +1,24 @@
package handler
import "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, "/")
}

View File

@@ -1,6 +1,6 @@
module git.max-richter.dev/max/marka/server
go 1.24.5
go 1.25.1
require git.max-richter.dev/max/marka/parser v0.0.0-20250819170608-69c2550f448e

View File

@@ -12,7 +12,7 @@ func CleanURLLike(p string) string {
return "/"
}
parts := []string{}
for seg := range strings.SplitSeq(strings.ReplaceAll(p, "/", "/"), "/") {
for seg := range strings.SplitSeq(p, "/") {
switch seg {
case "", ".":
continue

View File

@@ -1,6 +1,6 @@
module git.max-richter.dev/max/marka/template
go 1.24.5
go 1.25.1
require (
git.max-richter.dev/max/marka/registry v0.0.0-20250819170608-69c2550f448e

2
testdata/go.mod vendored
View File

@@ -1,3 +1,3 @@
module git.max-richter.dev/max/marka/testdata
go 1.24.5
go 1.25.1

View File

@@ -1,6 +1,6 @@
module git.max-richter.dev/max/marka/validator
go 1.24.5
go 1.25.1
require (
git.max-richter.dev/max/marka/registry v0.0.0-20250819170608-69c2550f448e