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

Update engine.go #352

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 43 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,42 @@
}

type OpenAPIConfig struct {
MiddlewareDisplayLimit int
}

func DefaultOpenAPIConfig() OpenAPIConfig {
return OpenAPIConfig{
MiddlewareDisplayLimit: 5,
}
// Local path to save the OpenAPI JSON spec
JSONFilePath string

Check failure on line 54 in engine.go

View workflow job for this annotation

GitHub Actions / tests

syntax error: unexpected name string at end of statement

Check failure on line 54 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected ';', found string

Check failure on line 54 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected name string at end of statement

Check failure on line 54 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected ';', found string (typecheck)
// If true, the server will not serve nor generate any OpenAPI resources
Disabled bool

Check failure on line 56 in engine.go

View workflow job for this annotation

GitHub Actions / tests

syntax error: unexpected name bool at end of statement

Check failure on line 56 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected name bool at end of statement
// If true, the engine will not print messages
DisableMessages bool

Check failure on line 58 in engine.go

View workflow job for this annotation

GitHub Actions / tests

syntax error: unexpected name bool at end of statement

Check failure on line 58 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected name bool at end of statement
// If true, the engine will not save the OpenAPI JSON spec locally
DisableLocalSave bool

Check failure on line 60 in engine.go

View workflow job for this annotation

GitHub Actions / tests

syntax error: unexpected name bool at end of statement

Check failure on line 60 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected name bool at end of statement
// Pretty prints the OpenAPI spec with proper JSON indentation
PrettyFormatJSON bool

Check failure on line 62 in engine.go

View workflow job for this annotation

GitHub Actions / tests

syntax error: unexpected name bool at end of statement

Check failure on line 62 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected name bool at end of statement (typecheck)
}

var defaultOpenAPIConfig = OpenAPIConfig{
JSONFilePath: "doc/openapi.json",
}
//implemnt action

Check warning on line 68 in engine.go

View workflow job for this annotation

GitHub Actions / lint

"implemnt" should be "implement".
func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {

Check failure on line 69 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected '(', found WithOpenAPIConfig

Check failure on line 69 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected ')', found OpenAPIConfig

Check failure on line 69 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected '(', found WithOpenAPIConfig (typecheck)
return func(e *Engine) {
e.openAPIConfig = config
}
}

// WithRequestContentType sets the accepted content types for the engine.
// By default, the accepted content types is */*.
func WithRequestContentType(consumes ...string) func(*Engine) {

Check failure on line 77 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected '(', found WithRequestContentType

Check failure on line 77 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected ')', found '...'

Check failure on line 77 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

missing ',' in parameter list

Check failure on line 77 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected '(', found WithRequestContentType (typecheck)
return func(e *Engine) { e.requestContentTypes = consumes }
}

func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {

Check failure on line 81 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected '(', found WithOpenAPIConfig

Check failure on line 81 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected ')', found OpenAPIConfig

Check failure on line 81 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected '(', found WithOpenAPIConfig (typecheck)
return func(e *Engine) {
if config.JSONFilePath != "" {
e.OpenAPIConfig.JSONFilePath = config.JSONFilePath
Expand All @@ -77,6 +90,14 @@
}
}

func (e *Engine) DisplayMiddlewares() []Middleware {

Check failure on line 93 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected operand, found ']'

Check failure on line 93 in engine.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected ';', found Middleware

Check failure on line 93 in engine.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected operand, found ']' (typecheck)
limit := e.openAPIConfig.MiddlewareDisplayLimit
if len(e.middlewares) < limit {
return e.middlewares
}
return e.middlewares[:limit]
}

// WithErrorHandler sets a customer error handler for the server
func WithErrorHandler(errorHandler func(err error) error) func(*Engine) {
return func(e *Engine) {
Expand Down Expand Up @@ -150,6 +171,28 @@
return json.Marshal(s.OpenAPI.Description())
}

func TestDefaultMiddlewareLimit(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test should go into engine_test.go

engine := NewEngine(DefaultOpenAPIConfig())
engine.middlewares = []Middleware{"A", "B", "C", "D", "E", "F"}

displayed := engine.DisplayMiddlewares()
if len(displayed) != 5 {
t.Errorf("Expected 5 middlewares, got %d", len(displayed))
}
}

func TestCustomMiddlewareLimit(t *testing.T) {
config := DefaultOpenAPIConfig()
config.MiddlewareDisplayLimit = 3
engine := NewEngine(config)
engine.middlewares = []Middleware{"A", "B", "C", "D", "E"}

displayed := engine.DisplayMiddlewares()
if len(displayed) != 3 {
t.Errorf("Expected 3 middlewares, got %d", len(displayed))
}
}

func (e *Engine) printOpenAPIMessage(msg string) {
if !e.OpenAPIConfig.DisableMessages {
slog.Info(msg)
Expand Down
Loading