Skip to content

Commit

Permalink
feat: only return registr token against localhost, add descriptions t…
Browse files Browse the repository at this point in the history
…o api docs (#413)

Signed-off-by: Sarah Funkhouser <[email protected]>
  • Loading branch information
golanglemonade authored Jan 26, 2025
1 parent e46d0e2 commit 7f94413
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 72 deletions.
3 changes: 3 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func serve(ctx context.Context) error {
// Add redis client to Handlers Config
so.Config.Handler.RedisClient = redisClient

// set dev flag
so.Config.Handler.IsDev = so.Config.Settings.Server.Dev

// add ready checks
so.AddServerOptions(
serveropts.WithReadyChecks(dbClient.Config, fgaClient, redisClient, dbClient.Job),
Expand Down
2 changes: 2 additions & 0 deletions internal/httpserve/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
type Handler struct {
// IsTest is a flag to determine if the application is running in test mode and will mock external calls
IsTest bool
// IsDev is a flag to determine if the application is running in development mode
IsDev bool
// DBClient to interact with the database
DBClient *ent.Client
// RedisClient to interact with redis
Expand Down
6 changes: 5 additions & 1 deletion internal/httpserve/handlers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ func (h *Handler) RegisterHandler(ctx echo.Context) error {
ID: meowuser.ID,
Email: meowuser.Email,
Message: "Welcome to Openlane!",
Token: meowtoken.Token,
}

// only return the token in development
if h.IsDev {
out.Token = meowtoken.Token
}

return h.Created(ctx, out)
Expand Down
2 changes: 1 addition & 1 deletion internal/httpserve/handlers/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (suite *HandlerTestSuite) TestVerifyHandler() {
email: "[email protected]",
tokenSet: true,
ttl: expiredTTL,
expectedMessage: "Token expired, a new token has been issued. Please try again",
expectedMessage: "Token expired, a new token has been issued. Please check your email and try again.",
expectedStatus: http.StatusCreated,
},
}
Expand Down
3 changes: 1 addition & 2 deletions internal/httpserve/handlers/verifyemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func (h *Handler) VerifyEmail(ctx echo.Context) error {
Reply: rout.Reply{Success: false},
ID: meowtoken.ID,
Email: user.Email,
Message: "Token expired, a new token has been issued. Please try again.",
Token: meowtoken.Token,
Message: "Token expired, a new token has been issued. Please check your email and try again.",
}

return h.Created(ctx, out)
Expand Down
23 changes: 22 additions & 1 deletion internal/httpserve/server/openapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"reflect"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"

Expand All @@ -18,7 +20,7 @@ func NewOpenAPISpec() (*openapi3.T, error) {
securityschemes := make(openapi3.SecuritySchemes)
examples := make(openapi3.Examples)

generator := openapi3gen.NewGenerator(openapi3gen.UseAllExportedFields())
generator := openapi3gen.NewGenerator(openapi3gen.UseAllExportedFields(), customizer)
for key, val := range openAPISchemas {
ref, err := generator.NewSchemaRefForValue(val, schemas)
if err != nil {
Expand Down Expand Up @@ -160,6 +162,25 @@ func NewOpenAPISpec() (*openapi3.T, error) {
}, nil
}

// customizer is a customizer function that allows for the modification of the generated schemas
// this is used to ignore fields that are not required in the OAS specification
// and to add additional metadata to the schema such as descriptions and examples
var customizer = openapi3gen.SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error {
if tag.Get("exclude") != "" && tag.Get("exclude") == "true" {
return &openapi3gen.ExcludeSchemaSentinel{}
}

if tag.Get("description") != "" {
schema.Description = tag.Get("description")
}

if tag.Get("example") != "" {
schema.Example = tag.Get("example")
}

return nil
})

// openAPISchemas is a mapping of types to auto generate schemas for - these specifically live under the OAS "schema" type so that we can simply make schemaRef's to them and not have to define them all individually in the OAS paths
var openAPISchemas = map[string]any{
"LoginRequest": &models.LoginRequest{},
Expand Down
Loading

0 comments on commit 7f94413

Please sign in to comment.