Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Use Iris for HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
scoiatael committed Feb 28, 2017
1 parent 0b4545a commit 63ff19a
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.o
*.a
*.so
archai

# Folders
_obj
Expand All @@ -23,4 +24,4 @@ _testmain.go
*.test
*.prof

vendor
vendor
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
run: archai
./archai


archai: $(shell find . -type f -regex .*go$)
go build .
14 changes: 5 additions & 9 deletions actions/http_server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package actions

import (
"fmt"

"encoding/json"

"log"
"fmt"

"github.com/pkg/errors"
"github.com/scoiatael/archai/http"
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions http/fast_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package http

import (
"encoding/json"

"fmt"

"strings"

"strconv"
"strings"

"github.com/pkg/errors"
"github.com/valyala/fasthttp"
Expand Down
77 changes: 77 additions & 0 deletions http/iris.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions http/new.go
Original file line number Diff line number Diff line change
@@ -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}
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
File renamed without changes.

0 comments on commit 63ff19a

Please sign in to comment.