memnarch/main.go

55 lines
1,013 B
Go
Raw Normal View History

2023-08-26 05:30:39 +00:00
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)
}
}