Compare commits
20 Commits
2b965cb8f7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
137e3da6d4
|
||
|
|
148bc2c15c
|
||
|
|
cc19724277
|
||
|
|
851babe1e0
|
||
|
|
f14b685e43
|
||
|
|
fd6aeb8a27
|
||
|
|
4bfd2b9450
|
||
|
|
dfc38e2c86
|
||
|
|
3fe7577250
|
||
|
|
3cd320637c
|
||
|
|
7c5f9f7829
|
||
|
|
6c47c8c3e9
|
||
|
|
814be43be8
|
||
|
|
dc2cd1108e
|
||
|
|
5f52a630ea
|
||
|
|
a07e489700
|
||
|
|
0cf5e77b2a
|
||
|
|
cb132e90dd
|
||
|
|
53e6c6fb9f
|
||
|
|
026537fa05
|
@@ -41,8 +41,6 @@ func DetectType(markdownContent string) (string, error) {
|
||||
}
|
||||
|
||||
func MatchBlocks(markdownContent, templateContent string) ([]matcher.Block, error) {
|
||||
markdownContent = strings.TrimSuffix(markdownContent, "\n")
|
||||
|
||||
tpl, err := template.CompileTemplate(templateContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile template -> %w", err)
|
||||
@@ -66,13 +64,16 @@ func ParseFile(markdownContent string) (any, error) {
|
||||
}
|
||||
|
||||
func ParseFileWithTemplate(markdownContent string, templateContent string) (any, error) {
|
||||
markdownContent = strings.TrimSuffix(markdownContent, "\n")
|
||||
|
||||
tpl, err := template.CompileTemplate(templateContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile template -> %w", err)
|
||||
}
|
||||
|
||||
for strings.HasSuffix(markdownContent, "\n") {
|
||||
markdownContent = strings.TrimSuffix(markdownContent, "\n")
|
||||
}
|
||||
markdownContent = markdownContent + "\n"
|
||||
|
||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, tpl, 0.3)
|
||||
|
||||
result, err := decoders.Parse(blocks)
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
value: Person
|
||||
hidden: true
|
||||
- path: datePublished
|
||||
- path: url
|
||||
pathAlias: link
|
||||
- path: articleSection
|
||||
- path: reviewRating.ratingValue
|
||||
pathAlias: rating
|
||||
@@ -27,11 +29,13 @@
|
||||
value: 5
|
||||
codec: const
|
||||
hidden: true
|
||||
- path: keywords
|
||||
- path: reviewRating.worstRating
|
||||
value: 1
|
||||
codec: const
|
||||
hidden: true
|
||||
}
|
||||
---
|
||||
|
||||
# { headline }
|
||||
{ articleBody }
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
- path: "_type"
|
||||
codec: const
|
||||
value: Recipe
|
||||
- path: link
|
||||
- path: url
|
||||
pathAlias: link
|
||||
- path: image
|
||||
- path: author._type
|
||||
codec: const
|
||||
@@ -24,6 +25,9 @@
|
||||
- path: prepTime
|
||||
- path: cookTime
|
||||
- path: recipeYield
|
||||
pathAlias:
|
||||
- yield
|
||||
- portion
|
||||
- path: cookingMethod
|
||||
- path: nutrition
|
||||
- path: recipeCategory
|
||||
@@ -32,7 +36,6 @@
|
||||
---
|
||||
|
||||
# { name | text }
|
||||
|
||||
{ description | text }
|
||||
|
||||
## Ingredients
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
codec: const
|
||||
value: Review
|
||||
- path: tmdbId
|
||||
- path: link
|
||||
pathAlias: link
|
||||
- path: image
|
||||
- path: author.name
|
||||
pathAlias: author
|
||||
@@ -35,7 +37,4 @@
|
||||
---
|
||||
|
||||
# { itemReviewed.name }
|
||||
{ keywords | hashtags }
|
||||
|
||||
## Review
|
||||
{ reviewBody }
|
||||
|
||||
@@ -37,7 +37,7 @@ func RenderFile(rawJSON []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
// 5) validate JSON against schema
|
||||
if schemaName, ok := data["@schema"].(string); ok {
|
||||
if schemaName, ok := data["_schema"].(string); ok {
|
||||
if validationErr := validator.ValidateSchema(data, schemaName); validationErr != nil {
|
||||
return nil, fmt.Errorf("failed to validate schema: %w", validationErr)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ cmd = "go build -o ./tmp/marka-server ./cmd/marka-server"
|
||||
bin = "./tmp/marka-server"
|
||||
|
||||
# Command to run the application with arguments.
|
||||
full_bin = "./tmp/marka-server -root=/home/max/Notes/resources -addr=:8080 -playground-root=./playground"
|
||||
full_bin = "MARKA_API_KEY='SECRET' ./tmp/marka-server -root=/home/max/Notes/resources -addr=:8080 -playground-root=./playground"
|
||||
|
||||
# Watch these file extensions.
|
||||
include_ext = ["go", "http"]
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -61,7 +62,9 @@ func main() {
|
||||
fsAdapter, err := adapters.NewLocalFsAdapter(absRoots)
|
||||
must(err)
|
||||
|
||||
http.Handle("/", handler.NewHandler(fsAdapter))
|
||||
apiKey := os.Getenv("MARKA_API_KEY")
|
||||
fmt.Println(apiKey)
|
||||
http.Handle("/", handler.NewHandler(fsAdapter, apiKey))
|
||||
|
||||
log.Printf("listening on %s, roots=%s", *addr, strings.Join(absRoots, ", "))
|
||||
log.Fatal(http.ListenAndServe(*addr, nil))
|
||||
|
||||
@@ -2,6 +2,7 @@ package adapters
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -118,6 +119,7 @@ func (l *LocalFsAdapter) readFile(path string, root string) (*Entry, error) {
|
||||
if err == nil {
|
||||
content = parsedContent
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
// Fallback to raw content on parsing error
|
||||
content = fileContent
|
||||
}
|
||||
@@ -255,6 +257,66 @@ func (l *LocalFsAdapter) Read(path string) (*Entry, error) {
|
||||
}
|
||||
|
||||
func (l *LocalFsAdapter) Write(path string, content []byte) error {
|
||||
pathParts := strings.Split(strings.Trim(path, "/"), "/")
|
||||
if len(pathParts) == 0 || pathParts[0] == "" {
|
||||
return errors.New("invalid path")
|
||||
}
|
||||
|
||||
rootIdentifier := pathParts[0]
|
||||
var targetRoot string
|
||||
for _, r := range l.roots {
|
||||
if filepath.Base(r) == rootIdentifier {
|
||||
targetRoot = r
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if targetRoot == "" {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
subPath := filepath.Join(pathParts[1:]...)
|
||||
target := filepath.Join(targetRoot, subPath)
|
||||
|
||||
absTarget, err := filepath.Abs(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
absRoot, err := filepath.Abs(targetRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.HasPrefix(absTarget, absRoot) {
|
||||
return errors.New("path escapes root")
|
||||
}
|
||||
|
||||
dir := filepath.Dir(absTarget)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(absTarget, content, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Invalidate cache
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
currentPath := path
|
||||
for {
|
||||
delete(l.cache, currentPath)
|
||||
if currentPath == "/" {
|
||||
break
|
||||
}
|
||||
lastSlash := strings.LastIndex(currentPath, "/")
|
||||
if lastSlash <= 0 {
|
||||
currentPath = "/"
|
||||
} else {
|
||||
currentPath = currentPath[:lastSlash]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -10,6 +11,7 @@ type ErrorResponse struct {
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, code int, err error) {
|
||||
log.Printf("error: %s", err)
|
||||
writeJSON(w, code, ErrorResponse{Error: err.Error()})
|
||||
}
|
||||
|
||||
|
||||
@@ -2,14 +2,20 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.max-richter.dev/max/marka/renderer"
|
||||
"git.max-richter.dev/max/marka/server/internal/adapters"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
adapter adapters.FileAdapter
|
||||
apiKey string
|
||||
}
|
||||
|
||||
func (h *Handler) get(w http.ResponseWriter, target string) {
|
||||
@@ -44,7 +50,55 @@ func (h *Handler) get(w http.ResponseWriter, target string) {
|
||||
writeError(w, http.StatusInternalServerError, errors.New("unknown entry type"))
|
||||
}
|
||||
|
||||
func (h *Handler) post(w http.ResponseWriter, target string) {
|
||||
func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) {
|
||||
if h.apiKey != "" {
|
||||
if r.Header.Get("Authentication") != h.apiKey {
|
||||
writeError(w, http.StatusUnauthorized, errors.New("invalid api key"))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
writeError(w, http.StatusUnauthorized, errors.New("invalid api key"))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, fmt.Errorf("failed to decode body: %w", err))
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
isResource := strings.HasPrefix(contentType, "application/json") && strings.HasSuffix(target, ".md")
|
||||
|
||||
var contentToWrite []byte
|
||||
if isResource {
|
||||
renderedContent, err := renderer.RenderFile(body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to render file: %w", err))
|
||||
return
|
||||
}
|
||||
contentToWrite = renderedContent
|
||||
} else {
|
||||
contentToWrite = body
|
||||
}
|
||||
|
||||
if err := h.adapter.Write(target, contentToWrite); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to write file: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
if isResource {
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(body, &data); err != nil && strings.HasSuffix(target, ".md") {
|
||||
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to decode body: %w", err))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, data)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -55,18 +109,21 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
target := cleanURLLike(reqPath)
|
||||
|
||||
fmt.Printf("[serve] %s %s\n", r.Method, target)
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
h.get(w, target)
|
||||
case http.MethodPost:
|
||||
h.post(w, target)
|
||||
h.post(w, r, target)
|
||||
default:
|
||||
writeError(w, http.StatusMethodNotAllowed, errors.New("method not allowed"))
|
||||
}
|
||||
}
|
||||
|
||||
func NewHandler(adapter adapters.FileAdapter) http.Handler {
|
||||
func NewHandler(adapter adapters.FileAdapter, apiKey string) http.Handler {
|
||||
return &Handler{
|
||||
adapter: adapter,
|
||||
apiKey: apiKey,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user