Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed middleware to utils, added rfc7807 errors #5

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions endpoint/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
"github.com/charmixer/golang-api-template/app"

"github.com/charmixer/golang-api-template/endpoint"
// "github.com/charmixer/golang-api-template/middleware"

"github.com/rs/zerolog/log"
)

type GetDocsRequest struct {}
type GetDocsEndpoint struct {
endpoint.Endpoint
Request GetDocsRequest
//Response GetMetricsResponse
}
func (ep *GetDocsEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("http://%s:%d/docs/openapi?format=json", app.Env.Domain, app.Env.Port)
Expand Down Expand Up @@ -142,10 +139,6 @@ func NewGetDocsEndpoint() (endpoint.EndpointHandler) {
//Schema: GetHealthResponse{},
}},
}),

endpoint.WithMiddleware(

),
)

// Must be pointer to allow ServeHTTP method to be used with *Endpoint
Expand Down
36 changes: 20 additions & 16 deletions endpoint/docs/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/charmixer/golang-api-template/app"

"github.com/charmixer/golang-api-template/endpoint"
"github.com/charmixer/golang-api-template/middleware"
"github.com/charmixer/golang-api-template/endpoint/problem"

"go.opentelemetry.io/otel"

Expand All @@ -23,32 +23,40 @@ var (
)

type GetOpenapiRequest struct {
Format string `json:"format" oas-query:"format" oas-desc:"Format returned by the endpoint, eg. json"`
Format string `json:"format" oas-query:"format" query:"format" oas-desc:"Format returned by the endpoint, eg. json"`
}
type GetOpenapiResponse exporter.Openapi

// https://golang.org/doc/effective_go#embedding
type GetOpenapiEndpoint struct {
endpoint.Endpoint
Request GetOpenapiRequest
Response exporter.Openapi
responseType string
}
func (ep *GetOpenapiEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (ep GetOpenapiEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tr := otel.Tracer("request")
ctx, span := tr.Start(ctx, fmt.Sprintf("%s execution", r.URL.Path))
defer span.End()

t := r.URL.Query().Get("format")
request := GetOpenapiRequest{}
if err := endpoint.WithRequestQueryParser(ctx, r, &request); err != nil {
problem.MustWrite(w, err)
return
}

ep.Response = app.Env.OpenAPI
response := app.Env.OpenAPI

if t == "json" {
responseType := ""
if request.Format == "json" {
w.Header().Set("Content-Type", "application/json")
ep.responseType = "json"
responseType = "json"
} else {
w.Header().Set("Content-Type", "text/plain; application/yaml; charset=utf-8")
ep.responseType = "yaml"
responseType = "yaml"
}

if err := endpoint.WithResponseWriter(ctx, w, responseType, response); err != nil {
problem.MustWrite(w, err)
return
}
}

Expand Down Expand Up @@ -78,12 +86,8 @@ func NewGetOpenapiEndpoint() (endpoint.EndpointHandler) {
Schema: GetOpenapiEndpoint.BadRequest{},
}*/},
}),

endpoint.WithMiddleware(
middleware.WithResponseWriter(&ep.responseType, &ep.Response),
),
)

// Must be pointer to allow ServeHTTP method to be used with *Endpoint
return &ep
return ep
}
25 changes: 0 additions & 25 deletions endpoint/errors/errors.go

This file was deleted.

61 changes: 39 additions & 22 deletions endpoint/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/charmixer/oas/api"

"github.com/charmixer/golang-api-template/endpoint"
"github.com/charmixer/golang-api-template/middleware"
"github.com/charmixer/golang-api-template/endpoint/problem"

"go.opentelemetry.io/otel"
)
Expand All @@ -19,26 +19,46 @@ var (

type GetHealthRequest struct {}
type GetHealthResponse struct {
Alive bool `json:"alive_json" oas-desc:"Tells if bla"`
Ready bool `json:"ready_json"`
Alive bool `json:"alive_json" oas-desc:"Tells if the service is alive (ping)"`
Ready bool `json:"ready_json" oas-desc:"Tells if the service is ready to accept requests"`
}

// https://golang.org/doc/effective_go#embedding
type GetHealthEndpoint struct {
endpoint.Endpoint
Request GetHealthRequest
Response GetHealthResponse
}
func (ep *GetHealthEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (ep GetHealthEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tr := otel.Tracer("request")
ctx, span := tr.Start(ctx, fmt.Sprintf("%s execution", r.URL.Path))
defer span.End()

w.Header().Set("Content-Type", "application/json")
request := GetHealthRequest{}
if err := endpoint.WithRequestQueryParser(ctx, r, &request); err != nil {
problem.MustWrite(w, err)
return
}

if err := endpoint.WithRequestValidation(ctx, &request); err != nil {
problem.MustWrite(w, err)
return
}

ep.Response = GetHealthResponse{
response := GetHealthResponse{
Alive: true,
Ready: true,
}

w.Header().Set("Content-Type", "application/json")

if err := endpoint.WithResponseValidation(ctx, response); err != nil {
problem.MustWrite(w, err)
return
}

if err := endpoint.WithJsonResponseWriter(ctx, w, response); err != nil {
problem.MustWrite(w, err)
return
}
}

Expand All @@ -47,29 +67,26 @@ func NewGetHealthEndpoint() (endpoint.EndpointHandler) {

ep.Setup(
endpoint.WithSpecification(api.Path{
Summary: "Test 2",
Description: `Testing 2`,
Summary: "Get health information about the service",
Description: ``,
Tags: OPENAPI_TAGS,

Request: api.Request{
Description: `Testing Request`,
Description: ``,
Schema: GetHealthRequest{},
},

Responses: []api.Response{{
Description: `Testing OK Response`,
Code: 200,
Description: http.StatusText(http.StatusOK),
Code: http.StatusOK,
Schema: GetHealthResponse{},
}},
},/*{
Description: http.StatusText(http.StatusBadRequest),
Code: http.StatusBadRequest,
Schema: problem.ProblemDetails{}, // TODO fix oas to work with: problem.ValidationError{},
}*/},
}),

endpoint.WithMiddleware(
middleware.WithResponseValidation(&ep.Response),
middleware.WithJsonResponseWriter(&ep.Response),
),

)

// Must be pointer to allow ServeHTTP method to be used with *Endpoint
return &ep
return ep
}
10 changes: 2 additions & 8 deletions endpoint/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ type GetMetricsResponse struct {}
// https://golang.org/doc/effective_go#embedding
type GetMetricsEndpoint struct {
endpoint.Endpoint
Request GetMetricsRequest
Response GetMetricsResponse
}
func (ep *GetMetricsEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (ep GetMetricsEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).(http.HandlerFunc)
t.ServeHTTP(w, r)
}
Expand All @@ -52,12 +50,8 @@ func NewGetMetricsEndpoint() (endpoint.EndpointHandler) {
//Schema: GetMetricsResponse{},
}},
}),

endpoint.WithMiddleware(

),
)

// Must be pointer to allow ServeHTTP method to be used with *Endpoint
return &ep
return ep
}
Loading