From 773810957fbdb805eff1258bf032ebfe7302106f Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Fri, 25 Aug 2023 23:30:39 -0600 Subject: [PATCH] basic test to spit the json back out --- .gitignore | 1 + go.mod | 5 ++++ go.sum | 2 ++ main.go | 55 ++++++++++++++++++++++++++++++++++++++++++++ templates/error.html | 3 +++ 5 files changed, 66 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 templates/error.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..261bf30 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +memnarch diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..611b1c1 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module forge.lightcrystal.systems/lightcrystal/memnarch + +go 1.20 + +require hacklab.nilfm.cc/quartzgun v0.3.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..be61828 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +hacklab.nilfm.cc/quartzgun v0.3.2 h1:PmRFZ/IgsXVWyNn1iOsQ/ZeMnOQIQy0PzFakhXBdZoU= +hacklab.nilfm.cc/quartzgun v0.3.2/go.mod h1:P6qK4HB0CD/xfyRq8wdEGevAPFDDmv0KCaESSvv93LU= diff --git a/main.go b/main.go new file mode 100644 index 0000000..99eb542 --- /dev/null +++ b/main.go @@ -0,0 +1,55 @@ +package main + +import ( + + "encoding/json" + "fmt" + "html/template" + "os" + "net/http" + + "hacklab.nilfm.cc/quartzgun/renderer" + "hacklab.nilfm.cc/quartzgun/router" + . "hacklab.nilfm.cc/quartzgun/util" + +) + +func testPayload(next http.Handler) http.Handler { + handler := func(w http.ResponseWriter, req *http.Request) { + data := &map[string]interface{}{} + + err := json.NewDecoder(req.Body).Decode(data) + + if err == nil { + AddContextValue(req, "data", data) + } else { + AddContextValue(req, "data", err.Error()) + } + + next.ServeHTTP(w, req) + } + + return http.HandlerFunc(handler) +} + +func run(args []string) error { + rtr := &router.Router{ + Fallback: *template.Must(template.ParseFiles("templates/error.html")), + } + + rtr.Post("/api/test", testPayload(renderer.JSON("data"))) + + + http.ListenAndServe(":9999", rtr); + return nil +} + +func main () { + err := run(os.Args) + if err == nil { + os.Exit(0) + } else { + fmt.Println(err.Error()) + os.Exit(1) + } +} \ No newline at end of file diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..f15ccb1 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,3 @@ +

500

+ +

ya done fucked up kid

\ No newline at end of file