-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Suggestions for structuring/organisation in larger projects #249
Description
Hi!
I'm trying to use oapi-codegen to generate server stubs for our API here but I'm struggling to find a way to structure and organise my generated code in a way that feels somewhat idiomatic-ish Go. My usecase is that the application I'm working on is structured into packages by feature (call it what you want; features, bounded contexts, modules - it's a bit besides the point). My HTTP handlers and models are defined in those packages, and I have a global main-entrypoint that wires up all the handlers across the system and spins up a HTTP server with those endpoints routed up.
Now, I'm trying to introduce oapi-codegen to this while keeping that structure intact. I've noticed that it doesn't support external $ref (#42) but that --import-mapping is added (#204) to allow having the models generated in a different package than the handlers to partially work around this issue. My desired structure is something like this:
cmd/
api/
main.go
openapi-server.gen.go
feat1/
openapi.yaml
openapi-types.gen.go
feat2/
openapi.yaml
openapi-types.gen.go
Ideally I'd want the API endpoints exposed by each feature to also be defined in openapi.yaml within the package, and have all of those merged together when generating the server stubs in cmd/api/. I currently have a proof of concept working of this using a mix of yq merge and swagger-cli bundle to preprocess the files before running oapi-codegen but the whole thing is feeling very hacky and brittle. Has anyone else attempted something similar and found a solution to this that might work? I'd be happy to contribute an example project to the repository if I could find a solution I'd be happy with, any input would be appreciated.