README.md: example change

Former-commit-id: 4a66a689f86e5cd93ec9001e5ca18b4f5d3923be
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-07-23 15:58:55 +03:00
parent f7c0cbe5cd
commit 7337396e3c
5 changed files with 87 additions and 41 deletions

121
README.md
View File

@ -19,7 +19,7 @@ Thank you to all our backers! 🙏 [Become a backer](https://iris-go.com/donate)
<a href="https://iris-go.com/donate" target="_blank"><img src="https://iris-go.com/backers.svg?v=2"/></a> <a href="https://iris-go.com/donate" target="_blank"><img src="https://iris-go.com/backers.svg?v=2"/></a>
```sh ```sh
$ cat example.go $ cat _examples/cookies/basic/main.go
``` ```
```go ```go
@ -27,57 +27,102 @@ package main
import "github.com/kataras/iris" import "github.com/kataras/iris"
func main() { func newApp() *iris.Application {
app := iris.New() app := iris.New()
// Load all templates from the "./views" folder
// where extension is ".html" and parse them
// using the standard `html/template` package.
app.RegisterView(iris.HTML("./views", ".html"))
// Method: GET // Set A Cookie.
// Resource: http://localhost:8080 app.Get("/cookies/{name}/{value}", func(ctx iris.Context) {
app.Get("/", func(ctx iris.Context) { name := ctx.Params().Get("name")
// Bind: {{.message}} with "Hello world!" value := ctx.Params().Get("value")
ctx.ViewData("message", "Hello world!")
// Render template file: ./views/hello.html ctx.SetCookieKV(name, value)
ctx.View("hello.html")
ctx.Writef("cookie added: %s = %s", name, value)
}) })
// Method: GET // Retrieve A Cookie.
// Resource: http://localhost:8080/user/42 app.Get("/cookies/{name}", func(ctx iris.Context) {
// name := ctx.Params().Get("name")
// Need to use a custom regexp instead?
// Easy, value := ctx.GetCookie(name)
// just mark the parameter's type to 'string'
// which accepts anything and make use of ctx.WriteString(value)
// its `regexp` macro function, i.e:
// app.Get("/user/{id:string regexp(^[0-9]+$)}")
app.Get("/user/{id:long}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetInt64("id")
ctx.Writef("User ID: %d", userID)
}) })
// Start the server using a network address. // Delete A Cookie.
app.Delete("/cookies/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.RemoveCookie(name)
ctx.Writef("cookie %s removed", name)
})
return app
}
func main() {
app := newApp()
// GET: http://localhost:8080/cookies/my_name/my_value
// GET: http://localhost:8080/cookies/my_name
// DELETE: http://localhost:8080/cookies/my_name
app.Run(iris.Addr(":8080")) app.Run(iris.Addr(":8080"))
} }
``` ```
> Learn more about path parameter's types by clicking [here](_examples/routing/dynamic-path/main.go#L31) * Alternatively, use a regular `http.Cookie`: `ctx.SetCookie(&http.Cookie{...})`
* If you want to set custom the path: `ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored"))`.
* If you want to be available only to the current request path: `ctx.SetCookieKV(name, value, iris.CookieCleanPath /* or iris.CookiePath("") */)`
* `iris.CookieExpires(time.Duration)`
* `iris.CookieHTTPOnly(false)`
* `ctx.Request().Cookie(name)` is also available, it's the `net/http` approach
* Learn more about path parameter's types by clicking [here](_examples/routing/dynamic-path/main.go#L31).
```html ### Testing your Application? Easy with Iris
<!-- file: ./views/hello.html -->
<html> ```go
<head> package main
<title>Hello Page</title>
</head> import (
<body> "fmt"
<h1>{{.message}}</h1> "testing"
</body>
</html> "github.com/kataras/iris/httptest"
)
// go test -v -run=TestCookiesBasic$
func TestCookiesBasic(t *testing.T) {
app := newApp()
e := httptest.New(t, app, httptest.URL("http://example.com"))
cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
// Test Set A Cookie.
t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).Expect().Status(httptest.StatusOK)
t1.Cookie(cookieName).Value().Equal(cookieValue) // validate cookie's existence, it should be there now.
t1.Body().Contains(cookieValue)
path := fmt.Sprintf("/cookies/%s", cookieName)
// Test Retrieve A Cookie.
t2 := e.GET(path).Expect().Status(httptest.StatusOK)
t2.Body().Equal(cookieValue)
// Test Remove A Cookie.
t3 := e.DELETE(path).Expect().Status(httptest.StatusOK)
t3.Body().Contains(cookieName)
t4 := e.GET(path).Expect().Status(httptest.StatusOK)
t4.Cookies().Empty()
t4.Body().Empty()
}
``` ```
### Serve your Application
```sh ```sh
$ go run example.go $ go run main.go
Now listening on: http://localhost:8080 Now listening on: http://localhost:8080
Application Started. Press CTRL+C to shut down. Application Started. Press CTRL+C to shut down.
_ _

View File

@ -34,7 +34,7 @@ func main() {
app.Post("/upload", func(ctx iris.Context) { app.Post("/upload", func(ctx iris.Context) {
// //
// UploadFormFiles // UploadFormFiles
// uploads any number of incoming files (multiple property on the form input). // uploads any number of incoming files ("multiple" property on the form input).
// //
// second argument is totally optionally, // second argument is totally optionally,

View File

@ -27,6 +27,7 @@ func main() {
// Read // Read
app.Post("/decode", func(ctx iris.Context) { app.Post("/decode", func(ctx iris.Context) {
// Read https://github.com/kataras/iris/blob/master/_examples/http_request/read-json/main.go as well.
var user User var user User
ctx.ReadJSON(&user) ctx.ReadJSON(&user)

View File

@ -31,7 +31,7 @@ func (c *VisitController) Get() string {
visits := c.Session.Increment("visits", 1) visits := c.Session.Increment("visits", 1)
// write the current, updated visits. // write the current, updated visits.
since := time.Now().Sub(c.StartTime).Seconds() since := time.Now().Sub(c.StartTime).Seconds()
return fmt.Sprintf("%d visit from my current session in %0.1f seconds of server's up-time", return fmt.Sprintf("%d visit(s) from my current session in %0.1f seconds of server's up-time",
visits, since) visits, since)
} }

View File

@ -40,5 +40,5 @@ func main() {
// http://localhost:8080 // http://localhost:8080
app.StaticWeb("/", "./public") app.StaticWeb("/", "./public")
app.Run(iris.Addr("localhost:8080")) app.Run(iris.Addr("localhost:8080"), iris.WithoutPathCorrection)
} }