2017-02-14 04:54:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2022-04-13 00:11:31 +02:00
|
|
|
"github.com/kataras/iris/v12/x/errors"
|
2017-02-14 04:54:11 +01:00
|
|
|
)
|
|
|
|
|
2020-04-08 15:48:22 +02:00
|
|
|
// User example struct for json and msgpack.
|
2017-07-10 17:32:42 +02:00
|
|
|
type User struct {
|
2020-04-08 15:48:22 +02:00
|
|
|
Firstname string `json:"firstname" msgpack:"firstname"`
|
|
|
|
Lastname string `json:"lastname" msgpack:"lastname"`
|
|
|
|
City string `json:"city" msgpack:"city"`
|
|
|
|
Age int `json:"age" msgpack:"age"`
|
2017-07-10 17:32:42 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 04:54:11 +01:00
|
|
|
// ExampleXML just a test struct to view represents xml content-type
|
|
|
|
type ExampleXML struct {
|
|
|
|
XMLName xml.Name `xml:"example"`
|
|
|
|
One string `xml:"one,attr"`
|
|
|
|
Two string `xml:"two,attr"`
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:48:22 +02:00
|
|
|
// ExampleYAML just a test struct to write yaml to the client.
|
|
|
|
type ExampleYAML struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
ServerAddr string `yaml:"ServerAddr"`
|
|
|
|
}
|
|
|
|
|
2017-02-14 04:54:11 +01:00
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2022-04-13 00:11:31 +02:00
|
|
|
// Optionally, set a custom handler for JSON, JSONP, Protobuf, MsgPack, YAML, Markdown...
|
|
|
|
// write errors.
|
|
|
|
app.SetContextErrorHandler(new(errorHandler))
|
2017-07-10 17:32:42 +02:00
|
|
|
// Read
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Post("/decode", func(ctx iris.Context) {
|
2020-06-07 14:26:06 +02:00
|
|
|
// Read https://github.com/kataras/iris/blob/master/_examples/request-body/read-json/main.go as well.
|
2017-07-10 17:32:42 +02:00
|
|
|
var user User
|
2021-10-13 15:22:22 +02:00
|
|
|
err := ctx.ReadJSON(&user)
|
|
|
|
if err != nil {
|
2022-04-13 00:11:31 +02:00
|
|
|
errors.InvalidArgument.Details(ctx, "unable to parse body", err.Error())
|
2021-10-13 15:22:22 +02:00
|
|
|
return
|
|
|
|
}
|
2017-07-10 17:32:42 +02:00
|
|
|
|
|
|
|
ctx.Writef("%s %s is %d years old and comes from %s!", user.Firstname, user.Lastname, user.Age, user.City)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Write
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/encode", func(ctx iris.Context) {
|
2020-04-08 15:48:22 +02:00
|
|
|
u := User{
|
2017-07-10 17:32:42 +02:00
|
|
|
Firstname: "John",
|
|
|
|
Lastname: "Doe",
|
|
|
|
City: "Neither FBI knows!!!",
|
|
|
|
Age: 25,
|
|
|
|
}
|
|
|
|
|
2020-04-27 11:28:30 +02:00
|
|
|
// Manually setting a content type: ctx.ContentType("text/javascript")
|
2020-04-08 15:48:22 +02:00
|
|
|
ctx.JSON(u)
|
2017-07-10 17:32:42 +02:00
|
|
|
})
|
|
|
|
|
2020-09-01 12:28:21 +02:00
|
|
|
// Use Secure field to prevent json hijacking.
|
|
|
|
// It prepends `"while(1),"` to the body when the data is array.
|
|
|
|
app.Get("/json_secure", func(ctx iris.Context) {
|
|
|
|
response := []string{"val1", "val2", "val3"}
|
|
|
|
options := iris.JSON{Indent: "", Secure: true}
|
|
|
|
ctx.JSON(response, options)
|
|
|
|
|
|
|
|
// Will output: while(1);["val1","val2","val3"]
|
|
|
|
})
|
|
|
|
|
|
|
|
// Use ASCII field to generate ASCII-only JSON
|
|
|
|
// with escaped non-ASCII characters.
|
|
|
|
app.Get("/json_ascii", func(ctx iris.Context) {
|
|
|
|
response := iris.Map{"lang": "GO-虹膜", "tag": "<br>"}
|
|
|
|
options := iris.JSON{Indent: " ", ASCII: true}
|
|
|
|
ctx.JSON(response, options)
|
|
|
|
|
|
|
|
/* Will output:
|
|
|
|
{
|
|
|
|
"lang": "GO-\u8679\u819c",
|
|
|
|
"tag": "\u003cbr\u003e"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
})
|
|
|
|
|
|
|
|
// Do not replace special HTML characters with their unicode entities
|
|
|
|
// using the UnescapeHTML field.
|
|
|
|
app.Get("/json_raw", func(ctx iris.Context) {
|
|
|
|
options := iris.JSON{UnescapeHTML: true}
|
|
|
|
ctx.JSON(iris.Map{
|
|
|
|
"html": "<b>Hello, world!</b>",
|
|
|
|
}, options)
|
|
|
|
|
|
|
|
// Will output: {"html":"<b>Hello, world!</b>"}
|
|
|
|
})
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// Other content types,
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/binary", func(ctx iris.Context) {
|
2017-07-10 17:32:42 +02:00
|
|
|
// useful when you want force-download of contents of raw bytes form.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.Binary([]byte("Some binary data here."))
|
2017-02-14 04:54:11 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/text", func(ctx iris.Context) {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.Text("Plain text here")
|
2017-02-14 04:54:11 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/json", func(ctx iris.Context) {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.JSON(map[string]string{"hello": "json"}) // or myjsonStruct{hello:"json}
|
2017-02-14 04:54:11 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/jsonp", func(ctx iris.Context) {
|
2020-09-01 12:28:21 +02:00
|
|
|
ctx.JSONP(map[string]string{"hello": "jsonp"}, iris.JSONP{Callback: "callbackName"})
|
2017-02-14 04:54:11 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/xml", func(ctx iris.Context) {
|
2020-06-28 22:05:07 +02:00
|
|
|
ctx.XML(ExampleXML{One: "hello", Two: "xml"})
|
|
|
|
// OR:
|
|
|
|
// ctx.XML(iris.XMLMap("keys", iris.Map{"key": "value"}))
|
2017-02-14 04:54:11 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/markdown", func(ctx iris.Context) {
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
|
2017-02-14 04:54:11 +01:00
|
|
|
})
|
|
|
|
|
2020-04-08 15:48:22 +02:00
|
|
|
app.Get("/yaml", func(ctx iris.Context) {
|
|
|
|
ctx.YAML(ExampleYAML{Name: "Iris", ServerAddr: "localhost:8080"})
|
2020-06-28 22:05:07 +02:00
|
|
|
// OR:
|
|
|
|
// ctx.YAML(iris.Map{"name": "Iris", "serverAddr": "localhost:8080"})
|
2020-04-08 15:48:22 +02:00
|
|
|
})
|
|
|
|
|
2020-06-25 12:04:36 +02:00
|
|
|
// app.Get("/protobuf", func(ctx iris.Context) {
|
|
|
|
// ctx.Protobuf(proto.Message)
|
|
|
|
// })
|
|
|
|
|
2020-04-08 15:48:22 +02:00
|
|
|
app.Get("/msgpack", func(ctx iris.Context) {
|
|
|
|
u := User{
|
|
|
|
Firstname: "John",
|
|
|
|
Lastname: "Doe",
|
|
|
|
City: "Neither FBI knows!!!",
|
|
|
|
Age: 25,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.MsgPack(u)
|
|
|
|
})
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// http://localhost:8080/decode
|
|
|
|
// http://localhost:8080/encode
|
2020-09-01 12:28:21 +02:00
|
|
|
// http://localhost:8080/json_secure
|
|
|
|
// http://localhost:8080/json_ascii
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
//
|
|
|
|
// http://localhost:8080/binary
|
|
|
|
// http://localhost:8080/text
|
|
|
|
// http://localhost:8080/json
|
|
|
|
// http://localhost:8080/jsonp
|
|
|
|
// http://localhost:8080/xml
|
|
|
|
// http://localhost:8080/markdown
|
2020-04-08 15:48:22 +02:00
|
|
|
// http://localhost:8080/msgpack
|
2017-07-22 21:57:20 +02:00
|
|
|
//
|
|
|
|
// `iris.WithOptimizations` is an optional configurator,
|
|
|
|
// if passed to the `Run` then it will ensure that the application
|
|
|
|
// response to the client as fast as possible.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// `iris.WithoutServerError` is an optional configurator,
|
|
|
|
// if passed to the `Run` then it will not print its passed error as an actual server error.
|
2020-04-28 04:22:58 +02:00
|
|
|
app.Listen(":8080", iris.WithOptimizations)
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
2022-04-13 00:11:31 +02:00
|
|
|
|
|
|
|
type errorHandler struct{}
|
|
|
|
|
|
|
|
func (h *errorHandler) HandleContextError(ctx iris.Context, err error) {
|
|
|
|
errors.Internal.Err(ctx, err)
|
|
|
|
}
|