This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
261 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"encoding/json" | ||
|
||
"log" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/scoiatael/archai/http" | ||
) | ||
|
||
type HttpServer struct { | ||
Addr string | ||
Port int | ||
} | ||
|
||
func (hs HttpServer) Run(c Context) error { | ||
handler := c.HttpHandler() | ||
handler.Get("/stream/:id", func(ctx http.GetContext) { | ||
stream := ctx.GetSegment(":id") | ||
action := ReadEvents{Stream: stream} | ||
action.Cursor = ctx.StringParam("stream") | ||
action.Amount = ctx.IntParam("amount", 10) | ||
err := action.Run(c) | ||
if err != nil { | ||
c.HandleErr(err) | ||
ctx.ServerErr(err) | ||
} else { | ||
events := make([]map[string]interface{}, len(action.Events)) | ||
for i, ev := range action.Events { | ||
events[i] = make(map[string]interface{}) | ||
events[i]["ID"] = ev.ID | ||
payload := make(map[string]interface{}) | ||
err := json.Unmarshal(ev.Blob, &payload) | ||
if err != nil { | ||
c.HandleErr(err) | ||
ctx.ServerErr(err) | ||
} | ||
events[i]["blob"] = payload | ||
} | ||
ctx.SendJson(events) | ||
} | ||
}) | ||
handler.Post("/stream/:id", func(ctx http.PostContext) { | ||
var err error | ||
stream := ctx.GetSegment(":id") | ||
body, err := ctx.JsonBodyParams() | ||
if err != nil { | ||
// Error was already send | ||
return | ||
} | ||
log.Println("Got ", body) | ||
payload, err := json.Marshal(body) | ||
if err != nil { | ||
c.HandleErr(err) | ||
ctx.ServerErr(err) | ||
return | ||
} | ||
action := WriteEvent{Stream: stream, Payload: payload, Meta: make(map[string]string)} | ||
action.Meta["origin"] = "http" | ||
action.Meta["compressed"] = "false" | ||
err = action.Run(c) | ||
if err != nil { | ||
c.HandleErr(err) | ||
ctx.ServerErr(err) | ||
} else { | ||
ctx.SendJson("OK") | ||
} | ||
}) | ||
connString := fmt.Sprintf("%s:%d", hs.Addr, hs.Port) | ||
return errors.Wrap(handler.Run(connString), "HttpServer starting..") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package http | ||
|
||
type Context interface { | ||
HandleErr(error) | ||
} | ||
|
||
type HttpContext interface { | ||
SendJson(interface{}) | ||
GetSegment(string) string | ||
ServerErr(error) | ||
} | ||
|
||
type GetContext interface { | ||
HttpContext | ||
StringParam(string) string | ||
IntParam(string, int) int | ||
} | ||
|
||
type PostContext interface { | ||
HttpContext | ||
JsonBodyParams() (map[string]interface{}, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"fmt" | ||
|
||
"strings" | ||
|
||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
type FastHttpContext struct { | ||
*fasthttp.RequestCtx | ||
Context Context | ||
} | ||
|
||
func (hc FastHttpContext) ServerErr(err error) { | ||
hc.Context.HandleErr(err) | ||
hc.Error(fmt.Sprintf(`{ "error": "%v" }`, err), fasthttp.StatusInternalServerError) | ||
} | ||
|
||
func (hc FastHttpContext) SendJson(response interface{}) { | ||
dump, err := json.Marshal(response) | ||
if err != nil { | ||
hc.ServerErr(err) | ||
} else { | ||
hc.SetBody(dump) | ||
} | ||
} | ||
|
||
// TODO: Do this normal way | ||
func (hc FastHttpContext) GetSegment(index string) string { | ||
segments := strings.Split(string(hc.Path()), "/") | ||
if len(segments) == 0 { | ||
return "" | ||
} else { | ||
return segments[len(segments)-1] | ||
} | ||
} | ||
|
||
type FastHttpGetContext struct { | ||
FastHttpContext | ||
} | ||
|
||
func (gc FastHttpGetContext) StringParam(name string) string { | ||
val := gc.QueryArgs().Peek(name) | ||
return string(val) | ||
} | ||
|
||
func (gc FastHttpGetContext) IntParam(name string, def int) int { | ||
val := gc.QueryArgs().Peek(name) | ||
i, err := strconv.Atoi(string(val)) | ||
if err != nil { | ||
return def | ||
} | ||
return i | ||
} | ||
|
||
type FastHttpPostContext struct { | ||
FastHttpContext | ||
} | ||
|
||
const expectedJSON = `{ "error": "expected JSON body" }` | ||
|
||
func (pc FastHttpPostContext) JsonBodyParams() (map[string]interface{}, error) { | ||
body := pc.PostBody() | ||
read := make(map[string]interface{}) | ||
err := json.Unmarshal(body, &read) | ||
if err != nil { | ||
pc.Error(expectedJSON, fasthttp.StatusBadRequest) | ||
} | ||
return read, err | ||
} | ||
|
||
// TODO: Add routing ;) | ||
type FastHttpHandlers struct { | ||
POST func(PostContext) | ||
GET func(GetContext) | ||
} | ||
|
||
type FastHttpHandler struct { | ||
handlers FastHttpHandlers | ||
Context Context | ||
} | ||
|
||
func (h *FastHttpHandler) Get(path string, handler func(GetContext)) { | ||
h.handlers.GET = handler | ||
} | ||
|
||
func (h *FastHttpHandler) Post(path string, handler func(PostContext)) { | ||
h.handlers.POST = handler | ||
} | ||
|
||
const ( | ||
methodNotAllowed = `{ "error": "method not allowed" }` | ||
contentType = `application/json` | ||
) | ||
|
||
func (h *FastHttpHandler) compile() func(*fasthttp.RequestCtx) { | ||
return func(ctx *fasthttp.RequestCtx) { | ||
ctx.SetContentType(contentType) | ||
httpCtx := FastHttpContext{ctx, h.Context} | ||
if ctx.IsPost() { | ||
h.handlers.POST(FastHttpPostContext{httpCtx}) | ||
} else if ctx.IsGet() { | ||
h.handlers.GET(FastHttpGetContext{httpCtx}) | ||
} else { | ||
ctx.Error(methodNotAllowed, fasthttp.StatusMethodNotAllowed) | ||
} | ||
} | ||
} | ||
|
||
func (h *FastHttpHandler) Run(addr string) error { | ||
return errors.Wrap(fasthttp.ListenAndServe(addr, h.compile()), "Starting fasthttp server") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters