From 63ff19ad4be10535537742dafdbcee9e67714003 Mon Sep 17 00:00:00 2001 From: scoiatael Date: Tue, 28 Feb 2017 13:16:02 +0100 Subject: [PATCH] Use Iris for HTTP --- .gitignore | 3 +- Makefile | 6 ++ actions/http_server.go | 14 ++-- config.go | 3 +- http/fast_http.go | 5 +- http/iris.go | 77 ++++++++++++++++++++++ http/new.go | 26 ++++++++ main.go | 3 +- scripts/{run_cassandra.sh => cassandra.sh} | 0 9 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 Makefile create mode 100644 http/iris.go create mode 100644 http/new.go rename scripts/{run_cassandra.sh => cassandra.sh} (100%) diff --git a/.gitignore b/.gitignore index e302ce5..490aac4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.o *.a *.so +archai # Folders _obj @@ -23,4 +24,4 @@ _testmain.go *.test *.prof -vendor \ No newline at end of file +vendor diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f739f8 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +run: archai + ./archai + + +archai: $(shell find . -type f -regex .*go$) + go build . diff --git a/actions/http_server.go b/actions/http_server.go index b16d3e2..1b17292 100644 --- a/actions/http_server.go +++ b/actions/http_server.go @@ -1,11 +1,8 @@ package actions import ( - "fmt" - "encoding/json" - - "log" + "fmt" "github.com/pkg/errors" "github.com/scoiatael/archai/http" @@ -19,9 +16,9 @@ type HttpServer struct { func (hs HttpServer) Run(c Context) error { handler := c.HttpHandler() handler.Get("/stream/:id", func(ctx http.GetContext) { - stream := ctx.GetSegment(":id") + stream := ctx.GetSegment("id") action := ReadEvents{Stream: stream} - action.Cursor = ctx.StringParam("stream") + action.Cursor = ctx.StringParam("cursor") action.Amount = ctx.IntParam("amount", 10) err := action.Run(c) if err != nil { @@ -45,13 +42,12 @@ func (hs HttpServer) Run(c Context) error { }) handler.Post("/stream/:id", func(ctx http.PostContext) { var err error - stream := ctx.GetSegment(":id") + stream := ctx.GetSegment("id") body, err := ctx.JsonBodyParams() if err != nil { - // Error was already send + // Error was already sent return } - log.Println("Got ", body) payload, err := json.Marshal(body) if err != nil { c.HandleErr(err) diff --git a/config.go b/config.go index 3f27f0b..58ef779 100644 --- a/config.go +++ b/config.go @@ -17,7 +17,6 @@ type Config struct { func (c Config) HandleErr(err error) { log.Print(err) - panic(err) } func (c Config) Migrations() map[string]persistence.Migration { @@ -37,7 +36,7 @@ func (c Config) Version() string { } func (c Config) HttpHandler() actions.HttpHandler { - return &http.FastHttpHandler{Context: c} + return http.NewIris(c) } func (c Config) Run() error { diff --git a/http/fast_http.go b/http/fast_http.go index 8a3aee6..d31ecbd 100644 --- a/http/fast_http.go +++ b/http/fast_http.go @@ -2,12 +2,9 @@ package http import ( "encoding/json" - "fmt" - - "strings" - "strconv" + "strings" "github.com/pkg/errors" "github.com/valyala/fasthttp" diff --git a/http/iris.go b/http/iris.go new file mode 100644 index 0000000..6bc6efa --- /dev/null +++ b/http/iris.go @@ -0,0 +1,77 @@ +package http + +import ( + "gopkg.in/kataras/iris.v6" +) + +type IrisHttpContext struct { + *iris.Context + context Context +} + +func (hc IrisHttpContext) SendJson(response interface{}) { + hc.JSON(iris.StatusOK, response) +} + +func (hc IrisHttpContext) ServerErr(err error) { + hc.context.HandleErr(err) + hc.JSON(iris.StatusInternalServerError, iris.Map{"error": err}) +} + +func (hc IrisHttpContext) GetSegment(index string) string { + return hc.Param(index) +} + +type IrisGetContext struct { + IrisHttpContext +} + +func (hc IrisHttpContext) StringParam(index string) string { + return hc.Param(index) +} + +func (hc IrisHttpContext) IntParam(index string, def int) int { + val, err := hc.ParamInt(index) + if err != nil { + return def + } + + return val +} + +type IrisPostContext struct { + IrisHttpContext +} + +func (hc IrisPostContext) JsonBodyParams() (map[string]interface{}, error) { + sess := iris.Map{} + err := hc.ReadJSON(&sess) + + if err != nil { + hc.JSON(iris.StatusBadRequest, iris.Map{"error": "expected JSON body"}) + } + return sess, err +} + +type IrisHandler struct { + framework *iris.Framework + context Context +} + +func (h *IrisHandler) Get(path string, handler func(GetContext)) { + h.framework.Get(path, func(ctx *iris.Context) { + handler(IrisGetContext{IrisHttpContext{ctx, h.context}}) + }) +} + +func (h *IrisHandler) Post(path string, handler func(PostContext)) { + h.framework.Post(path, func(ctx *iris.Context) { + handler(IrisPostContext{IrisHttpContext{ctx, h.context}}) + }) +} + +func (h *IrisHandler) Run(addr string) error { + h.framework.Listen(addr) + + return nil +} diff --git a/http/new.go b/http/new.go new file mode 100644 index 0000000..41f94a5 --- /dev/null +++ b/http/new.go @@ -0,0 +1,26 @@ +package http + +import ( + "gopkg.in/kataras/iris.v6" + "gopkg.in/kataras/iris.v6/adaptors/httprouter" +) + +func NewIris(c Context) *IrisHandler { + handler := IrisHandler{} + handler.context = c + + app := iris.New() + app.Adapt( + // adapt a logger which prints all errors to the os.Stdout + iris.DevLogger(), + // adapt the adaptors/httprouter or adaptors/gorillamux + httprouter.New(), + ) + + handler.framework = app + return &handler +} + +func NewFastHttp(c Context) *FastHttpHandler { + return &FastHttpHandler{Context: c} +} diff --git a/main.go b/main.go index b009432..49d80b1 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,10 @@ func main() { app.Action = func(c *cli.Context) error { config := Config{Keyspace: "archai_test", Hosts: []string{"127.0.0.1"}} config.Actions = []actions.Action{ + actions.Migrate{}, //actions.WriteEventFromStream{Stream: "test-stream", Input: os.Stdin}, actions.ReadEventsToStream{Stream: "test-stream", Output: os.Stdout}, - actions.HttpServer{Port: 8080}, + actions.HttpServer{Port: 8080, Addr: "127.0.0.1"}, } return config.Run() } diff --git a/scripts/run_cassandra.sh b/scripts/cassandra.sh similarity index 100% rename from scripts/run_cassandra.sh rename to scripts/cassandra.sh