-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from danielgtaylor/v1-middleware
fix: support v1 middleware, add example
- Loading branch information
Showing
5 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// An example showing how to use Huma v1 middleware in a Huma v2 project. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/danielgtaylor/huma/middleware" | ||
"github.com/danielgtaylor/huma/v2" | ||
"github.com/danielgtaylor/huma/v2/adapters/humachi" | ||
"github.com/go-chi/chi" | ||
) | ||
|
||
// Options for the CLI. | ||
type Options struct { | ||
Port int `help:"Port to listen on" default:"8888"` | ||
} | ||
|
||
// GreetingInput represents the greeting operation request. | ||
type GreetingInput struct { | ||
Name string `path:"name" doc:"Name to greet"` | ||
} | ||
|
||
// GreetingOutput represents the greeting operation response. | ||
type GreetingOutput struct { | ||
Body struct { | ||
Message string `json:"message" doc:"Greeting message" example:"Hello, world!"` | ||
} | ||
} | ||
|
||
func main() { | ||
// Create a CLI app which takes a port option. | ||
cli := huma.NewCLI(func(hooks huma.Hooks, opts *Options) { | ||
// Create a Chi v4.x.x router instance. | ||
router := chi.NewMux() | ||
|
||
// Attach the Huma v1 middleware to the router. | ||
router.Use(middleware.DefaultChain) | ||
|
||
// Create the API | ||
config := huma.DefaultConfig("My API", "1.0.0") | ||
api := humachi.NewV4(router, config) | ||
|
||
// Register GET /greeting/{name} | ||
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) { | ||
resp := &GreetingOutput{} | ||
resp.Body.Message = fmt.Sprintf("Hello, %s!", input.Name) | ||
return resp, nil | ||
}) | ||
|
||
srv := &http.Server{ | ||
Addr: fmt.Sprintf("%s:%d", "localhost", opts.Port), | ||
Handler: router, | ||
} | ||
|
||
// Tell the CLI how to start your router. | ||
hooks.OnStart(func() { | ||
// Start the server | ||
log.Printf("Server is running with: host:%v port:%v\n", "localhost", opts.Port) | ||
|
||
err := srv.ListenAndServe() | ||
if err != nil && err != http.ErrServerClosed { | ||
log.Fatalf("listen: %s\n", err) | ||
} | ||
}) | ||
}) | ||
|
||
// Run the CLI. When passed no commands, it starts the server. | ||
cli.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters