Code Monkey home page Code Monkey logo

Comments (6)

danielgtaylor avatar danielgtaylor commented on May 17, 2024 3

@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.

danielgtaylor avatar danielgtaylor commented on May 17, 2024 3

@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.

JanRuettinger avatar JanRuettinger commented on May 17, 2024

How is the reference related to the issue?

from huma.

Mastermjr avatar Mastermjr commented on May 17, 2024

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.

IcelandicIcecream avatar IcelandicIcecream commented on May 17, 2024

@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.

Yes please!

from huma.

Mastermjr avatar Mastermjr commented on May 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.