mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
README.md: example change
Former-commit-id: 4a66a689f86e5cd93ec9001e5ca18b4f5d3923be
This commit is contained in:
parent
f7c0cbe5cd
commit
7337396e3c
121
README.md
121
README.md
|
@ -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>
|
||||
|
||||
```sh
|
||||
$ cat example.go
|
||||
$ cat _examples/cookies/basic/main.go
|
||||
```
|
||||
|
||||
```go
|
||||
|
@ -27,57 +27,102 @@ package main
|
|||
|
||||
import "github.com/kataras/iris"
|
||||
|
||||
func main() {
|
||||
func newApp() *iris.Application {
|
||||
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
|
||||
// Resource: http://localhost:8080
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
// Bind: {{.message}} with "Hello world!"
|
||||
ctx.ViewData("message", "Hello world!")
|
||||
// Render template file: ./views/hello.html
|
||||
ctx.View("hello.html")
|
||||
// Set A Cookie.
|
||||
app.Get("/cookies/{name}/{value}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
value := ctx.Params().Get("value")
|
||||
|
||||
ctx.SetCookieKV(name, value)
|
||||
|
||||
ctx.Writef("cookie added: %s = %s", name, value)
|
||||
})
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/user/42
|
||||
//
|
||||
// Need to use a custom regexp instead?
|
||||
// Easy,
|
||||
// just mark the parameter's type to 'string'
|
||||
// which accepts anything and make use of
|
||||
// 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)
|
||||
// Retrieve A Cookie.
|
||||
app.Get("/cookies/{name}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
|
||||
value := ctx.GetCookie(name)
|
||||
|
||||
ctx.WriteString(value)
|
||||
})
|
||||
|
||||
// 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"))
|
||||
}
|
||||
```
|
||||
|
||||
> 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
|
||||
<!-- file: ./views/hello.html -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{.message}}</h1>
|
||||
</body>
|
||||
</html>
|
||||
### Testing your Application? Easy with Iris
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"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
|
||||
$ go run example.go
|
||||
$ go run main.go
|
||||
Now listening on: http://localhost:8080
|
||||
Application Started. Press CTRL+C to shut down.
|
||||
_
|
||||
|
|
|
@ -34,7 +34,7 @@ func main() {
|
|||
app.Post("/upload", func(ctx iris.Context) {
|
||||
//
|
||||
// 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,
|
||||
|
|
|
@ -27,6 +27,7 @@ func main() {
|
|||
|
||||
// Read
|
||||
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
|
||||
ctx.ReadJSON(&user)
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ func (c *VisitController) Get() string {
|
|||
visits := c.Session.Increment("visits", 1)
|
||||
// write the current, updated visits.
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
@ -40,5 +40,5 @@ func main() {
|
|||
// http://localhost:8080
|
||||
app.StaticWeb("/", "./public")
|
||||
|
||||
app.Run(iris.Addr("localhost:8080"))
|
||||
app.Run(iris.Addr("localhost:8080"), iris.WithoutPathCorrection)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user