convert to real micro server #3

Closed
opened 2021-01-31 01:31:53 +03:00 by vtolstov · 2 comments
vtolstov commented 2021-01-31 01:31:53 +03:00 (Migrated from github.com)

via helper funcs we already have endpoint metadata, that allows to convert to real micro server

  • protoc-gen-micro generates with pure net/http server handler and util/router stuff matches for path based on method
  • matches contains pattern and handler
  • if matched - fill handler request struct, run micro wrappers and pass to handler
  • generate stubs http handlers for each endpoint
  • unmarshal via codec request to needed struct
  • pass struct to handler
  • marshal via codec response and write with proper status
  • done
via helper funcs we already have endpoint metadata, that allows to convert to real micro server * protoc-gen-micro generates with pure net/http server handler and util/router stuff matches for path based on method * matches contains pattern and handler * if matched - fill handler request struct, run micro wrappers and pass to handler * generate stubs http handlers for each endpoint * unmarshal via codec request to needed struct * pass struct to handler * marshal via codec response and write with proper status * done
vtolstov commented 2021-02-02 14:48:16 +03:00 (Migrated from github.com)
package main

import (
	"context"
	"fmt"
)

type Request struct{}
type Response struct{}

type rspCodeKey struct{}

type rspCodeVal struct {
	code int
}

func SetRspCode(ctx context.Context, code int) {
	if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok {
		rsp.code = code
	}
}

func getRspCode(ctx context.Context) int {
	var code int
	if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok {
		code = rsp.code
	}
	return code
}

func handler(ctx context.Context, req *Request, rsp *Response) error {
	SetRspCode(ctx, 200)
	return nil
}

func main() {
	ctx := context.WithValue(context.Background(), rspCodeKey{}, &rspCodeVal{})
	err := handler(ctx, &Request{}, &Response{})
	if err != nil {
		panic(err)
	}
	fmt.Printf("code: %d", GetRspCode(ctx))
}
```go package main import ( "context" "fmt" ) type Request struct{} type Response struct{} type rspCodeKey struct{} type rspCodeVal struct { code int } func SetRspCode(ctx context.Context, code int) { if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok { rsp.code = code } } func getRspCode(ctx context.Context) int { var code int if rsp, ok := ctx.Value(rspCodeKey{}).(*rspCodeVal); ok { code = rsp.code } return code } func handler(ctx context.Context, req *Request, rsp *Response) error { SetRspCode(ctx, 200) return nil } func main() { ctx := context.WithValue(context.Background(), rspCodeKey{}, &rspCodeVal{}) err := handler(ctx, &Request{}, &Response{}) if err != nil { panic(err) } fmt.Printf("code: %d", GetRspCode(ctx)) } ```
vtolstov commented 2021-02-07 18:54:45 +03:00 (Migrated from github.com)

done

done
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: unistack-org/micro-server-http#3
No description provided.