Comments (6)
@JanRuettinger my recommendation is quite similar to your original example, in that you have a wrapper object to track the dependencies and then register your operations from it so they have access to those dependencies. Something like this:
type HelloServer struct {
DB DBType
S3 S3Type
}
func NewHelloServer(db DBType, s3 S3Type) *HelloServer {
return &HelloServer{
DB: db,
S3: s3,
}
}
func (s *HelloServer) RegisterHello(api huma.API) {
huma.Register(api, huma.Operation{
OperationID: "get-greeting",
Summary: "Get a greeting",
Method: http.MethodGet,
Path: "/greeting/{name}",
}, func(ctx context.Context, input *GreetingInput) (*GreetingOutput, error) {
// TODO: use dependencies here as s.DB or s.S3
return nil, nil
})
}
func main() {
db := NewDB()
s3 := NewS3()
hello := NewHelloServer(db, s3)
router := chi.NewMux()
api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0"))
huma.AutoRegister(api, hello)
http.ListenAndServe(":8888", router)
}
The HelloServer
would group all the operations related to the "Hello" feature or which need those same dependencies. You can have many different servers registered to a single API. This setup also makes testing easier since you can use dependency injection with mocks.
Hopefully this answers your question!
from huma.
@Mastermjr you could try submitting a PR after modifying the files at https://github.com/danielgtaylor/huma/tree/main/docs. Running the docs locally is pretty easy:
# First, make sure you have Python installed, then grab the doc generator
$ pip install mkdocs-material
# Then run the docs locally
$ cd huma/docs
$ mkdocs serve
Should be running at http://localhost:8000/.
Separately, I'm working on a new example API to show off a bunch of features, project layout, testing, integration with DBs/Queues/etc, webhooks/SSE and stuff like that. It's in the early stages but I'm hoping to get something working within the next few weeks that can act as an example for people to learn from.
from huma.
How is the reference related to the issue?
from huma.
How could I get this added to the docs so others can reference this in the future?
When I've been learning how I want to adopt this project, its been difficult to figure out how I want to design my application so its easy to maintain
I know this issue is closed, so I'm happy to open a new one under this same topic
from huma.
@Mastermjr you could try submitting a PR after modifying the files at https://github.com/danielgtaylor/huma/tree/main/docs. Running the docs locally is pretty easy:
# First, make sure you have Python installed, then grab the doc generator $ pip install mkdocs-material # Then run the docs locally $ cd huma/docs $ mkdocs serveShould be running at http://localhost:8000/.
Separately, I'm working on a new example API to show off a bunch of features, project layout, testing, integration with DBs/Queues/etc, webhooks/SSE and stuff like that. It's in the early stages but I'm hoping to get something working within the next few weeks that can act as an example for people to learn from.
Yes please!
from huma.
I've gotten pulled into other things, when I get my PoC working I'll see how I can translate it into a post and get it put up here.
Would actually like some comments on how I've designed the endpoints.
Posting this publically so that I'm more inclined to follow through on this :)
from huma.
Related Issues (20)
- Support Discriminator and DiscriminatorMap HOT 2
- Setting headers in error response HOT 6
- Add more tests for cookie HOT 3
- Add custom header from SSE Handler HOT 3
- How should nullable query params be handled without pointers? HOT 4
- Add support for netip.Addr HOT 1
- Forwarding headers through the middleware not working HOT 5
- Override ErrorModel for single operations HOT 6
- Feature: Accept valueless path/query/header/cookie tags
- Docs UI doesn't handle multiple cookies properly HOT 1
- Allow custom Transform for nested objects HOT 2
- Error thrown when setting cli name HOT 2
- Error return for huma.StreamResponse.Body HOT 1
- Schema(r huma.Registry) *huma.Schema not working on Pointer
- Validation errors localization HOT 2
- Question: Is there a way to add comments to fields? HOT 1
- Version Chi v4 Into Separate Release
- Removing schema reference in response HOT 2
- Multiline doc description for struct fields
- Bug: API doc hidden is not *real* hidden HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from huma.