Skip to content

Commit

Permalink
story(issue-298): endpoint move to explicit request and response inte…
Browse files Browse the repository at this point in the history
…rfaces (#299)

* refactor(issue-298): implement request and response interfaces and enforce them on the operation

* refactor(issue-298): update tests and examples

* chore(docs): updated coverage badge.

* test(issue-298): make noop handler generic over request and response

* test(issue-298): added cases for consumes json

* refactor(issue-298): update examples to use consumes and produces json helpers

* test(issue-298): added cases for produces json

* chore(docs): updated coverage badge.

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
Zaba505 and actions-user authored Oct 6, 2024
1 parent ea93de3 commit 8964de3
Show file tree
Hide file tree
Showing 14 changed files with 702 additions and 220 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/z5labs/bedrock.svg)](https://pkg.go.dev/github.com/z5labs/bedrock)
[![Go Report Card](https://goreportcard.com/badge/github.com/z5labs/bedrock)](https://goreportcard.com/report/github.com/z5labs/bedrock)
![Coverage](https://img.shields.io/badge/Coverage-96.5%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-95.6%25-brightgreen)
[![build](https://github.com/z5labs/bedrock/actions/workflows/build.yaml/badge.svg)](https://github.com/z5labs/bedrock/actions/workflows/build.yaml)

**bedrock provides a minimal, modular and composable foundation for
Expand Down
29 changes: 9 additions & 20 deletions example/custom_framework/echo/endpoint/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package endpoint

import (
"context"
"encoding/json"
"log/slog"
"net/http"

"github.com/z5labs/bedrock/example/custom_framework/framework/rest"

"github.com/z5labs/bedrock/rest/endpoint"
)

type echoHandler struct {
Expand All @@ -24,36 +25,24 @@ func Echo(log *slog.Logger) rest.Endpoint {
}

return rest.Endpoint{
Method: http.MethodPost,
Path: "/echo",
Operation: rest.NewOperation(h),
Method: http.MethodPost,
Path: "/echo",
Operation: rest.NewOperation(
endpoint.ConsumesJson(
endpoint.ProducesJson(h),
),
),
}
}

type EchoRequest struct {
Msg string `json:"msg"`
}

func (EchoRequest) ContentType() string {
return "application/json"
}

func (req *EchoRequest) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, req)
}

type EchoResponse struct {
Msg string `json:"msg"`
}

func (EchoResponse) ContentType() string {
return "application/json"
}

func (resp *EchoResponse) MarshalBinary() ([]byte, error) {
return json.Marshal(resp)
}

func (h *echoHandler) Handle(ctx context.Context, req *EchoRequest) (*EchoResponse, error) {
h.log.InfoContext(ctx, "echoing back received message to client", slog.String("echo_msg", req.Msg))
resp := &EchoResponse{Msg: req.Msg}
Expand Down
4 changes: 2 additions & 2 deletions example/custom_framework/framework/rest/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Header(name string, required bool, pattern string) OperationOption {
}
}

func NewOperation[Req, Resp any](h OperationHandler[Req, Resp], opts ...OperationOption) rest.Operation {
func NewOperation[I, O any, Req endpoint.Request[I], Resp endpoint.Response[O]](h OperationHandler[I, O], opts ...OperationOption) rest.Operation {
opOpts := &operationConfig{}
for _, opt := range opts {
opt(opOpts)
Expand All @@ -41,5 +41,5 @@ func NewOperation[Req, Resp any](h OperationHandler[Req, Resp], opts ...Operatio
endpointOpts = append(endpointOpts, endpoint.Headers(opOpts.headers...))
}

return endpoint.NewOperation(h, endpointOpts...)
return endpoint.NewOperation[I, O, Req, Resp](h, endpointOpts...)
}
4 changes: 3 additions & 1 deletion example/simple_rest/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func Init(ctx context.Context, cfg Config) (bedrock.App, error) {
http.MethodPost,
"/echo",
endpoint.NewOperation(
echoService,
endpoint.ConsumesJson(
endpoint.ProducesJson(echoService),
),
endpoint.Headers(
endpoint.Header{
Name: "Authorization",
Expand Down
18 changes: 0 additions & 18 deletions example/simple_rest/echo/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,10 @@

package echo

import "encoding/json"

type Request struct {
Msg string `json:"msg"`
}

func (Request) ContentType() string {
return "application/json"
}

func (req *Request) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, req)
}

type Response struct {
Msg string `json:"msg"`
}

func (Response) ContentType() string {
return "application/json"
}

func (resp *Response) MarshalBinary() ([]byte, error) {
return json.Marshal(resp)
}
Loading

0 comments on commit 8964de3

Please sign in to comment.