basic test to spit the json back out
This commit is contained in:
parent
f72bab1b10
commit
773810957f
5 changed files with 66 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
memnarch
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module forge.lightcrystal.systems/lightcrystal/memnarch
|
||||
|
||||
go 1.20
|
||||
|
||||
require hacklab.nilfm.cc/quartzgun v0.3.2
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
55
main.go
Normal file
55
main.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
3
templates/error.html
Normal file
3
templates/error.html
Normal file
|
@ -0,0 +1,3 @@
|
|||
<h1>500</h1>
|
||||
|
||||
<p>ya done fucked up kid</p>
|
Loading…
Reference in a new issue