Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions tonic/README
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,42 @@ of various parameters.

Here is an example input object.

```go
type MyInput struct {
Foo int `path:"foo"`
Bar string `query:"bar" default:"foobar"`
Baz string `json:"baz"`
}
```

Output objects can be of any type, and will be marshaled to JSON.

Input validation is performed after binding into the object using the validator library
(https://github.com/go-playground/validator). You can use the tag 'validate' on your object
definition to perform validation inside the tonic handler phase.

```go
type MyInput struct {
Foo int `path:"foo" validate:"required,gt=10"`
Bar string `query:"bar" default:"foobar" validate:"nefield=Baz"`
Baz string `json:"baz" validate:"required,email"`
}
```

enum input validation is also implemented natively by tonic, and can check that the provided input
value corresponds to one of the expected enum values.

```go
type MyInput struct {
Bar string `query:"bar" enum:"foo,buz,biz"`
}

```

The handler can return an error, which will be returned to the caller.

Here is a basic application that greets a user on http://localhost:8080/hello/me

```go
import (
"errors"
"fmt"
Expand Down Expand Up @@ -66,7 +72,7 @@ Here is a basic application that greets a user on http://localhost:8080/hello/me
r.GET("/hello/:name", tonic.Handler(GreetUser, 200))
r.Run(":8080")
}

```

If needed, you can also override different parts of the logic via certain available hooks in tonic:
- binding
Expand All @@ -82,6 +88,7 @@ We provide a ready-to-use error hook that depends on the juju/errors package (ri

Example of the same application as before, using juju errors:

```go
import (
"fmt"

Expand Down Expand Up @@ -112,6 +119,6 @@ Example of the same application as before, using juju errors:
r.GET("/hello/:name", tonic.Handler(GreetUser, 200))
r.Run(":8080")
}

```

You can also easily serve auto-generated swagger documentation (using tonic data) with https://github.com/wi2l/fizz