main.go (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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) } } |