Skip to content

Commit 69f6b56

Browse files
author
chengjin
committed
init v1.0.0
1 parent 36235a4 commit 69f6b56

30 files changed

+1615
-0
lines changed

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.dll
4+
*.so
5+
*.dylib
6+
7+
# Test binary, build with `go test -c`
8+
*.test
9+
10+
# Output of the go coverage tool, specifically when used with LiteIDE
11+
*.out
12+
dist
13+
14+
.idea
15+
vendor
16+
.envrc

.goreleaser.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
builds:
2+
- skip: true
3+
snapshot:
4+
name_template: "{{ .Tag }}-next"
5+
changelog:
6+
sort: asc
7+
filters:
8+
exclude:
9+
- '^docs:'
10+
- '^test:'

PULL_REQUEST_TEMPLATE.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
**Describe the PR**
2+
e.g. add cool parser.
3+
4+
**Relation issue**
5+
e.g. https://github.com/swaggo/fasthttp-swagger/pull/123/files
6+
7+
**Additional context**
8+
Add any other context about the problem here.

README.md

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# fasthttp-swagger
2+
3+
gin middleware to automatically generate RESTFUL API documentation with Swagger 2.0.
4+
5+
[![Build Status](https://github.com/swaggo/fasthttp-swagger/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/features/actions)
6+
[![Codecov branch](https://img.shields.io/codecov/c/github/swaggo/fasthttp-swagger/master.svg)](https://codecov.io/gh/swaggo/fasthttp-swagger)
7+
[![Go Report Card](https://goreportcard.com/badge/github.com/swaggo/fasthttp-swagger)](https://goreportcard.com/report/github.com/swaggo/fasthttp-swagger)
8+
[![GoDoc](https://godoc.org/github.com/swaggo/fasthttp-swagger?status.svg)](https://godoc.org/github.com/swaggo/fasthttp-swagger)
9+
[![Release](https://img.shields.io/github/release/swaggo/fasthttp-swagger.svg?style=flat-square)](https://github.com/swaggo/fasthttp-swagger/releases)
10+
11+
## Usage
12+
13+
### Start using it
14+
15+
1. Add comments to your API source code, [See Declarative Comments Format](https://swaggo.github.io/swaggo.io/declarative_comments_format/).
16+
2. Download [Swag](https://github.com/swaggo/swag) for Go by using:
17+
18+
```sh
19+
go get -u github.com/swaggo/swag/cmd/swag
20+
```
21+
22+
3. Run the [Swag](https://github.com/swaggo/swag) at your Go project root path(for instance `~/root/go-peoject-name`),
23+
[Swag](https://github.com/swaggo/swag) will parse comments and generate required files(`docs` folder and `docs/doc.go`)
24+
at `~/root/go-peoject-name/docs`.
25+
26+
```sh
27+
swag init
28+
```
29+
30+
4. Download [fasthttp-swagger](https://github.com/swaggo/fasthttp-swagger) by using:
31+
32+
```sh
33+
go get -u github.com/swaggo/fasthttp-swagger
34+
go get -u github.com/swaggo/files
35+
```
36+
37+
Import following in your code:
38+
39+
```go
40+
import "github.com/swaggo/fasthttp-swagger" // fasthttp-swagger middleware
41+
42+
```
43+
44+
### Canonical example:
45+
46+
Now assume you have implemented a simple api as following:
47+
48+
```go
49+
// A get function which returns a hello world string by json
50+
func Helloworld(ctx *fasthttp.RequestCtx) {
51+
ctx.SetStatusCode(http.StatusOK)
52+
ctx.Write([]byte("helloworld"))
53+
}
54+
55+
```
56+
57+
So how to use fasthttp-swagger on api above? Just follow the following guide.
58+
59+
1. Add Comments for apis and main function with fasthttp-swagger rules like following:
60+
61+
```go
62+
// @BasePath /api/v1
63+
64+
// PingExample godoc
65+
// @Summary ping example
66+
// @Schemes
67+
// @Description do ping
68+
// @Tags example
69+
// @Accept json
70+
// @Produce json
71+
// @Success 200 {string} Helloworld
72+
// @Router /example/helloworld [get]
73+
func Helloworld(ctx *fasthttp.RequestCtx) {
74+
ctx.SetStatusCode(http.StatusOK)
75+
ctx.Write([]byte("helloworld"))
76+
}
77+
```
78+
79+
2. Use `swag init` command to generate a docs, docs generated will be stored at
80+
3. import the docs like this:
81+
I assume your project named `github.com/go-project-name/docs`.
82+
83+
```go
84+
import (
85+
docs "github.com/go-project-name/docs"
86+
)
87+
```
88+
89+
4. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI.
90+
91+
5. The full code and folder relatives here:
92+
93+
```go
94+
package main
95+
96+
import (
97+
_ "github.com/go-project-name/docs"
98+
fasthttpSwagger "github.com/swaggo/fasthttp-swagger"
99+
)
100+
// @BasePath /api/v1
101+
102+
// PingExample godoc
103+
// @Summary ping example
104+
// @Schemes
105+
// @Description do ping
106+
// @Tags example
107+
// @Accept json
108+
// @Produce json
109+
// @Success 200 {string} Helloworld
110+
// @Router /example/helloworld [get]
111+
func Helloworld(ctx *fasthttp.RequestCtx) {
112+
ctx.SetStatusCode(http.StatusOK)
113+
ctx.Write([]byte("helloworld"))
114+
}
115+
116+
func main() {
117+
fasthttp.ListenAndServe(":8080", func(ctx *fasthttp.RequestCtx) {
118+
path := string(ctx.RequestURI())
119+
switch {
120+
case strings.HasPrefix(path, "/swagger"):
121+
fastHttpSwagger.WrapHandler(fastHttpSwagger.InstanceName("swagger"))(ctx)
122+
case path == "/api/v1/example/helloworld":
123+
Helloworld(ctx)
124+
}
125+
})
126+
}
127+
```
128+
129+
Demo project tree, `swag init` is run at relative `.`
130+
131+
```
132+
.
133+
├── docs
134+
│   ├── docs.go
135+
│   ├── swagger.json
136+
│   └── swagger.yaml
137+
├── go.mod
138+
├── go.sum
139+
└── main.go
140+
```

b0x.yml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# all folders and files are relative to the path
2+
# where fileb0x was run at!
3+
4+
# default: main
5+
pkg: swaggerFiles
6+
7+
# destination
8+
dest: "./swaggerFiles/"
9+
10+
# gofmt
11+
# type: bool
12+
# default: false
13+
fmt: true
14+
15+
# compress files
16+
# at the moment, only supports gzip
17+
#
18+
# type: object
19+
compression:
20+
# activates the compression
21+
#
22+
# type: bool
23+
# default: false
24+
compress: false
25+
26+
# valid values are:
27+
# -> "NoCompression"
28+
# -> "BestSpeed"
29+
# -> "BestCompression"
30+
# -> "DefaultCompression" or ""
31+
#
32+
# type: string
33+
# default: "DefaultCompression" # when: Compress == true && Method == ""
34+
method: ""
35+
36+
# true = do it yourself (the file is written as gzip compressed file into the memory file system)
37+
# false = decompress files at run time (while writing file into memory file system)
38+
#
39+
# type: bool
40+
# default: false
41+
keep: false
42+
43+
# ---------------
44+
# -- DANGEROUS --
45+
# ---------------
46+
#
47+
# cleans the destination folder (only b0xfiles)
48+
# you should use this when using the spread function
49+
# type: bool
50+
# default: false
51+
clean: true
52+
53+
# default: ab0x.go
54+
output: "ab0x.go"
55+
56+
# [unexporTed] builds non-exporTed functions, variables and types...
57+
# type: bool
58+
# default: false
59+
unexporTed: false
60+
61+
# [spread] means it will make a file to hold all fileb0x data
62+
# and each file into a separaTed .go file
63+
#
64+
# example:
65+
# theres 2 files in the folder assets, they're: hello.json and world.txt
66+
# when spread is activaTed, fileb0x will make a file:
67+
# b0x.go or [output]'s data, assets_hello.json.go and assets_world.txt.go
68+
#
69+
#
70+
# type: bool
71+
# default: false
72+
spread: true
73+
74+
# type: array of objects
75+
custom:
76+
77+
- files:
78+
# everything inside the folder
79+
# type: array of strings
80+
- "./dist/"
81+
82+
# base is the path that will be removed from all files' path
83+
# type: string
84+
base: "dist"

example/basic/api/api.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"github.com/swaggo/swag/example/basic/web"
6+
"github.com/valyala/fasthttp"
7+
)
8+
9+
// @Summary Add a new pet to the store
10+
// @Description get string by ID
11+
// @Accept json
12+
// @Produce json
13+
// @Param some_id path int true "Some ID"
14+
// @Success 200 {string} string "ok"
15+
// @Failure 400 {object} web.APIError "We need ID!!"
16+
// @Failure 404 {object} web.APIError "Can not find ID"
17+
// @Router /testapi/get-string-by-int/{some_id} [get]
18+
func GetStringByInt(ctx *fasthttp.RequestCtx) {
19+
err := web.APIError{}
20+
fmt.Println(err)
21+
}
22+
23+
// @Description get struct array by ID
24+
// @Accept json
25+
// @Produce json
26+
// @Param some_id path string true "Some ID"
27+
// @Param offset query int true "Offset"
28+
// @Param limit query int true "Offset"
29+
// @Success 200 {string} string "ok"
30+
// @Failure 400 {object} web.APIError "We need ID!!"
31+
// @Failure 404 {object} web.APIError "Can not find ID"
32+
// @Router /testapi/get-struct-array-by-string/{some_id} [get]
33+
func GetStructArrayByString(ctx *fasthttp.RequestCtx) {
34+
35+
}
36+
37+
type Pet3 struct {
38+
ID int `json:"id"`
39+
}

0 commit comments

Comments
 (0)