-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read the _examples/basic for migration
- Loading branch information
Showing
32 changed files
with
278 additions
and
766 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.vscode | ||
.sum | ||
*.exe |
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 |
---|---|---|
|
@@ -3,14 +3,13 @@ | |
[Iris](https://github.com/kataras/iris) middleware to automatically generate RESTful API documentation with Swagger 2.0 as requested at [#1231](https://github.com/kataras/iris/issues/1231). | ||
|
||
[](https://travis-ci.org/iris-contrib/swagger) | ||
[](https://codecov.io/gh/iris-contrib/swagger) | ||
[](https://goreportcard.com/report/github.com/iris-contrib/swagger) | ||
[](https://godoc.org/github.com/iris-contrib/swagger) | ||
[](https://app.fossa.io/projects/git%2Bgithub.com%2Firis-contrib%2Fswagger?ref=badge_shield) | ||
[](https://pkg.go.dev/github.com/iris-contrib/swagger) | ||
|
||
## Usage | ||
|
||
### Start using it | ||
|
||
1. Add comments to your API source code, [See Declarative Comments Format](https://swaggo.github.io/swaggo.io/declarative_comments_format/). | ||
2. Download [Swag](https://github.com/swaggo/swag) for Go by using: | ||
|
||
|
@@ -27,7 +26,7 @@ $ swag init | |
4. Download [swagger for Iris](https://github.com/iris-contrib/swagger) by using: | ||
|
||
```sh | ||
$ go get github.com/iris-contrib/swagger/v12@v12.2.0-alpha | ||
$ go get github.com/iris-contrib/swagger/v12@master | ||
``` | ||
|
||
And import following in your code: | ||
|
@@ -38,7 +37,7 @@ import "github.com/iris-contrib/swagger/v12/swaggerFiles" // swagger embed files | |
|
||
``` | ||
|
||
### Canonical example: | ||
### Example Code: | ||
|
||
```go | ||
package main | ||
|
@@ -49,7 +48,9 @@ import ( | |
"github.com/iris-contrib/swagger/v12" | ||
"github.com/iris-contrib/swagger/v12/swaggerFiles" | ||
|
||
_ "./docs" // docs is generated by Swag CLI, you have to import it. | ||
_ "github.com/your_username/your_project/docs" | ||
// docs folder should be generated by Swag CLI (swag init), | ||
// you have to import it. | ||
) | ||
|
||
// @title Swagger Example API | ||
|
@@ -64,64 +65,37 @@ import ( | |
// @license.name Apache 2.0 | ||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
// @host petstore.swagger.io | ||
// @host localhost:8080 | ||
// @BasePath /v2 | ||
func main() { | ||
app := iris.New() | ||
|
||
config := &swagger.Config{ | ||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition | ||
config := swagger.Config{ | ||
// The url pointing to API definition. | ||
URL: "http://localhost:8080/swagger/doc.json", | ||
DeepLinking: true, | ||
DocExpansion: "list", | ||
DomID: "#swagger-ui", | ||
// The UI prefix URL (see route). | ||
Prefix: "/swagger", | ||
} | ||
// use swagger middleware to | ||
app.Get("/swagger/{any:path}", swagger.CustomWrapHandler(config, swaggerFiles.Handler)) | ||
swaggerUI := swagger.Handler(swaggerFiles.Handler, config) | ||
|
||
// Register on http://localhost:8080/swagger | ||
app.Get("/swagger", swaggerUI) | ||
// And the wildcard one for index.html, *.js, *.css and e.t.c. | ||
app.Get("/swagger/{any:path}", swaggerUI) | ||
|
||
app.Run(iris.Addr(":8080")) | ||
app.Listen(":8080") | ||
} | ||
``` | ||
|
||
5. Run it, and browser to http://localhost:8080/swagger/index.html, you can see Swagger 2.0 Api documents. | ||
5. Run it, and browse to http://localhost:8080/swagger/index.html, you can see Swagger 2.0 API documentation. | ||
|
||
 | ||
|
||
6. If you want to disable swagger when some environment variable is set, use `DisablingWrapHandler` | ||
|
||
### Example with disabling: | ||
6. If you want to disable swagger when some environment variable is set, use `DisablingHandler` instead of `Handler`. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/kataras/iris/v12" | ||
|
||
"github.com/iris-contrib/swagger/v12" | ||
"github.com/iris-contrib/swagger/v12/swaggerFiles" | ||
|
||
_ "./docs" // docs is generated by Swag CLI, you have to import it. | ||
) | ||
|
||
// @title Swagger Example API | ||
// @version 1.0 | ||
// @description This is a sample server Petstore server. | ||
// @termsOfService http://swagger.io/terms/ | ||
|
||
// @contact.name API Support | ||
// @contact.url http://www.swagger.io/support | ||
// @contact.email [email protected] | ||
|
||
// @license.name Apache 2.0 | ||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
// @host petstore.swagger.io | ||
// @BasePath /v2 | ||
func main() { | ||
app := iris.New() | ||
|
||
// use swagger middleware to | ||
app.Get("/swagger/{any:path}", swagger.DisablingWrapHandler(swaggerFiles.Handler, "NAME_OF_ENV_VARIABLE")) | ||
|
||
app.Run(iris.Addr(":8080")) | ||
} | ||
swagger.DisablingHandler(swaggerFiles.Handler, config) | ||
``` | ||
|
||
Then, if you set envioment variable `NAME_OF_ENV_VARIABLE` to anything, `/swagger/*any` | ||
will respond 404, just like when route unspecified. |
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,9 @@ | ||
# Basic | ||
|
||
```sh | ||
$ go get github.com/swaggo/swag/cmd/swag | ||
$ swag init | ||
$ go run main.go | ||
``` | ||
|
||
Navigate to <http://localhost:8080/swagger>. |
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
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ definitions: | |
errorMessage: | ||
type: string | ||
type: object | ||
host: petstore.swagger.io | ||
host: localhost:8080 | ||
info: | ||
contact: | ||
email: [email protected] | ||
|
@@ -43,12 +43,10 @@ paths: | |
description: We need ID!! | ||
schema: | ||
$ref: '#/definitions/web.APIError' | ||
type: object | ||
"404": | ||
description: Can not find ID | ||
schema: | ||
$ref: '#/definitions/web.APIError' | ||
type: object | ||
summary: Add a new pet to the store | ||
/testapi/get-struct-array-by-string/{some_id}: | ||
get: | ||
|
@@ -82,10 +80,8 @@ paths: | |
description: We need ID!! | ||
schema: | ||
$ref: '#/definitions/web.APIError' | ||
type: object | ||
"404": | ||
description: Can not find ID | ||
schema: | ||
$ref: '#/definitions/web.APIError' | ||
type: object | ||
swagger: "2.0" |
Oops, something went wrong.