Skip to content

Commit

Permalink
net: add WriteHeader to flatend microservice api
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 15, 2020
1 parent 7e98524 commit 210581c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type Context struct {
written bool // written before?
}

func (c *Context) WriteHeader(key, val string) {
c.headers[key] = val
}

func (c *Context) Write(data []byte) (int, error) {
if !c.written {
packet := ServiceResponsePacket{
Expand Down Expand Up @@ -97,6 +101,9 @@ func acquireContext(headers map[string]string, body io.ReadCloser, id uint32, co

func releaseContext(ctx *Context) {
ctx.written = false
for key := range ctx.headers {
delete(ctx.headers, key)
}
contextPool.Put(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function main() {
const node = new Node();

node.register("get_todos", (ctx: Context) => ctx.json(ctx.headers));
node.register("all_todos", (ctx: Context) => fs.createReadStream("tsconfig.json").pipe(ctx));
node.register("all_todos", (ctx: Context) => fs.createReadStream("package.json").pipe(ctx.header('content-type', 'application/json')));

node.register('pipe', async (ctx: Context) => {
const body = await ctx.body({limit: 65536});
Expand Down

0 comments on commit 210581c

Please sign in to comment.