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

[feature] Switch API server to huma for OpenAPI docs auto generation/documentation #311

Merged

Conversation

els0r
Copy link
Owner

@els0r els0r commented May 3, 2024

Simplifies the API documentation massively, as it is now embedded in the struct tags. Closes #309 .

API

Under the hood, the huma framework is used:

  • documents API via struct tags in relevant entities
  • replaces route registration from the gin framework

Not only does this simplify the hooks into the API, it also doesn't touch the remaining server setup fundamentally. The router in front of huma is still gin.

The API documentation is reachable under the /docs endpoint.

Error Handling

This addresses parts of #277. The detail errors for parsing args now relies on the huma.ErrorModel, which returns an RFC-compliant list of errors.

The benefit is that every field in the query args is validated and the whole list of offending fields is returned in the error. Both the API and goQuery use this to provide details to the caller.

For goQuery, the output looks as follows

$ go run cmd/goQuery/main.go -i eth0 sip,dap -c "dport 89" -n 0
Error running query: failed to prepare query:

    query (value: sip,dap)
    ----------------------
    invalid query type: 
    
    sip,dap
        ^
    unknown attribute
    
    condition (value: dport 89)
    ---------------------------
    invalid condition: 
    
    dport 89
          ^
    expected comparison operator
    
    num_results (value: 0)
    ----------------------
    invalid number of result rows: must be > 0

Flags / OpenAPI Generation

The OpenAPI spec can now be generated by the tool itself. This is necessary in order to guarantee up-to-date generation during the release step.

To this end, the --openapi flag was added to both goProbe and global-query. It causes the tools to start and generate the API spec as soon as possible.

Since neither tools start without config, the .buildconfig directory contains a minimal, stripped-down config, which can be relied on for OpenAPI spec generation.

@els0r els0r linked an issue May 3, 2024 that may be closed by this pull request
@els0r els0r marked this pull request as ready for review May 3, 2024 21:26
@els0r els0r requested a review from fako1024 May 3, 2024 21:26
Copy link
Collaborator

@fako1024 fako1024 left a comment

Choose a reason for hiding this comment

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

Noice - Thanks for going through all the painful effort of documenting the stuff in code! There's a few minor points of feedback (and the usual nitpicks), but apart from that LGTM!

.github/workflows/build-packages.yml Outdated Show resolved Hide resolved
.github/workflows/build-packages.yml Outdated Show resolved Hide resolved
cmd/global-query/cmd/server.go Outdated Show resolved Hide resolved
cmd/goProbe/goProbe.go Outdated Show resolved Hide resolved
cmd/global-query/cmd/server.go Outdated Show resolved Hide resolved
pkg/api/info.go Outdated Show resolved Hide resolved
pkg/api/info.go Outdated Show resolved Hide resolved
pkg/api/server/server.go Show resolved Hide resolved
pkg/capture/capturetypes/status.go Outdated Show resolved Hide resolved
pkg/results/result.go Outdated Show resolved Hide resolved
@els0r els0r merged commit dd1093e into main May 15, 2024
5 checks passed
@els0r els0r deleted the 309-openapi-switch-api-server-to-huma-for-docs-auto-generation branch May 15, 2024 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI: switch API server to huma for docs auto-generation
2 participants