Saved searches
Use saved searches to filter your results more quickly
Cancel Create saved search
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
savaki / swag Public
Simple go wrapper to create swagger compatible APIs
License
savaki/swag
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch branches/tags
Branches Tags
Could not load branches
Nothing to show
Could not load tags
Nothing to show
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Cancel Create
- Local
- Codespaces
HTTPS GitHub CLI
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more about the CLI.
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
Latest commit message
Commit time
README.md
swag
swag is a lightweight library to generate swagger json for Go projects.
No code generation, no framework constraints, just a simple swagger definition.
swag is heavily geared towards generating REST/JSON apis.
Installation
go get github.com/savaki/swag
Status
This package should be considered a release candidate. No further package changes are expected at this point.
Concepts
swag uses functional options to generate both the swagger endpoints and the swagger definition. Where possible swag attempts to use reasonable defaults that may be overridden by the user.
Endpoints
swag provides a separate package, endpoint , to generate swagger endpoints. These endpoints can be passed to the swagger definition generate via swag.Endpoints(. )
In this simple example, we generate an endpoint to retrieve all pets. The only required fields for an endpoint are the method, path, and the summary.
allPets := endpoint.New("get", "/pet", "Return all the pets")
However, it’ll probably be useful if you include definitions of what GET /pet returns:
allPets := endpoint.New("get", "/pet", "Return all the pets", endpoint.Response(http.StatusOk, Pet<>, "Successful operation"), endpoint.Response(http.StatusInternalServerError, Error<>, "Oops . something went wrong"), )
Refer to the godoc for a list of all the endpoint options
Walk
As a convenience to users, *swagger.Api implements a Walk method to simplify traversal of all the endpoints. See the complete example below for how Walk can be used to bind endpoints to the router.
api := swag.New( swag.Title("Swagger Petstore"), swag.Endpoints(post, get), ) // iterate over each endpoint, if we've defined a handler, we can use it to bind to the router. We're using ```gin`` // in this example, but any web framework will do. // api.Walk(func(path string, endpoint *swagger.Endpoint) < h := endpoint.Handler.(func(c *gin.Context)) path = swag.ColonPath(path) router.Handle(endpoint.Method, path, h) >)
Complete Example
func handlePet(w http.ResponseWriter, _ *http.Request) < // your code here > type Pet struct < Id int64 `json:"id"` Name string `json:"name"` PhotoUrls []string `json:"photoUrls"` Tags []string `json:"tags"` > func main() < // define our endpoints // post := endpoint.New("post", "/pet", "Add a new pet to the store", endpoint.Handler(handle), endpoint.Description("Additional information on adding a pet to the store"), endpoint.Body(Pet<>, "Pet object that needs to be added to the store", true), endpoint.Response(http.StatusOK, Pet<>, "Successfully added pet"), ) get := endpoint.New("get", "/pet/", "Find pet by ID", endpoint.Handler(handle), endpoint.Path("petId", "integer", "ID of pet to return", true), endpoint.Response(http.StatusOK, Pet<>, "successful operation"), ) // define the swagger api that will contain our endpoints // api := swag.New( swag.Title("Swagger Petstore"), swag.Endpoints(post, get), ) // iterate over each endpoint and add them to the default server mux // for path, endpoints := range api.Paths < http.Handle(path, endpoints) > // use the api to server the swagger.json file // enableCors := true http.Handle("/swagger", api.Handler(enableCors)) http.ListenAndServe(":8080", nil) >
Additional Examples
Examples for popular web frameworks can be found in the examples directory:
Ready to get more out of work and life?
Your new go-to. Rolling easier work, rewarding careers, faster pay, and everyday savings into the world’s first employment superapp.
Everything you need to work, shop, and save.
It’s streamlined work, faster pay, and game-changing rewards. Swag cuts down life admin, so you can make time for what matters.
Work Career Money Benefits
Work
Simplify your work admin, policies and communications.
- Stay across company news, shoutouts and events
- Complete training and track goals
- Manage leave requests and timesheets
Career
Discover new internal opportunities and apply for roles in minutes.
- Discover internal job opportunities
- Fast-track your career development
- Talk to internal hiring managers 1:1
- Refer friends and family for roles
Money
Get paid on-demand and make saving second nature.
- Break free from pay cycles with InstaPay—the flexible, credit-free way to get paid
- Score your own Swag Visa Debit card and Swag Spend account
- Shop with Google Pay or Apple Pay
Benefits
Get more from your everyday spend with Cashback offers and the Swag store.
- Enjoy up to 20% cash back from leading retailers like The Iconic, Adore Beauty and JB Hi-Fi
- Save on everyday essentials with 5% off gift cards from Uber Eats, Caltex, Dan Murphy’s and more
- Get rewarded with Hero Dollars at work. Use it to unlock exclusive savings in the Swag store
How It Works
How It Works
Receive your physical Swag Visa Debit card in the mail after 5-7 days.
Spend and save on Cashback offers and in the Swag store.
Set up your Swag Spend Account and instantly set up your digital Visa Debit card.
Download the Swag app today.
The world loves Swag
42% average time saved
average time saved
Organisations Employees Research & Development Cut work admin
Work smarter, save faster.
Swag is your one-stop-shop to level-up your work, savings and life. Download for free today.
Give us a follow
Stay up to date with the latest work advice, career tips and money hacks—created just for you.
Acknowledgement of Country
We acknowledgement Aboriginal and Torres Strait Islander peoples as the First Australians and Traditional Custodians of the lands where we live, learn and work.
Get in Touch
Your Region
- Australia
- New Zealand
- United Kingdom
- Singapore
- Malaysia
- Legal Centre
- Cookies
- Cookie Policy
- Privacy Policy
- Terms
2023 © Swag. All rights reserved
Powered by Employment Hero
- Legal Centre
- Cookies
- Cookie Policy
- Privacy Policy
- Terms
2023 © Swag. All rights reserved. Powered by Employment Hero
При подготовке материала использовались источники:
https://github.com/savaki/swag
https://swagapp.com/