Compare commits
11 Commits
6c47c8c3e9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
137e3da6d4
|
||
|
|
148bc2c15c
|
||
|
|
cc19724277
|
||
|
|
851babe1e0
|
||
|
|
f14b685e43
|
||
|
|
fd6aeb8a27
|
||
|
|
4bfd2b9450
|
||
|
|
dfc38e2c86
|
||
|
|
3fe7577250
|
||
|
|
3cd320637c
|
||
|
|
7c5f9f7829
|
@@ -38,5 +38,4 @@
|
||||
---
|
||||
|
||||
# { headline }
|
||||
|
||||
{ articleBody }
|
||||
|
||||
@@ -36,7 +36,6 @@
|
||||
---
|
||||
|
||||
# { name | text }
|
||||
|
||||
{ description | text }
|
||||
|
||||
## Ingredients
|
||||
|
||||
@@ -37,5 +37,4 @@
|
||||
---
|
||||
|
||||
# { itemReviewed.name }
|
||||
|
||||
{ reviewBody }
|
||||
|
||||
@@ -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"
|
||||
@@ -62,6 +63,7 @@ func main() {
|
||||
must(err)
|
||||
|
||||
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, ", "))
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -62,16 +63,16 @@ func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) {
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err)
|
||||
writeError(w, http.StatusBadRequest, fmt.Errorf("failed to decode body: %w", err))
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
isJSON := strings.HasPrefix(contentType, "application/json")
|
||||
isResource := strings.HasPrefix(contentType, "application/json") && strings.HasSuffix(target, ".md")
|
||||
|
||||
var contentToWrite []byte
|
||||
if strings.HasSuffix(target, ".md") && isJSON {
|
||||
if isResource {
|
||||
renderedContent, err := renderer.RenderFile(body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to render file: %w", err))
|
||||
@@ -87,7 +88,17 @@ func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) {
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
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) {
|
||||
@@ -98,6 +109,8 @@ 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)
|
||||
|
||||
Reference in New Issue
Block a user