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 }
|
# { headline }
|
||||||
|
|
||||||
{ articleBody }
|
{ articleBody }
|
||||||
|
|||||||
@@ -36,7 +36,6 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
# { name | text }
|
# { name | text }
|
||||||
|
|
||||||
{ description | text }
|
{ description | text }
|
||||||
|
|
||||||
## Ingredients
|
## Ingredients
|
||||||
|
|||||||
@@ -37,5 +37,4 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
# { itemReviewed.name }
|
# { itemReviewed.name }
|
||||||
|
|
||||||
{ reviewBody }
|
{ reviewBody }
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ cmd = "go build -o ./tmp/marka-server ./cmd/marka-server"
|
|||||||
bin = "./tmp/marka-server"
|
bin = "./tmp/marka-server"
|
||||||
|
|
||||||
# Command to run the application with arguments.
|
# 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.
|
# Watch these file extensions.
|
||||||
include_ext = ["go", "http"]
|
include_ext = ["go", "http"]
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -62,6 +63,7 @@ func main() {
|
|||||||
must(err)
|
must(err)
|
||||||
|
|
||||||
apiKey := os.Getenv("MARKA_API_KEY")
|
apiKey := os.Getenv("MARKA_API_KEY")
|
||||||
|
fmt.Println(apiKey)
|
||||||
http.Handle("/", handler.NewHandler(fsAdapter, apiKey))
|
http.Handle("/", handler.NewHandler(fsAdapter, apiKey))
|
||||||
|
|
||||||
log.Printf("listening on %s, roots=%s", *addr, strings.Join(absRoots, ", "))
|
log.Printf("listening on %s, roots=%s", *addr, strings.Join(absRoots, ", "))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -62,16 +63,16 @@ func (h *Handler) post(w http.ResponseWriter, r *http.Request, target string) {
|
|||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusBadRequest, err)
|
writeError(w, http.StatusBadRequest, fmt.Errorf("failed to decode body: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
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
|
var contentToWrite []byte
|
||||||
if strings.HasSuffix(target, ".md") && isJSON {
|
if isResource {
|
||||||
renderedContent, err := renderer.RenderFile(body)
|
renderedContent, err := renderer.RenderFile(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusInternalServerError, fmt.Errorf("failed to render file: %w", err))
|
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
|
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) {
|
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)
|
target := cleanURLLike(reqPath)
|
||||||
|
|
||||||
|
fmt.Printf("[serve] %s %s\n", r.Method, target)
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
h.get(w, target)
|
h.get(w, target)
|
||||||
|
|||||||
Reference in New Issue
Block a user