Skip to content

Commit

Permalink
better doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 25, 2023
1 parent 3b444f4 commit 3751b35
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ run:
linters:
enable-all: true
disable:
- tagalign
- ireturn
- gofumpt
- testpackage
Expand Down
6 changes: 3 additions & 3 deletions lib/examples/add/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
//
// curl 'localhost:3000/double' -d 3
func main() {
r := goapi.New()
g := goapi.New()

goapi.Add(r, Double)
goapi.Add(g, Double)

log.Println(r.Start(":3000"))
log.Println(g.Start(":3000"))
}

// Double for "POST /double" which doubles the input to response.
Expand Down
6 changes: 3 additions & 3 deletions lib/examples/as-echo-middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
func main() {
e := echo.New()

router := goapi.New()
g := goapi.New()

router.GET("/hello", func() Res {
g.GET("/hello", func() Res {
return Res{Data: "World"}
})

e.Use(echo.WrapMiddleware(router.Handler))
e.Use(echo.WrapMiddleware(g.Handler))

_ = e.Start(":3000")
}
Expand Down
6 changes: 3 additions & 3 deletions lib/examples/as-gin-middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
func main() {
e := gin.New()

router := goapi.New()
g := goapi.New()

router.GET("/hello", func() Res {
g.GET("/hello", func() Res {
return Res{Data: "World"}
})

e.Use(func(ctx *gin.Context) {
router.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
g.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx.Next()
})).ServeHTTP(ctx.Writer, ctx.Request)
})
Expand Down
22 changes: 16 additions & 6 deletions lib/examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log"
"regexp"

"github.com/NaturalSelectionLabs/goapi"
"github.com/NaturalSelectionLabs/goapi/lib/middlewares/apidoc"
Expand All @@ -21,11 +22,13 @@ import (
//
// bash ./lib/examples/basic/test.sh
func main() {
r := goapi.New()
g := goapi.New()

r.POST("/login", func(p ParamsLogin) ResLogin {
g.Router().AddFormatChecker("password", passwordChecker{})

g.POST("/login", func(p ParamsLogin) ResLogin {
// If the username and password are not correct, return a LoginFail response.
if p.Username != "admin" || p.Password != "123456" {
if p.Username != "[email protected]" || p.Password != "123456" {
return goapi.StatusUnauthorized{}
}

Expand All @@ -42,17 +45,17 @@ func main() {

// You can use multiple parameters at the same time to get url values, headers, request context, or request body.
// The order of the parameters doesn't matter.
r.GET("/users/{id}/posts", GetPosts{})
g.GET("/users/{id}/posts", GetPosts{})

// Install endpoints for openapi doc.
apidoc.Install(r, func(doc *openapi.Document) *openapi.Document {
apidoc.Install(g, func(doc *openapi.Document) *openapi.Document {
// Use this callback to customize the openapi document.
doc.Info.Title = "Basic Example"
doc.Info.Version = "0.0.1"
return doc
})

log.Println(r.Start(":3000"))
log.Println(g.Start(":3000"))
}

// GetPosts is the handler for fetching posts of a user.
Expand Down Expand Up @@ -94,3 +97,10 @@ func fetchPosts(c context.Context, id int, keyword, typ string) []string {

return posts
}

type passwordChecker struct {
}

func (passwordChecker) IsFormat(input interface{}) bool {
return regexp.MustCompile(`^\d+$`).MatchString(input.(string))
}
2 changes: 1 addition & 1 deletion lib/examples/basic/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ echo
echo
echo '== login =='

curl localhost:3000/login -id '{"username": "admin", "password": "123456"}'
curl localhost:3000/login -id '{"username": "[email protected]", "password": "123456"}'

echo
echo
Expand Down
5 changes: 3 additions & 2 deletions lib/examples/basic/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ type ParamsPagination struct {
// It should be treated as a common json struct of golang, goapi won't do any special handling for it,
// such as default field tag won't work.
type ParamsLogin struct {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" format:"email"`
// Here format:"password" is a custom format checker added by [goapi.Router.AddFormatChecker].
Password string `json:"password" format:"password"`
}

// PostType of a post.
Expand Down
6 changes: 3 additions & 3 deletions lib/examples/hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type Hello struct {
}

func main() {
r := goapi.New()
g := goapi.New()

r.GET("/", func() Hello {
g.GET("/", func() Hello {
return Hello{Data: "Hello World!"}
})

log.Println(r.Start(":3000"))
log.Println(g.Start(":3000"))
}

0 comments on commit 3751b35

Please sign in to comment.