mirror of
https://github.com/kataras/iris.git
synced 2026-05-14 18:13:49 +00:00
8.4.0 | New MVC Features | Refactor examples and godoc for go 1.9 use. Read HISTORY.md.
Former-commit-id: 90c05e743052bc3722e7fefaa0cbb0ed5153a1fb
This commit is contained in:
@@ -123,7 +123,7 @@ the template file via `Data` field.
|
||||
|
||||
Access to the template layout via the `Layout` field.
|
||||
|
||||
Access to the low-level `context.Context` via the `Ctx` field.
|
||||
Access to the low-level `iris.Context/context.Context` via the `Ctx` field.
|
||||
|
||||
Flow as you used to, `Controllers` can be registered to any `Party`,
|
||||
including Subdomains, the Party's begin and done handlers work as expected.
|
||||
@@ -238,7 +238,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) files to
|
||||
- [Stream Writer](http_responsewriter/stream-writer/main.go)
|
||||
- [Transactions](http_responsewriter/transactions/main.go)
|
||||
|
||||
> The `context.ResponseWriter()` returns an enchament version of a http.ResponseWriter, these examples show some places where the Context uses this object. Besides that you can use it as you did before iris.
|
||||
> The `context/context#ResponseWriter()` returns an enchament version of a http.ResponseWriter, these examples show some places where the Context uses this object. Besides that you can use it as you did before iris.
|
||||
|
||||
### ORM
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/basicauth"
|
||||
)
|
||||
|
||||
@@ -25,7 +24,7 @@ func newApp() *iris.Application {
|
||||
app.Get("/mysecret", authentication, h)
|
||||
*/
|
||||
|
||||
app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })
|
||||
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
|
||||
|
||||
// to party
|
||||
|
||||
@@ -49,7 +48,7 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func h(ctx context.Context) {
|
||||
func h(ctx iris.Context) {
|
||||
username, password, _ := ctx.Request().BasicAuth()
|
||||
// third parameter it will be always true because the middleware
|
||||
// makes sure for that, otherwise this handler will not be executed.
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
"sort"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
|
||||
@@ -95,7 +94,7 @@ func init() {
|
||||
// the URL query string. If you provide it in a different way,
|
||||
// assign your own function to this variable that returns the provider
|
||||
// name for your request.
|
||||
var GetProviderName = func(ctx context.Context) (string, error) {
|
||||
var GetProviderName = func(ctx iris.Context) (string, error) {
|
||||
// try to get it from the url param "provider"
|
||||
if p := ctx.URLParam("provider"); p != "" {
|
||||
return p, nil
|
||||
@@ -124,7 +123,7 @@ for the requested provider.
|
||||
|
||||
See https://github.com/markbates/goth/examples/main.go to see this in action.
|
||||
*/
|
||||
func BeginAuthHandler(ctx context.Context) {
|
||||
func BeginAuthHandler(ctx iris.Context) {
|
||||
url, err := GetAuthURL(ctx)
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
@@ -145,7 +144,7 @@ as either "provider" or ":provider" or from the context's value of "provider" ke
|
||||
I would recommend using the BeginAuthHandler instead of doing all of these steps
|
||||
yourself, but that's entirely up to you.
|
||||
*/
|
||||
func GetAuthURL(ctx context.Context) (string, error) {
|
||||
func GetAuthURL(ctx iris.Context) (string, error) {
|
||||
providerName, err := GetProviderName(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -173,7 +172,7 @@ func GetAuthURL(ctx context.Context) (string, error) {
|
||||
// If no state string is associated with the request, one will be generated.
|
||||
// This state is sent to the provider and can be retrieved during the
|
||||
// callback.
|
||||
var SetState = func(ctx context.Context) string {
|
||||
var SetState = func(ctx iris.Context) string {
|
||||
state := ctx.URLParam("state")
|
||||
if len(state) > 0 {
|
||||
return state
|
||||
@@ -186,7 +185,7 @@ var SetState = func(ctx context.Context) string {
|
||||
// GetState gets the state returned by the provider during the callback.
|
||||
// This is used to prevent CSRF attacks, see
|
||||
// http://tools.ietf.org/html/rfc6749#section-10.12
|
||||
var GetState = func(ctx context.Context) string {
|
||||
var GetState = func(ctx iris.Context) string {
|
||||
return ctx.URLParam("state")
|
||||
}
|
||||
|
||||
@@ -199,7 +198,7 @@ as either "provider" or ":provider".
|
||||
|
||||
See https://github.com/markbates/goth/examples/main.go to see this in action.
|
||||
*/
|
||||
var CompleteUserAuth = func(ctx context.Context) (goth.User, error) {
|
||||
var CompleteUserAuth = func(ctx iris.Context) (goth.User, error) {
|
||||
providerName, err := GetProviderName(ctx)
|
||||
if err != nil {
|
||||
return goth.User{}, err
|
||||
@@ -237,7 +236,7 @@ var CompleteUserAuth = func(ctx context.Context) (goth.User, error) {
|
||||
}
|
||||
|
||||
// Logout invalidates a user session.
|
||||
func Logout(ctx context.Context) error {
|
||||
func Logout(ctx iris.Context) error {
|
||||
providerName, err := GetProviderName(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -363,7 +362,7 @@ func main() {
|
||||
|
||||
// start of the router
|
||||
|
||||
app.Get("/auth/{provider}/callback", func(ctx context.Context) {
|
||||
app.Get("/auth/{provider}/callback", func(ctx iris.Context) {
|
||||
|
||||
user, err := CompleteUserAuth(ctx)
|
||||
if err != nil {
|
||||
@@ -377,12 +376,12 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/logout/{provider}", func(ctx context.Context) {
|
||||
app.Get("/logout/{provider}", func(ctx iris.Context) {
|
||||
Logout(ctx)
|
||||
ctx.Redirect("/", iris.StatusTemporaryRedirect)
|
||||
})
|
||||
|
||||
app.Get("/auth/{provider}", func(ctx context.Context) {
|
||||
app.Get("/auth/{provider}", func(ctx iris.Context) {
|
||||
// try to get the user without re-authenticating
|
||||
if gothUser, err := CompleteUserAuth(ctx); err == nil {
|
||||
ctx.ViewData("", gothUser)
|
||||
@@ -394,7 +393,7 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
|
||||
ctx.ViewData("", providerIndex)
|
||||
|
||||
|
||||
3
_examples/cache/simple/main.go
vendored
3
_examples/cache/simple/main.go
vendored
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/cache"
|
||||
)
|
||||
@@ -67,7 +66,7 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func writeMarkdown(ctx context.Context) {
|
||||
func writeMarkdown(ctx iris.Context) {
|
||||
// tap multiple times the browser's refresh button and you will
|
||||
// see this println only once every 10 seconds.
|
||||
println("Handler executed. Content refreshed.")
|
||||
|
||||
@@ -2,12 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello!</b>")
|
||||
})
|
||||
// [...]
|
||||
|
||||
@@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello!</b>")
|
||||
})
|
||||
// [...]
|
||||
|
||||
@@ -2,12 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello!</b>")
|
||||
})
|
||||
// [...]
|
||||
|
||||
@@ -2,12 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello!</b>")
|
||||
})
|
||||
// [...]
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -13,14 +12,14 @@ func main() {
|
||||
app.Use(ionMiddleware)
|
||||
|
||||
// Method GET: http://localhost:8080/
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1> Home </h1>")
|
||||
// this will print an error,
|
||||
// this route's handler will never be executed because the middleware's criteria not passed.
|
||||
})
|
||||
|
||||
// Method GET: http://localhost:8080/ok
|
||||
app.Get("/ok", func(ctx context.Context) {
|
||||
app.Get("/ok", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello world!")
|
||||
// this will print "OK. Hello world!".
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -13,12 +12,12 @@ func main() {
|
||||
app.Use(ionMiddleware)
|
||||
|
||||
// Method GET: http://localhost:8080/
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("Home")
|
||||
})
|
||||
|
||||
// Method GET: http://localhost:8080/ok
|
||||
app.Get("/ok", func(ctx context.Context) {
|
||||
app.Get("/ok", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello world!</b>")
|
||||
})
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -14,7 +13,7 @@ func main() {
|
||||
// app.Favicon("./static/favicons/favicon.ico.ico", "/favicon_16_16.ico")
|
||||
// This will serve the ./static/favicons/favicon.ico.ico to: localhost:8080/favicon_16_16.ico
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML(`<a href="/favicon.ico"> press here to see the favicon.ico</a>.
|
||||
At some browsers like chrome, it should be visible at the top-left side of the browser's window,
|
||||
because some browsers make requests to the /favicon.ico automatically,
|
||||
|
||||
@@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
file := "./files/first.zip"
|
||||
ctx.SendFile(file, "c.zip")
|
||||
})
|
||||
|
||||
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
// same as embedded-single-page-application but without go-bindata, the files are "physical" stored in the
|
||||
@@ -15,15 +13,15 @@ var page = struct {
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.RegisterView(view.HTML("./public", ".html"))
|
||||
app.RegisterView(iris.HTML("./public", ".html"))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ViewData("Page", page)
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
// or just serve index.html as it is:
|
||||
// app.Get("/", func(ctx context.Context) {
|
||||
// app.Get("/", func(ctx iris.Context) {
|
||||
// ctx.ServeFile("index.html", false)
|
||||
// })
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
// $ go get -u github.com/jteeuwen/go-bindata/...
|
||||
@@ -17,9 +15,9 @@ var page = struct {
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.RegisterView(view.HTML("./public", ".html").Binary(Asset, AssetNames))
|
||||
app.RegisterView(iris.HTML("./public", ".html").Binary(Asset, AssetNames))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ViewData("Page", page)
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
// +build !go1.9
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
"github.com/kataras/iris/middleware/recover"
|
||||
@@ -20,21 +17,21 @@ func main() {
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/
|
||||
app.Handle("GET", "/", func(ctx context.Context) {
|
||||
app.Handle("GET", "/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Welcome!</b>")
|
||||
})
|
||||
|
||||
// same as app.Handle("GET", "/ping", [...])
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/ping
|
||||
app.Get("/ping", func(ctx context.Context) {
|
||||
app.Get("/ping", func(ctx iris.Context) {
|
||||
ctx.WriteString("pong")
|
||||
})
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/hello
|
||||
app.Get("/hello", func(ctx context.Context) {
|
||||
ctx.JSON(context.Map{"message": "Hello iris web framework."})
|
||||
app.Get("/hello", func(ctx iris.Context) {
|
||||
ctx.JSON(iris.Map{"message": "Hello iris web framework."})
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
// +build go1.9
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// Same as `main.go` for go1.8+ but it omits the
|
||||
// `github.com/kataras/iris/context` import path
|
||||
// because of the type alias feature of go 1.9.
|
||||
|
||||
func main() {
|
||||
// The `iris#Default` adds two built'n handlers
|
||||
// that can recover from any http-relative panics
|
||||
// and log the requests to the terminal.
|
||||
//
|
||||
// Use `iris#New` instead.
|
||||
app := iris.Default()
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/
|
||||
app.Handle("GET", "/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello world!</b>")
|
||||
})
|
||||
|
||||
// same as app.Handle("GET", "/ping", [...])
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/ping
|
||||
app.Get("/ping", func(ctx iris.Context) {
|
||||
ctx.WriteString("pong")
|
||||
})
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/hello
|
||||
app.Get("/hello", func(ctx iris.Context) {
|
||||
ctx.JSON(iris.Map{"message": "Hello iris web framework."})
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/ping
|
||||
// http://localhost:8080/hello
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -4,17 +4,16 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx context.Context) {
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
|
||||
@@ -4,17 +4,16 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx context.Context) {
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
|
||||
@@ -4,17 +4,16 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx context.Context) {
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
|
||||
@@ -4,17 +4,16 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx context.Context) {
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
"github.com/valyala/tcplisten"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// $ go get github.com/valyala/tcplisten
|
||||
@@ -26,7 +25,7 @@ import (
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello World!</b>")
|
||||
})
|
||||
|
||||
|
||||
@@ -8,13 +8,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||
})
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// Before continue:
|
||||
@@ -26,7 +25,7 @@ func main() {
|
||||
app.Shutdown(ctx)
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/host"
|
||||
)
|
||||
|
||||
@@ -25,7 +24,7 @@ func Configurator(app *iris.Application) {
|
||||
}) // or put the ticker outside of the gofunc and put the configurator before or after the app.Get, outside of this gofunc
|
||||
}()
|
||||
|
||||
app.Get("/counter", func(ctx context.Context) {
|
||||
app.Get("/counter", func(ctx iris.Context) {
|
||||
ctx.Writef("Counter value = %d", counterValue)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello World!</h1>")
|
||||
})
|
||||
|
||||
|
||||
@@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello World!</h1>")
|
||||
})
|
||||
|
||||
|
||||
@@ -3,21 +3,20 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from SECURE SERVER!")
|
||||
})
|
||||
|
||||
app.Get("/test2", func(ctx context.Context) {
|
||||
app.Get("/test2", func(ctx iris.Context) {
|
||||
ctx.Writef("Welcome to secure server from /test2!")
|
||||
})
|
||||
|
||||
app.Get("/redirect", func(ctx context.Context) {
|
||||
app.Get("/redirect", func(ctx iris.Context) {
|
||||
ctx.Redirect("/test2")
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/core/host"
|
||||
)
|
||||
@@ -12,11 +11,11 @@ import (
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the SECURE server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx context.Context) {
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the SECURE server on path /mypath")
|
||||
})
|
||||
|
||||
|
||||
@@ -5,14 +5,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/host"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello, try to refresh the page after ~10 secs</h1>")
|
||||
})
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
type Visitor struct {
|
||||
@@ -17,16 +15,16 @@ func main() {
|
||||
app := iris.New()
|
||||
|
||||
// set the view html template engine
|
||||
app.RegisterView(view.HTML("./templates", ".html").Reload(true))
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
if err := ctx.View("form.html"); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.WriteString(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Post("/form_action", func(ctx context.Context) {
|
||||
app.Post("/form_action", func(ctx iris.Context) {
|
||||
visitor := Visitor{}
|
||||
err := ctx.ReadForm(&visitor)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
type Company struct {
|
||||
@@ -11,7 +10,7 @@ type Company struct {
|
||||
Other string
|
||||
}
|
||||
|
||||
func MyHandler(ctx context.Context) {
|
||||
func MyHandler(ctx iris.Context) {
|
||||
c := &Company{}
|
||||
if err := ctx.ReadJSON(c); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
)
|
||||
|
||||
@@ -28,7 +27,7 @@ func main() {
|
||||
|
||||
app.Use(customLogger)
|
||||
|
||||
h := func(ctx context.Context) {
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
app.Get("/", h)
|
||||
@@ -40,12 +39,12 @@ func main() {
|
||||
// http errors have their own handlers, therefore
|
||||
// registering a middleare should be done manually.
|
||||
/*
|
||||
app.OnErrorCode(404 ,customLogger, func(ctx context.Context) {
|
||||
app.OnErrorCode(404 ,customLogger, func(ctx iris.Context) {
|
||||
ctx.Writef("My Custom 404 error page ")
|
||||
})
|
||||
*/
|
||||
// or catch all http errors:
|
||||
app.OnAnyErrorCode(customLogger, func(ctx context.Context) {
|
||||
app.OnAnyErrorCode(customLogger, func(ctx iris.Context) {
|
||||
// this should be added to the logs, at the end because of the `logger.Config#MessageContextKey`
|
||||
ctx.Values().Set("logger_message",
|
||||
"a dynamic message passed to the logs")
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
)
|
||||
|
||||
@@ -18,11 +17,11 @@ func main() {
|
||||
defer close()
|
||||
|
||||
app.Use(r)
|
||||
app.OnAnyErrorCode(r, func(ctx context.Context) {
|
||||
app.OnAnyErrorCode(r, func(ctx iris.Context) {
|
||||
ctx.HTML("<h1> Error: Please try <a href ='/'> this </a> instead.</h1>")
|
||||
})
|
||||
|
||||
h := func(ctx context.Context) {
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
|
||||
@@ -66,7 +65,7 @@ var excludeExtensions = [...]string{
|
||||
".svg",
|
||||
}
|
||||
|
||||
func newRequestLogger() (h context.Handler, close func() error) {
|
||||
func newRequestLogger() (h iris.Handler, close func() error) {
|
||||
close = func() error { return nil }
|
||||
|
||||
c := logger.Config{
|
||||
@@ -93,7 +92,7 @@ func newRequestLogger() (h context.Handler, close func() error) {
|
||||
|
||||
// we don't want to use the logger
|
||||
// to log requests to assets and etc
|
||||
c.AddSkipper(func(ctx context.Context) bool {
|
||||
c.AddSkipper(func(ctx iris.Context) bool {
|
||||
path := ctx.Path()
|
||||
for _, ext := range excludeExtensions {
|
||||
if strings.HasSuffix(path, ext) {
|
||||
|
||||
@@ -9,17 +9,15 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.RegisterView(view.HTML("./templates", ".html"))
|
||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||
|
||||
// Serve the form.html to the user
|
||||
app.Get("/upload", func(ctx context.Context) {
|
||||
app.Get("/upload", func(ctx iris.Context) {
|
||||
//create a token (optionally)
|
||||
|
||||
now := time.Now().Unix()
|
||||
@@ -33,8 +31,8 @@ func main() {
|
||||
})
|
||||
|
||||
// Handle the post request from the upload_form.html to the server
|
||||
app.Post("/upload", context.LimitRequestBodySize(10<<20),
|
||||
func(ctx context.Context) {
|
||||
app.Post("/upload", iris.LimitRequestBodySize(10<<20),
|
||||
func(ctx iris.Context) {
|
||||
// or use ctx.SetMaxRequestBodySize(10 << 20)
|
||||
//to limit the uploaded file(s) size.
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ package controllers
|
||||
import (
|
||||
"github.com/kataras/iris/_examples/http_responsewriter/quicktemplate/templates"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// ExecuteTemplate renders a "tmpl" partial template to the `context#ResponseWriter`.
|
||||
func ExecuteTemplate(ctx context.Context, tmpl templates.Partial) {
|
||||
func ExecuteTemplate(ctx iris.Context, tmpl templates.Partial) {
|
||||
ctx.Gzip(true)
|
||||
ctx.ContentType("text/html")
|
||||
templates.WriteTemplate(ctx.ResponseWriter(), tmpl)
|
||||
|
||||
@@ -3,11 +3,11 @@ package controllers
|
||||
import (
|
||||
"github.com/kataras/iris/_examples/http_responsewriter/quicktemplate/templates"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// Hello renders our ../templates/hello.qtpl file using the compiled ../templates/hello.qtpl.go file.
|
||||
func Hello(ctx context.Context) {
|
||||
func Hello(ctx iris.Context) {
|
||||
// vars := make(map[string]interface{})
|
||||
// vars["message"] = "Hello World!"
|
||||
// vars["name"] = ctx.Params().Get("name")
|
||||
|
||||
@@ -3,11 +3,11 @@ package controllers
|
||||
import (
|
||||
"github.com/kataras/iris/_examples/http_responsewriter/quicktemplate/templates"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// Index renders our ../templates/index.qtpl file using the compiled ../templates/index.qtpl.go file.
|
||||
func Index(ctx context.Context) {
|
||||
func Index(ctx iris.Context) {
|
||||
tmpl := &templates.Index{}
|
||||
|
||||
// render the template
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time" // showcase the delay
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -14,7 +13,7 @@ func main() {
|
||||
|
||||
timeWaitForCloseStream := 4 * time.Second
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
i := 0
|
||||
// goroutine in order to no block and just wait,
|
||||
// goroutine is OPTIONAL and not a very good option but it depends on the needs
|
||||
@@ -35,7 +34,7 @@ func main() {
|
||||
time.Sleep(timeWaitForCloseStream)
|
||||
})
|
||||
|
||||
app.Get("/alternative", func(ctx context.Context) {
|
||||
app.Get("/alternative", func(ctx iris.Context) {
|
||||
// Send the response in chunks and wait for a second between each chunk.
|
||||
ctx.StreamWriter(func(w io.Writer) bool {
|
||||
for i := 1; i <= 4; i++ {
|
||||
|
||||
@@ -26,7 +26,7 @@ func main() {
|
||||
app := iris.New()
|
||||
|
||||
// Read
|
||||
app.Post("/decode", func(ctx context.Context) {
|
||||
app.Post("/decode", func(ctx iris.Context) {
|
||||
var user User
|
||||
ctx.ReadJSON(&user)
|
||||
|
||||
@@ -34,7 +34,7 @@ func main() {
|
||||
})
|
||||
|
||||
// Write
|
||||
app.Get("/encode", func(ctx context.Context) {
|
||||
app.Get("/encode", func(ctx iris.Context) {
|
||||
peter := User{
|
||||
Firstname: "John",
|
||||
Lastname: "Doe",
|
||||
@@ -48,28 +48,28 @@ func main() {
|
||||
|
||||
// Other content types,
|
||||
|
||||
app.Get("/binary", func(ctx context.Context) {
|
||||
app.Get("/binary", func(ctx iris.Context) {
|
||||
// useful when you want force-download of contents of raw bytes form.
|
||||
ctx.Binary([]byte("Some binary data here."))
|
||||
})
|
||||
|
||||
app.Get("/text", func(ctx context.Context) {
|
||||
app.Get("/text", func(ctx iris.Context) {
|
||||
ctx.Text("Plain text here")
|
||||
})
|
||||
|
||||
app.Get("/json", func(ctx context.Context) {
|
||||
app.Get("/json", func(ctx iris.Context) {
|
||||
ctx.JSON(map[string]string{"hello": "json"}) // or myjsonStruct{hello:"json}
|
||||
})
|
||||
|
||||
app.Get("/jsonp", func(ctx context.Context) {
|
||||
app.Get("/jsonp", func(ctx iris.Context) {
|
||||
ctx.JSONP(map[string]string{"hello": "jsonp"}, context.JSONP{Callback: "callbackName"})
|
||||
})
|
||||
|
||||
app.Get("/xml", func(ctx context.Context) {
|
||||
ctx.XML(ExampleXML{One: "hello", Two: "xml"}) // or context.Map{"One":"hello"...}
|
||||
app.Get("/xml", func(ctx iris.Context) {
|
||||
ctx.XML(ExampleXML{One: "hello", Two: "xml"}) // or iris.Map{"One":"hello"...}
|
||||
})
|
||||
|
||||
app.Get("/markdown", func(ctx context.Context) {
|
||||
app.Get("/markdown", func(ctx iris.Context) {
|
||||
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
|
||||
})
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// get a filename based on the date, file logs works that way the most times
|
||||
@@ -34,7 +33,7 @@ func main() {
|
||||
// attach the file as logger, remember, iris' app logger is just an io.Writer.
|
||||
app.Logger().SetOutput(newLogFile())
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
// for the sake of simplicity, in order see the logs at the ./_today_.txt
|
||||
ctx.Application().Logger().Info("Request path: " + ctx.Path())
|
||||
ctx.Writef("hello")
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/i18n"
|
||||
)
|
||||
|
||||
@@ -17,7 +16,7 @@ func newApp() *iris.Application {
|
||||
"el-GR": "./locales/locale_el-GR.ini",
|
||||
"zh-CN": "./locales/locale_zh-CN.ini"}}))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
|
||||
// it tries to find the language by:
|
||||
// ctx.Values().GetString("language")
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/middleware/pprof"
|
||||
)
|
||||
@@ -10,7 +9,7 @@ import (
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1> Please click <a href='/debug/pprof'>here</a>")
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/middleware/recaptcha"
|
||||
)
|
||||
@@ -34,12 +33,12 @@ var htmlForm = `<form action="/comment" method="POST">
|
||||
<input type="submit" name="button" value="Verify">
|
||||
</form>`
|
||||
|
||||
func showRecaptchaForm(ctx context.Context) {
|
||||
func showRecaptchaForm(ctx iris.Context) {
|
||||
contents := fmt.Sprintf(htmlForm, publicDataSiteKey)
|
||||
ctx.HTML(contents)
|
||||
}
|
||||
|
||||
func postComment(ctx context.Context) {
|
||||
func postComment(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.JSON(context.Map{"success": true})
|
||||
ctx.JSON(iris.Map{"success": true})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/middleware/recover"
|
||||
)
|
||||
@@ -14,7 +13,7 @@ func main() {
|
||||
|
||||
i := 0
|
||||
// let's simmilate a panic every next request
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
i++
|
||||
if i%2 == 0 {
|
||||
panic("a panic here")
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
)
|
||||
|
||||
@@ -29,7 +29,7 @@ type AuthController struct {
|
||||
}
|
||||
|
||||
// BeginRequest saves login state to the context, the user id.
|
||||
func (c *AuthController) BeginRequest(ctx context.Context) {
|
||||
func (c *AuthController) BeginRequest(ctx iris.Context) {
|
||||
c.SessionController.BeginRequest(ctx)
|
||||
|
||||
if userID := c.Session.Get(sessionIDKey); userID != nil {
|
||||
@@ -116,7 +116,7 @@ func (c *AuthController) logout() {
|
||||
// AllowUser will check if this client is a logged user,
|
||||
// if not then it will redirect that guest to the login page
|
||||
// otherwise it will allow the execution of the next handler.
|
||||
func AllowUser(ctx context.Context) {
|
||||
func AllowUser(ctx iris.Context) {
|
||||
if ctx.Values().Get(sessionIDKey) != nil {
|
||||
ctx.Next()
|
||||
return
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -55,14 +54,14 @@ func main() {
|
||||
app.Logger().Fatalf("orm failed to initialized User table: %v", err)
|
||||
}
|
||||
|
||||
app.Get("/insert", func(ctx context.Context) {
|
||||
app.Get("/insert", func(ctx iris.Context) {
|
||||
user := &User{Username: "kataras", Salt: "hash---", Password: "hashed", CreatedAt: time.Now(), UpdatedAt: time.Now()}
|
||||
orm.Insert(user)
|
||||
|
||||
ctx.Writef("user inserted: %#v", user)
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
user := User{ID: 1}
|
||||
if ok, _ := orm.Get(&user); ok {
|
||||
ctx.Writef("user found: %#v", user)
|
||||
|
||||
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
// User is just a bindable object structure.
|
||||
@@ -22,10 +20,10 @@ func main() {
|
||||
// Define templates using the std html/template engine.
|
||||
// Parse and load all files inside "./views" folder with ".html" file extension.
|
||||
// Reload the templates on each request (development mode).
|
||||
app.RegisterView(view.HTML("./views", ".html").Reload(true))
|
||||
app.RegisterView(iris.HTML("./views", ".html").Reload(true))
|
||||
|
||||
// Register custom handler for specific http errors.
|
||||
app.OnErrorCode(iris.StatusInternalServerError, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) {
|
||||
// .Values are used to communicate between handlers, middleware.
|
||||
errMessage := ctx.Values().GetString("error")
|
||||
if errMessage != "" {
|
||||
@@ -36,22 +34,22 @@ func main() {
|
||||
ctx.Writef("(Unexpected) internal server error")
|
||||
})
|
||||
|
||||
app.Use(func(ctx context.Context) {
|
||||
app.Use(func(ctx iris.Context) {
|
||||
ctx.Application().Logger().Infof("Begin request for path: %s", ctx.Path())
|
||||
ctx.Next()
|
||||
})
|
||||
// app.Done(func(ctx context.Context) {]})
|
||||
app.Subdomain("wtf.").Post("/decode", func(ctx context.Context) {})
|
||||
app.Subdomain("wtf.").Post("/decode", func(ctx context.Context) {})
|
||||
// app.Done(func(ctx iris.Context) {]})
|
||||
app.Subdomain("wtf.").Post("/decode", func(ctx iris.Context) {})
|
||||
app.Subdomain("wtf.").Post("/decode", func(ctx iris.Context) {})
|
||||
// Method POST: http://localhost:8080/decode
|
||||
app.Post("/decode", func(ctx context.Context) {
|
||||
app.Post("/decode", func(ctx iris.Context) {
|
||||
var user User
|
||||
ctx.ReadJSON(&user)
|
||||
ctx.Writef("%s %s is %d years old and comes from %s", user.Firstname, user.Lastname, user.Age, user.City)
|
||||
})
|
||||
|
||||
// Method GET: http://localhost:8080/encode
|
||||
app.Get("/encode", func(ctx context.Context) {
|
||||
app.Get("/encode", func(ctx iris.Context) {
|
||||
doe := User{
|
||||
Username: "Johndoe",
|
||||
Firstname: "John",
|
||||
@@ -78,7 +76,7 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8"))
|
||||
}
|
||||
|
||||
func logThisMiddleware(ctx context.Context) {
|
||||
func logThisMiddleware(ctx iris.Context) {
|
||||
ctx.Application().Logger().Infof("Path: %s | IP: %s", ctx.Path(), ctx.RemoteAddr())
|
||||
|
||||
// .Next is required to move forward to the chain of handlers,
|
||||
@@ -86,7 +84,7 @@ func logThisMiddleware(ctx context.Context) {
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
func profileByUsername(ctx context.Context) {
|
||||
func profileByUsername(ctx iris.Context) {
|
||||
// .Params are used to get dynamic path parameters.
|
||||
username := ctx.Params().Get("username")
|
||||
ctx.ViewData("Username", username)
|
||||
@@ -95,7 +93,7 @@ func profileByUsername(ctx context.Context) {
|
||||
ctx.View("users/profile.html")
|
||||
}
|
||||
|
||||
func getUserByID(ctx context.Context) {
|
||||
func getUserByID(ctx iris.Context) {
|
||||
userID := ctx.Params().Get("id") // Or convert directly using: .Values().GetInt/GetInt64 etc...
|
||||
// your own db fetch here instead of user :=...
|
||||
user := User{Username: "username" + userID}
|
||||
@@ -103,7 +101,7 @@ func getUserByID(ctx context.Context) {
|
||||
ctx.XML(user)
|
||||
}
|
||||
|
||||
func createUser(ctx context.Context) {
|
||||
func createUser(ctx iris.Context) {
|
||||
var user User
|
||||
err := ctx.ReadForm(&user)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -14,23 +13,23 @@ func main() {
|
||||
|
||||
// GET -> HTTP Method
|
||||
// / -> Path
|
||||
// func(ctx context.Context) -> The route's handler.
|
||||
// func(ctx iris.Context) -> The route's handler.
|
||||
//
|
||||
// Third receiver should contains the route's handler(s), they are executed by order.
|
||||
app.Handle("GET", "/", func(ctx context.Context) {
|
||||
app.Handle("GET", "/", func(ctx iris.Context) {
|
||||
// navigate to the middle of $GOPATH/src/github.com/kataras/iris/context/context.go
|
||||
// to overview all context's method (there a lot of them, read that and you will learn how iris works too)
|
||||
ctx.HTML("Hello from " + ctx.Path()) // Hello from /
|
||||
})
|
||||
|
||||
app.Get("/home", func(ctx context.Context) {
|
||||
app.Get("/home", func(ctx iris.Context) {
|
||||
ctx.Writef(`Same as app.Handle("GET", "/", [...])`)
|
||||
})
|
||||
|
||||
app.Get("/donate", donateHandler, donateFinishHandler)
|
||||
|
||||
// Pssst, don't forget dynamic-path example for more "magic"!
|
||||
app.Get("/api/users/{userid:int min(1)}", func(ctx context.Context) {
|
||||
app.Get("/api/users/{userid:int min(1)}", func(ctx iris.Context) {
|
||||
userID, err := ctx.Params().GetInt("userid")
|
||||
|
||||
if err != nil {
|
||||
@@ -45,15 +44,15 @@ func main() {
|
||||
"user_id": userID,
|
||||
})
|
||||
})
|
||||
// app.Post("/", func(ctx context.Context){}) -> for POST http method.
|
||||
// app.Put("/", func(ctx context.Context){})-> for "PUT" http method.
|
||||
// app.Delete("/", func(ctx context.Context){})-> for "DELETE" http method.
|
||||
// app.Options("/", func(ctx context.Context){})-> for "OPTIONS" http method.
|
||||
// app.Trace("/", func(ctx context.Context){})-> for "TRACE" http method.
|
||||
// app.Head("/", func(ctx context.Context){})-> for "HEAD" http method.
|
||||
// app.Connect("/", func(ctx context.Context){})-> for "CONNECT" http method.
|
||||
// app.Patch("/", func(ctx context.Context){})-> for "PATCH" http method.
|
||||
// app.Any("/", func(ctx context.Context){}) for all http methods.
|
||||
// app.Post("/", func(ctx iris.Context){}) -> for POST http method.
|
||||
// app.Put("/", func(ctx iris.Context){})-> for "PUT" http method.
|
||||
// app.Delete("/", func(ctx iris.Context){})-> for "DELETE" http method.
|
||||
// app.Options("/", func(ctx iris.Context){})-> for "OPTIONS" http method.
|
||||
// app.Trace("/", func(ctx iris.Context){})-> for "TRACE" http method.
|
||||
// app.Head("/", func(ctx iris.Context){})-> for "HEAD" http method.
|
||||
// app.Connect("/", func(ctx iris.Context){})-> for "CONNECT" http method.
|
||||
// app.Patch("/", func(ctx iris.Context){})-> for "PATCH" http method.
|
||||
// app.Any("/", func(ctx iris.Context){}) for all http methods.
|
||||
|
||||
// More than one route can contain the same path with a different http mapped method.
|
||||
// You can catch any route creation errors with:
|
||||
@@ -64,13 +63,13 @@ func main() {
|
||||
|
||||
adminRoutes := app.Party("/admin", adminMiddleware)
|
||||
|
||||
adminRoutes.Done(func(ctx context.Context) { // executes always last if ctx.Next()
|
||||
adminRoutes.Done(func(ctx iris.Context) { // executes always last if ctx.Next()
|
||||
ctx.Application().Logger().Infof("response sent to " + ctx.Path())
|
||||
})
|
||||
// adminRoutes.Layout("/views/layouts/admin.html") // set a view layout for these routes, see more at view examples.
|
||||
|
||||
// GET: http://localhost:8080/admin
|
||||
adminRoutes.Get("/", func(ctx context.Context) {
|
||||
adminRoutes.Get("/", func(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.StatusCode(iris.StatusOK) // default is 200 == iris.StatusOK
|
||||
ctx.HTML("<h1>Hello from admin/</h1>")
|
||||
@@ -79,11 +78,11 @@ func main() {
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/admin/login
|
||||
adminRoutes.Get("/login", func(ctx context.Context) {
|
||||
adminRoutes.Get("/login", func(ctx iris.Context) {
|
||||
// [...]
|
||||
})
|
||||
// POST: http://localhost:8080/admin/login
|
||||
adminRoutes.Post("/login", func(ctx context.Context) {
|
||||
adminRoutes.Post("/login", func(ctx iris.Context) {
|
||||
// [...]
|
||||
})
|
||||
|
||||
@@ -93,18 +92,18 @@ func main() {
|
||||
{ // braces are optional, it's just type of style, to group the routes visually.
|
||||
|
||||
// http://v1.localhost:8080
|
||||
v1.Get("/", func(ctx context.Context) {
|
||||
v1.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("Version 1 API. go to <a href='" + ctx.Path() + "/api" + "'>/api/users</a>")
|
||||
})
|
||||
|
||||
usersAPI := v1.Party("/api/users")
|
||||
{
|
||||
// http://v1.localhost:8080/api/users
|
||||
usersAPI.Get("/", func(ctx context.Context) {
|
||||
usersAPI.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("All users")
|
||||
})
|
||||
// http://v1.localhost:8080/api/users/42
|
||||
usersAPI.Get("/{userid:int}", func(ctx context.Context) {
|
||||
usersAPI.Get("/{userid:int}", func(ctx iris.Context) {
|
||||
ctx.Writef("user with id: %s", ctx.Params().Get("userid"))
|
||||
})
|
||||
}
|
||||
@@ -113,7 +112,7 @@ func main() {
|
||||
// wildcard subdomains.
|
||||
wildcardSubdomain := app.Party("*.")
|
||||
{
|
||||
wildcardSubdomain.Get("/", func(ctx context.Context) {
|
||||
wildcardSubdomain.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Subdomain can be anything, now you're here from: %s", ctx.Subdomain())
|
||||
})
|
||||
}
|
||||
@@ -137,22 +136,22 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func adminMiddleware(ctx context.Context) {
|
||||
func adminMiddleware(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.Next() // to move to the next handler, or don't that if you have any auth logic.
|
||||
}
|
||||
|
||||
func donateHandler(ctx context.Context) {
|
||||
func donateHandler(ctx iris.Context) {
|
||||
ctx.Writef("Just like an inline handler, but it can be " +
|
||||
"used by other package, anywhere in your project.")
|
||||
|
||||
// let's pass a value to the next handler
|
||||
// Values is the way handlers(or middleware) are communicating between each other.
|
||||
ctx.Values().Set("donate_url", "https://github.com/kataras/iris#buy-me-a-cup-of-coffee")
|
||||
ctx.Values().Set("donate_url", "https://github.com/kataras/iris#-people")
|
||||
ctx.Next() // in order to execute the next handler in the chain, look donate route.
|
||||
}
|
||||
|
||||
func donateFinishHandler(ctx context.Context) {
|
||||
func donateFinishHandler(ctx iris.Context) {
|
||||
// values can be any type of object so we could cast the value to a string
|
||||
// but iris provides an easy to do that, if donate_url is not defined, then it returns an empty string instead.
|
||||
donateURL := ctx.Values().GetString("donate_url")
|
||||
@@ -160,7 +159,7 @@ func donateFinishHandler(ctx context.Context) {
|
||||
ctx.Writef("\n\nDonate sent(?).")
|
||||
}
|
||||
|
||||
func notFoundHandler(ctx context.Context) {
|
||||
func notFoundHandler(ctx iris.Context) {
|
||||
ctx.HTML("Custom route for 404 not found http code, here you can render a view, html, json <b>any valid response</b>.")
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
@@ -25,7 +24,7 @@ var owner = &Owner{
|
||||
// Let's implement a context which will give us access
|
||||
// to the client's Session with a trivial `ctx.Session()` call.
|
||||
type Context struct {
|
||||
context.Context
|
||||
iris.Context
|
||||
session *sessions.Session
|
||||
}
|
||||
|
||||
@@ -49,7 +48,7 @@ var contextPool = sync.Pool{New: func() interface{} {
|
||||
return &Context{}
|
||||
}}
|
||||
|
||||
func acquire(original context.Context) *Context {
|
||||
func acquire(original iris.Context) *Context {
|
||||
ctx := contextPool.Get().(*Context)
|
||||
ctx.Context = original // set the context to the original one in order to have access to iris's implementation.
|
||||
ctx.session = nil // reset the session
|
||||
@@ -62,8 +61,8 @@ func release(ctx *Context) {
|
||||
|
||||
// Handler will convert our handler of func(*Context) to an iris Handler,
|
||||
// in order to be compatible with the HTTP API.
|
||||
func Handler(h func(*Context)) context.Handler {
|
||||
return func(original context.Context) {
|
||||
func Handler(h func(*Context)) iris.Handler {
|
||||
return func(original iris.Context) {
|
||||
ctx := acquire(original)
|
||||
h(ctx)
|
||||
release(ctx)
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// In this example you'll just see one use case of .WrapRouter.
|
||||
@@ -20,15 +19,15 @@ func newApp() *iris.Application {
|
||||
|
||||
app := iris.New()
|
||||
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Resource Not found</b>")
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ServeFile("./public/index.html", false)
|
||||
})
|
||||
|
||||
app.Get("/profile/{username}", func(ctx context.Context) {
|
||||
app.Get("/profile/{username}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("username"))
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -15,7 +14,7 @@ func main() {
|
||||
// with a single known paramete and custom http errors, now it's time to see wildcard parameters and macros.
|
||||
|
||||
// iris, like net/http std package registers route's handlers
|
||||
// by a Handler, the iris' type of handler is just a func(ctx context.Context)
|
||||
// by a Handler, the iris' type of handler is just a func(ctx iris.Context)
|
||||
// where context comes from github.com/kataras/iris/context.
|
||||
// Until go 1.9 you will have to import that package too, after go 1.9 this will be not be necessary.
|
||||
//
|
||||
@@ -92,7 +91,7 @@ func main() {
|
||||
// })
|
||||
|
||||
// you can use the "string" type which is valid for a single path parameter that can be anything.
|
||||
app.Get("/username/{name}", func(ctx context.Context) {
|
||||
app.Get("/username/{name}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("name"))
|
||||
}) // type is missing = {name:string}
|
||||
|
||||
@@ -116,7 +115,7 @@ func main() {
|
||||
// http://localhost:8080/profile/id>=1
|
||||
// this will throw 404 even if it's found as route on : /profile/0, /profile/blabla, /profile/-1
|
||||
// macro parameter functions are optional of course.
|
||||
app.Get("/profile/{id:int min(1)}", func(ctx context.Context) {
|
||||
app.Get("/profile/{id:int min(1)}", func(ctx iris.Context) {
|
||||
// second parameter is the error but it will always nil because we use macros,
|
||||
// the validaton already happened.
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
@@ -124,7 +123,7 @@ func main() {
|
||||
})
|
||||
|
||||
// to change the error code per route's macro evaluator:
|
||||
app.Get("/profile/{id:int min(1)}/friends/{friendid:int min(1) else 504}", func(ctx context.Context) {
|
||||
app.Get("/profile/{id:int min(1)}/friends/{friendid:int min(1) else 504}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
friendid, _ := ctx.Params().GetInt("friendid")
|
||||
ctx.Writef("Hello id: %d looking for friend id: ", id, friendid)
|
||||
@@ -132,11 +131,11 @@ func main() {
|
||||
|
||||
// http://localhost:8080/game/a-zA-Z/level/0-9
|
||||
// remember, alphabetical is lowercase or uppercase letters only.
|
||||
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx context.Context) {
|
||||
app.Get("/game/{name:alphabetical}/level/{level:int}", func(ctx iris.Context) {
|
||||
ctx.Writef("name: %s | level: %s", ctx.Params().Get("name"), ctx.Params().Get("level"))
|
||||
})
|
||||
|
||||
app.Get("/lowercase/static", func(ctx context.Context) {
|
||||
app.Get("/lowercase/static", func(ctx iris.Context) {
|
||||
ctx.Writef("static and dynamic paths are not conflicted anymore!")
|
||||
})
|
||||
|
||||
@@ -144,18 +143,18 @@ func main() {
|
||||
// which its value is only lowercase letters.
|
||||
|
||||
// http://localhost:8080/lowercase/anylowercase
|
||||
app.Get("/lowercase/{name:string regexp(^[a-z]+)}", func(ctx context.Context) {
|
||||
app.Get("/lowercase/{name:string regexp(^[a-z]+)}", func(ctx iris.Context) {
|
||||
ctx.Writef("name should be only lowercase, otherwise this handler will never executed: %s", ctx.Params().Get("name"))
|
||||
})
|
||||
|
||||
// http://localhost:8080/single_file/app.js
|
||||
app.Get("/single_file/{myfile:file}", func(ctx context.Context) {
|
||||
app.Get("/single_file/{myfile:file}", func(ctx iris.Context) {
|
||||
ctx.Writef("file type validates if the parameter value has a form of a file name, got: %s", ctx.Params().Get("myfile"))
|
||||
})
|
||||
|
||||
// http://localhost:8080/myfiles/any/directory/here/
|
||||
// this is the only macro type that accepts any number of path segments.
|
||||
app.Get("/myfiles/{directory:path}", func(ctx context.Context) {
|
||||
app.Get("/myfiles/{directory:path}", func(ctx iris.Context) {
|
||||
ctx.Writef("path type accepts any number of path segments, path after /myfiles/ is: %s", ctx.Params().Get("directory"))
|
||||
}) // for wildcard path (any number of path segments) without validation you can use:
|
||||
// /myfiles/*
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -42,30 +41,30 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
}
|
||||
|
||||
func h(ctx context.Context) {
|
||||
func h(ctx iris.Context) {
|
||||
param := ctx.Params().Get("p")
|
||||
ctx.WriteString(param)
|
||||
}
|
||||
|
||||
func staticWildcardH(ctx context.Context) {
|
||||
func staticWildcardH(ctx iris.Context) {
|
||||
param := ctx.Params().Get("p")
|
||||
ctx.WriteString("from staticWildcardH: param=" + param)
|
||||
}
|
||||
|
||||
func other(ctx context.Context) {
|
||||
func other(ctx iris.Context) {
|
||||
param := ctx.Params().Get("paramother")
|
||||
ctx.Writef("from other: %s", param)
|
||||
}
|
||||
|
||||
func other2(ctx context.Context) {
|
||||
func other2(ctx iris.Context) {
|
||||
param := ctx.Params().Get("paramothersecond")
|
||||
ctx.Writef("from other2: %s", param)
|
||||
}
|
||||
|
||||
func staticPath(ctx context.Context) {
|
||||
func staticPath(ctx iris.Context) {
|
||||
ctx.Writef("from the static path(/): %s", ctx.Path())
|
||||
}
|
||||
|
||||
func staticPathOther2(ctx context.Context) {
|
||||
func staticPathOther2(ctx iris.Context) {
|
||||
ctx.Writef("from the static path(/other2/static2): %s", ctx.Path())
|
||||
}
|
||||
|
||||
@@ -2,26 +2,25 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.OnErrorCode(iris.StatusInternalServerError, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusInternalServerError, func(ctx iris.Context) {
|
||||
ctx.HTML("Message: <b>" + ctx.Values().GetString("message") + "</b>")
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML(`Click <a href="/my500">here</a> to fire the 500 status code`)
|
||||
})
|
||||
|
||||
app.Get("/my500", func(ctx context.Context) {
|
||||
app.Get("/my500", func(ctx iris.Context) {
|
||||
ctx.Values().Set("message", "this is the error message")
|
||||
ctx.StatusCode(500)
|
||||
})
|
||||
|
||||
app.Get("/u/{firstname:alphabetical}", func(ctx context.Context) {
|
||||
app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("firstname"))
|
||||
})
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -20,13 +19,13 @@ const notFoundHTML = "<h1> custom http error page </h1>"
|
||||
|
||||
func registerErrors(app *iris.Application) {
|
||||
// set a custom 404 handler
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
ctx.HTML(notFoundHTML)
|
||||
})
|
||||
}
|
||||
|
||||
func registerGamesRoutes(app *iris.Application) {
|
||||
gamesMiddleware := func(ctx context.Context) {
|
||||
gamesMiddleware := func(ctx iris.Context) {
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
@@ -107,7 +106,7 @@ func newApp() *iris.Application {
|
||||
// and sends back the same body
|
||||
// remember, we have limit to that body in order
|
||||
// to protect ourselves from "over heating".
|
||||
app.Post("/", context.LimitRequestBodySize(maxBodySize), func(ctx context.Context) {
|
||||
app.Post("/", iris.LimitRequestBodySize(maxBodySize), func(ctx iris.Context) {
|
||||
// get request body
|
||||
b, err := ioutil.ReadAll(ctx.Request().Body)
|
||||
// if is larger then send a bad request status
|
||||
@@ -123,7 +122,7 @@ func newApp() *iris.Application {
|
||||
return app
|
||||
}
|
||||
|
||||
func h(ctx context.Context) {
|
||||
func h(ctx iris.Context) {
|
||||
method := ctx.Method() // the http method requested a server's resource.
|
||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -44,7 +43,7 @@ func main() {
|
||||
|
||||
usersRoutes := app.Party("/users")
|
||||
// GET: http://localhost:8080/users/help
|
||||
usersRoutes.Get("/help", func(ctx context.Context) {
|
||||
usersRoutes.Get("/help", func(ctx iris.Context) {
|
||||
ctx.Writef("GET / -- fetch all users\n")
|
||||
ctx.Writef("GET /$ID -- fetch a user by id\n")
|
||||
ctx.Writef("POST / -- create new user\n")
|
||||
@@ -53,32 +52,32 @@ func main() {
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/users
|
||||
usersRoutes.Get("/", func(ctx context.Context) {
|
||||
usersRoutes.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("get all users")
|
||||
})
|
||||
|
||||
// GET: http://localhost:8080/users/42
|
||||
// **/users/42 and /users/help works after iris version 7.0.5**
|
||||
usersRoutes.Get("/{id:int}", func(ctx context.Context) {
|
||||
usersRoutes.Get("/{id:int}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
ctx.Writef("get user by id: %d", id)
|
||||
})
|
||||
|
||||
// POST: http://localhost:8080/users
|
||||
usersRoutes.Post("/", func(ctx context.Context) {
|
||||
usersRoutes.Post("/", func(ctx iris.Context) {
|
||||
username, password := ctx.PostValue("username"), ctx.PostValue("password")
|
||||
ctx.Writef("create user for username= %s and password= %s", username, password)
|
||||
})
|
||||
|
||||
// PUT: http://localhost:8080/users
|
||||
usersRoutes.Put("/{id:int}", func(ctx context.Context) {
|
||||
usersRoutes.Put("/{id:int}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id") // or .Get to get its string represatantion.
|
||||
username := ctx.PostValue("username")
|
||||
ctx.Writef("update user for id= %d and new username= %s", id, username)
|
||||
})
|
||||
|
||||
// DELETE: http://localhost:8080/users/42
|
||||
usersRoutes.Delete("/{id:int}", func(ctx context.Context) {
|
||||
usersRoutes.Delete("/{id:int}", func(ctx iris.Context) {
|
||||
id, _ := ctx.Params().GetInt("id")
|
||||
ctx.Writef("delete user by id: %d", id)
|
||||
})
|
||||
@@ -118,7 +117,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func info(ctx context.Context) {
|
||||
func info(ctx iris.Context) {
|
||||
method := ctx.Method() // the http method requested a server's resource.
|
||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
@@ -12,7 +11,7 @@ func main() {
|
||||
// you normally don't need it because of the {{ urlpath "routename" "path" "values" "here"}}
|
||||
rv := router.NewRoutePathReverser(app)
|
||||
|
||||
myroute := app.Get("/anything/{anythingparameter:path}", func(ctx context.Context) {
|
||||
myroute := app.Get("/anything/{anythingparameter:path}", func(ctx iris.Context) {
|
||||
paramValue := ctx.Params().Get("anythingparameter")
|
||||
ctx.Writef("The path after /anything is: %s", paramValue)
|
||||
})
|
||||
@@ -20,13 +19,13 @@ func main() {
|
||||
myroute.Name = "myroute"
|
||||
|
||||
// useful for links, although iris' view engine has the {{ urlpath "routename" "path values"}} already.
|
||||
app.Get("/reverse_myroute", func(ctx context.Context) {
|
||||
app.Get("/reverse_myroute", func(ctx iris.Context) {
|
||||
myrouteRequestPath := rv.Path(myroute.Name, "any/path")
|
||||
ctx.HTML("Should be <b>/anything/any/path</b>: " + myrouteRequestPath)
|
||||
})
|
||||
|
||||
// execute a route, similar to redirect but without redirect :)
|
||||
app.Get("/execute_myroute", func(ctx context.Context) {
|
||||
app.Get("/execute_myroute", func(ctx iris.Context) {
|
||||
ctx.Exec("GET", "/anything/any/path") // like it was called by the client.
|
||||
})
|
||||
|
||||
|
||||
@@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
none := app.None("/invisible/{username}", func(ctx context.Context) {
|
||||
none := app.None("/invisible/{username}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s with method: %s", ctx.Values().GetString("username"), ctx.Method())
|
||||
|
||||
if from := ctx.Values().GetString("from"); from != "" {
|
||||
@@ -16,7 +15,7 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/change", func(ctx context.Context) {
|
||||
app.Get("/change", func(ctx iris.Context) {
|
||||
|
||||
if none.IsOnline() {
|
||||
none.Method = iris.MethodNone
|
||||
@@ -28,7 +27,7 @@ func main() {
|
||||
app.RefreshRouter()
|
||||
})
|
||||
|
||||
app.Get("/execute", func(ctx context.Context) {
|
||||
app.Get("/execute", func(ctx iris.Context) {
|
||||
// same as navigating to "http://localhost:8080/invisible/iris" when /change has being invoked and route state changed
|
||||
// from "offline" to "online"
|
||||
ctx.Values().Set("from", "/execute") // values and session can be shared when calling Exec from a "foreign" context.
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/boltdb"
|
||||
@@ -33,10 +32,10 @@ func main() {
|
||||
// the rest of the code stays the same.
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
})
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
//set session values
|
||||
s.Set("name", "iris")
|
||||
@@ -45,7 +44,7 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx context.Context) {
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
s := sess.Start(ctx)
|
||||
// set session values
|
||||
@@ -55,36 +54,36 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/get/{key}", func(ctx context.Context) {
|
||||
app.Get("/get/{key}", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx context.Context) {
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
sess.Start(ctx).Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries
|
||||
sess.Start(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
//destroy, removes the entire session data and cookie
|
||||
sess.Destroy(ctx)
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx context.Context) {
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
// updates expire date with a new date
|
||||
sess.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/file"
|
||||
@@ -29,10 +28,10 @@ func main() {
|
||||
// the rest of the code stays the same.
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
})
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
//set session values
|
||||
s.Set("name", "iris")
|
||||
@@ -41,7 +40,7 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx context.Context) {
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
s := sess.Start(ctx)
|
||||
// set session values
|
||||
@@ -51,36 +50,36 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/get/{key}", func(ctx context.Context) {
|
||||
app.Get("/get/{key}", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx context.Context) {
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
sess.Start(ctx).Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries
|
||||
sess.Start(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
//destroy, removes the entire session data and cookie
|
||||
sess.Destroy(ctx)
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx context.Context) {
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
// updates expire date with a new date
|
||||
sess.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/leveldb"
|
||||
@@ -34,10 +33,10 @@ func main() {
|
||||
// the rest of the code stays the same.
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
})
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
//set session values
|
||||
s.Set("name", "iris")
|
||||
@@ -46,7 +45,7 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx context.Context) {
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
s := sess.Start(ctx)
|
||||
// set session values
|
||||
@@ -56,36 +55,36 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/get/{key}", func(ctx context.Context) {
|
||||
app.Get("/get/{key}", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx context.Context) {
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
sess.Start(ctx).Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries
|
||||
sess.Start(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
//destroy, removes the entire session data and cookie
|
||||
sess.Destroy(ctx)
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx context.Context) {
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
// updates expire date with a new date
|
||||
sess.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
"github.com/kataras/iris/sessions/sessiondb/redis"
|
||||
@@ -40,10 +39,10 @@ func main() {
|
||||
// the rest of the code stays the same.
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
})
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
//set session values
|
||||
s.Set("name", "iris")
|
||||
@@ -52,29 +51,29 @@ func main() {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx context.Context) {
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
sess.Start(ctx).Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries
|
||||
sess.Start(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
//destroy, removes the entire session data and cookie
|
||||
sess.Destroy(ctx)
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx context.Context) {
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
// updates expire date with a new date
|
||||
sess.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
@@ -11,13 +10,13 @@ func main() {
|
||||
app := iris.New()
|
||||
sess := sessions.New(sessions.Config{Cookie: "myappsessionid"})
|
||||
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
s.SetFlash("name", "iris")
|
||||
ctx.Writef("Message setted, is available for the next request")
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
name := s.GetFlashString("name")
|
||||
if name == "" {
|
||||
@@ -27,7 +26,7 @@ func main() {
|
||||
ctx.Writef("Hello %s", name)
|
||||
})
|
||||
|
||||
app.Get("/test", func(ctx context.Context) {
|
||||
app.Get("/test", func(ctx iris.Context) {
|
||||
s := sess.Start(ctx)
|
||||
name := s.GetFlashString("name")
|
||||
if name == "" {
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
@@ -12,7 +11,7 @@ var (
|
||||
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
|
||||
)
|
||||
|
||||
func secret(ctx context.Context) {
|
||||
func secret(ctx iris.Context) {
|
||||
|
||||
// Check if user is authenticated
|
||||
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
||||
@@ -24,7 +23,7 @@ func secret(ctx context.Context) {
|
||||
ctx.WriteString("The cake is a lie!")
|
||||
}
|
||||
|
||||
func login(ctx context.Context) {
|
||||
func login(ctx iris.Context) {
|
||||
session := sess.Start(ctx)
|
||||
|
||||
// Authentication goes here
|
||||
@@ -34,7 +33,7 @@ func login(ctx context.Context) {
|
||||
session.Set("authenticated", true)
|
||||
}
|
||||
|
||||
func logout(ctx context.Context) {
|
||||
func logout(ctx iris.Context) {
|
||||
session := sess.Start(ctx)
|
||||
|
||||
// Revoke users authentication
|
||||
|
||||
@@ -7,7 +7,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
|
||||
@@ -30,10 +29,10 @@ func newApp() *iris.Application {
|
||||
Decode: secureCookie.Decode,
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
})
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
|
||||
//set session values
|
||||
s := mySessions.Start(ctx)
|
||||
@@ -43,7 +42,7 @@ func newApp() *iris.Application {
|
||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific key, as string, if no found returns just an empty string
|
||||
s := mySessions.Start(ctx)
|
||||
name := s.GetString("name")
|
||||
@@ -51,23 +50,23 @@ func newApp() *iris.Application {
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx context.Context) {
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
s := mySessions.Start(ctx)
|
||||
s.Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries
|
||||
mySessions.Start(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx context.Context) {
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
// updates expire date with a new date
|
||||
mySessions.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
//destroy, removes the entire session data and cookie
|
||||
mySessions.Destroy(ctx)
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
@@ -31,10 +30,10 @@ func main() {
|
||||
// want to be crazy safe? Take a look at the "securecookie" example folder.
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
})
|
||||
app.Get("/set", func(ctx context.Context) {
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
|
||||
//set session values.
|
||||
s := sess.Start(ctx)
|
||||
@@ -52,29 +51,29 @@ func main() {
|
||||
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific value, as string, if no found returns just an empty string
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx context.Context) {
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
sess.Start(ctx).Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries
|
||||
sess.Start(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx context.Context) {
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
// updates expire date
|
||||
sess.ShiftExpiration(ctx)
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
|
||||
//destroy, removes the entire session data and cookie
|
||||
sess.Destroy(ctx)
|
||||
@@ -91,7 +90,7 @@ func main() {
|
||||
//
|
||||
// Use `SetImmutable` consistently, it's slower than `Set`.
|
||||
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
||||
app.Get("/set_immutable", func(ctx context.Context) {
|
||||
app.Get("/set_immutable", func(ctx iris.Context) {
|
||||
business := []businessModel{{Name: "Edward"}, {Name: "value 2"}}
|
||||
s := sess.Start(ctx)
|
||||
s.SetImmutable("businessEdit", business)
|
||||
@@ -103,7 +102,7 @@ func main() {
|
||||
|
||||
})
|
||||
|
||||
app.Get("/get_immutable", func(ctx context.Context) {
|
||||
app.Get("/get_immutable", func(ctx iris.Context) {
|
||||
valSlice := sess.Start(ctx).Get("businessEdit")
|
||||
if valSlice == nil {
|
||||
ctx.HTML("please navigate to the <a href='/set_immutable'>/set_immutable</a> first")
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -17,18 +16,18 @@ func main() {
|
||||
|
||||
dashboard := app.Party("dashboard.")
|
||||
{
|
||||
dashboard.Get("/", func(ctx context.Context) {
|
||||
dashboard.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("HEY FROM dashboard")
|
||||
})
|
||||
}
|
||||
system := app.Party("system.")
|
||||
{
|
||||
system.Get("/", func(ctx context.Context) {
|
||||
system.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("HEY FROM system")
|
||||
})
|
||||
}
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("HEY FROM frontend /")
|
||||
})
|
||||
// http://domain.local:80
|
||||
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -15,26 +14,26 @@ func main() {
|
||||
admin := app.Party("admin.")
|
||||
{
|
||||
// admin.mydomain.com
|
||||
admin.Get("/", func(c context.Context) {
|
||||
admin.Get("/", func(c iris.Context) {
|
||||
c.Writef("INDEX FROM admin.mydomain.com")
|
||||
})
|
||||
// admin.mydomain.com/hey
|
||||
admin.Get("/hey", func(c context.Context) {
|
||||
admin.Get("/hey", func(c iris.Context) {
|
||||
c.Writef("HEY FROM admin.mydomain.com/hey")
|
||||
})
|
||||
// admin.mydomain.com/hey2
|
||||
admin.Get("/hey2", func(c context.Context) {
|
||||
admin.Get("/hey2", func(c iris.Context) {
|
||||
c.Writef("HEY SECOND FROM admin.mydomain.com/hey")
|
||||
})
|
||||
}
|
||||
|
||||
// mydomain.com/
|
||||
app.Get("/", func(c context.Context) {
|
||||
app.Get("/", func(c iris.Context) {
|
||||
c.Writef("INDEX FROM no-subdomain hey")
|
||||
})
|
||||
|
||||
// mydomain.com/hey
|
||||
app.Get("/hey", func(c context.Context) {
|
||||
app.Get("/hey", func(c iris.Context) {
|
||||
c.Writef("HEY FROM no-subdomain hey")
|
||||
})
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// register a dynamic-wildcard subdomain to your server machine(dns/...) first, check ./hosts if you use windows.
|
||||
@@ -20,15 +19,15 @@ func main() {
|
||||
admin := app.Party("admin.")
|
||||
{
|
||||
// admin.mydomain.com
|
||||
admin.Get("/", func(ctx context.Context) {
|
||||
admin.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("INDEX FROM admin.mydomain.com")
|
||||
})
|
||||
// admin.mydomain.com/hey
|
||||
admin.Get("/hey", func(ctx context.Context) {
|
||||
admin.Get("/hey", func(ctx iris.Context) {
|
||||
ctx.Writef("HEY FROM admin.mydomain.com/hey")
|
||||
})
|
||||
// admin.mydomain.com/hey2
|
||||
admin.Get("/hey2", func(ctx context.Context) {
|
||||
admin.Get("/hey2", func(ctx iris.Context) {
|
||||
ctx.Writef("HEY SECOND FROM admin.mydomain.com/hey")
|
||||
})
|
||||
}*/
|
||||
@@ -43,11 +42,11 @@ func main() {
|
||||
dynamicSubdomains.Get("/something/{paramfirst}", dynamicSubdomainHandlerWithParam)
|
||||
}
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
|
||||
})
|
||||
|
||||
app.Get("/hello", func(ctx context.Context) {
|
||||
app.Get("/hello", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from mydomain.com path: %s", ctx.Path())
|
||||
})
|
||||
|
||||
@@ -58,14 +57,14 @@ func main() {
|
||||
app.Run(iris.Addr("mydomain.com:8080")) // for beginners: look ../hosts file
|
||||
}
|
||||
|
||||
func dynamicSubdomainHandler(ctx context.Context) {
|
||||
func dynamicSubdomainHandler(ctx iris.Context) {
|
||||
username := ctx.Subdomain()
|
||||
ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username)
|
||||
// if http://username4.mydomain.com:8080/ prints:
|
||||
// Hello from dynamic subdomain path: /, here you can handle the route for dynamic subdomains, handle the user: username4
|
||||
}
|
||||
|
||||
func dynamicSubdomainHandlerWithParam(ctx context.Context) {
|
||||
func dynamicSubdomainHandlerWithParam(ctx iris.Context) {
|
||||
username := ctx.Subdomain()
|
||||
ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username)
|
||||
ctx.Writef("The paramfirst is: %s", ctx.Params().Get("paramfirst"))
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func newApp() *iris.Application {
|
||||
@@ -53,7 +52,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func info(ctx context.Context) {
|
||||
func info(ctx iris.Context) {
|
||||
method := ctx.Method()
|
||||
subdomain := ctx.Subdomain()
|
||||
path := ctx.Path()
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/basicauth"
|
||||
)
|
||||
|
||||
@@ -15,7 +14,7 @@ func newApp() *iris.Application {
|
||||
|
||||
authentication := basicauth.New(authConfig)
|
||||
|
||||
app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })
|
||||
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
|
||||
|
||||
// to party
|
||||
|
||||
@@ -33,7 +32,7 @@ func newApp() *iris.Application {
|
||||
return app
|
||||
}
|
||||
|
||||
func h(ctx context.Context) {
|
||||
func h(ctx iris.Context) {
|
||||
username, password, _ := ctx.Request().BasicAuth()
|
||||
// third parameter it will be always true because the middleware
|
||||
// makes sure for that, otherwise this handler will not be executed.
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
# Controllers from scratch
|
||||
|
||||
This folder shows how [@kataras](https://github.com/kataras) started to develop
|
||||
the MVC idea inside the Iris web framework itself.
|
||||
|
||||
**Now** it has been enhanced and it's a **built'n** feature and can be used as:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
// when we have a path separated by spaces
|
||||
// then the Controller is registered to all of them one by one.
|
||||
//
|
||||
// myDB is binded to the controller's `*DB` field: use only structs and pointers.
|
||||
app.Controller("/profile /profile/browse /profile/{id:int} /profile/me",
|
||||
new(ProfileController), myDB) // IMPORTANT
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
// UserModel our example model which will render on the template.
|
||||
type UserModel struct {
|
||||
ID int64
|
||||
Username string
|
||||
}
|
||||
|
||||
// DB is our example database.
|
||||
type DB struct {
|
||||
usersTable map[int64]UserModel
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// GetUserByID imaginary database lookup based on user id.
|
||||
func (db *DB) GetUserByID(id int64) (u UserModel, found bool) {
|
||||
db.mu.RLock()
|
||||
u, found = db.usersTable[id]
|
||||
db.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
|
||||
var myDB = &DB{
|
||||
usersTable: map[int64]UserModel{
|
||||
1: {1, "kataras"},
|
||||
2: {2, "makis"},
|
||||
42: {42, "jdoe"},
|
||||
},
|
||||
}
|
||||
|
||||
// ProfileController our example user controller which controls
|
||||
// the paths of "/profile" "/profile/{id:int}" and "/profile/me".
|
||||
type ProfileController struct {
|
||||
mvc.Controller // IMPORTANT
|
||||
|
||||
User UserModel `iris:"model"`
|
||||
// we will bind it but you can also tag it with`iris:"persistence"`
|
||||
// and init the controller with manual &PorifleController{DB: myDB}.
|
||||
DB *DB
|
||||
}
|
||||
|
||||
// Get method handles all "GET" HTTP Method requests of the controller's paths.
|
||||
func (pc *ProfileController) Get() { // IMPORTANT
|
||||
path := pc.Path
|
||||
|
||||
// requested: /profile path
|
||||
if path == "/profile" {
|
||||
pc.Tmpl = "profile/index.html"
|
||||
return
|
||||
}
|
||||
// requested: /profile/browse
|
||||
// this exists only to proof the concept of changing the path:
|
||||
// it will result to a redirection.
|
||||
if path == "/profile/browse" {
|
||||
pc.Path = "/profile"
|
||||
return
|
||||
}
|
||||
|
||||
// requested: /profile/me path
|
||||
if path == "/profile/me" {
|
||||
pc.Tmpl = "profile/me.html"
|
||||
return
|
||||
}
|
||||
|
||||
// requested: /profile/$ID
|
||||
id, _ := pc.Params.GetInt64("id")
|
||||
|
||||
user, found := pc.DB.GetUserByID(id)
|
||||
if !found {
|
||||
pc.Status = iris.StatusNotFound
|
||||
pc.Tmpl = "profile/notfound.html"
|
||||
pc.Data["ID"] = id
|
||||
return
|
||||
}
|
||||
|
||||
pc.Tmpl = "profile/profile.html"
|
||||
pc.User = user
|
||||
}
|
||||
|
||||
/* Can use more than one, the factory will make sure
|
||||
that the correct http methods are being registered for each route
|
||||
for this controller, uncomment these if you want:
|
||||
|
||||
func (pc *ProfileController) Post() {}
|
||||
func (pc *ProfileController) Put() {}
|
||||
func (pc *ProfileController) Delete() {}
|
||||
func (pc *ProfileController) Connect() {}
|
||||
func (pc *ProfileController) Head() {}
|
||||
func (pc *ProfileController) Patch() {}
|
||||
func (pc *ProfileController) Options() {}
|
||||
func (pc *ProfileController) Trace() {}
|
||||
*/
|
||||
|
||||
/*
|
||||
func (c *ProfileController) All() {}
|
||||
// OR
|
||||
func (c *ProfileController) Any() {}
|
||||
*/
|
||||
```
|
||||
|
||||
Example can be found at: [_examples/mvc](https://github.com/kataras/iris/tree/master/_examples/mvc).
|
||||
@@ -1,155 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
// path params.
|
||||
Params *context.RequestParams
|
||||
|
||||
// view properties.
|
||||
Layout string
|
||||
Tmpl string
|
||||
Data map[string]interface{}
|
||||
|
||||
// give access to the request context itself.
|
||||
Ctx context.Context
|
||||
}
|
||||
|
||||
// all lowercase, so user can see only the fields
|
||||
// that are necessary to him/her.
|
||||
func (b *Controller) init(ctx context.Context) {
|
||||
b.Ctx = ctx
|
||||
b.Params = ctx.Params()
|
||||
b.Data = make(map[string]interface{}, 0)
|
||||
}
|
||||
|
||||
func (b *Controller) exec() {
|
||||
if v := b.Tmpl; v != "" {
|
||||
if l := b.Layout; l != "" {
|
||||
b.Ctx.ViewLayout(l)
|
||||
}
|
||||
if d := b.Data; d != nil {
|
||||
for key, value := range d {
|
||||
b.Ctx.ViewData(key, value)
|
||||
}
|
||||
}
|
||||
b.Ctx.View(v)
|
||||
}
|
||||
}
|
||||
|
||||
// get the field name at compile-time,
|
||||
// will help us to catch any unexpected results on future versions.
|
||||
var baseControllerName = reflect.TypeOf(Controller{}).Name()
|
||||
|
||||
func RegisterController(app *iris.Application, path string, c interface{}) {
|
||||
typ := reflect.TypeOf(c)
|
||||
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
typ = reflect.PtrTo(typ)
|
||||
}
|
||||
|
||||
elem := typ.Elem()
|
||||
|
||||
// check if "c" has the "Controller" typeof `Controller` field.
|
||||
b, has := elem.FieldByName(baseControllerName)
|
||||
if !has {
|
||||
panic("controller should have a field of Controller type")
|
||||
}
|
||||
|
||||
baseControllerFieldIndex := b.Index[0]
|
||||
persistenceFields := make(map[int]reflect.Value, 0)
|
||||
|
||||
if numField := elem.NumField(); numField > 1 {
|
||||
val := reflect.Indirect(reflect.ValueOf(c))
|
||||
|
||||
for i := 0; i < numField; i++ {
|
||||
f := elem.Field(i)
|
||||
valF := val.Field(i)
|
||||
// catch persistence data by tags, i.e:
|
||||
// MyData string `iris:"persistence"`
|
||||
// DB *DB `iris:"persistence"`
|
||||
if t, ok := f.Tag.Lookup("iris"); ok {
|
||||
if t == "persistence" {
|
||||
persistenceFields[i] = reflect.ValueOf(valF.Interface())
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// check if has Any() or All()
|
||||
// if yes, then register all http methods and
|
||||
// exit.
|
||||
m, has := typ.MethodByName("Any")
|
||||
if !has {
|
||||
m, has = typ.MethodByName("All")
|
||||
}
|
||||
if has {
|
||||
app.Any(path,
|
||||
controllerToHandler(elem, persistenceFields,
|
||||
baseControllerFieldIndex, m.Index))
|
||||
return
|
||||
}
|
||||
|
||||
// else search the entire controller
|
||||
// for any compatible method function
|
||||
// and register that.
|
||||
for _, method := range router.AllMethods {
|
||||
httpMethodFuncName := strings.Title(strings.ToLower(method))
|
||||
|
||||
m, has := typ.MethodByName(httpMethodFuncName)
|
||||
if !has {
|
||||
continue
|
||||
}
|
||||
|
||||
httpMethodIndex := m.Index
|
||||
|
||||
app.Handle(method, path,
|
||||
controllerToHandler(elem, persistenceFields,
|
||||
baseControllerFieldIndex, httpMethodIndex))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func controllerToHandler(elem reflect.Type, persistenceFields map[int]reflect.Value,
|
||||
baseControllerFieldIndex, httpMethodIndex int) context.Handler {
|
||||
return func(ctx context.Context) {
|
||||
// create a new controller instance of that type(>ptr).
|
||||
c := reflect.New(elem)
|
||||
|
||||
// get the responsible method.
|
||||
// Remember:
|
||||
// To improve the performance
|
||||
// we don't compare the ctx.Method()[HTTP Method]
|
||||
// to the instance's Method, each handler is registered
|
||||
// to a specific http method.
|
||||
methodFunc := c.Method(httpMethodIndex)
|
||||
|
||||
// get the Controller embedded field.
|
||||
b, _ := c.Elem().Field(baseControllerFieldIndex).Addr().Interface().(*Controller)
|
||||
|
||||
if len(persistenceFields) > 0 {
|
||||
elem := c.Elem()
|
||||
for index, value := range persistenceFields {
|
||||
elem.Field(index).Set(value)
|
||||
}
|
||||
}
|
||||
|
||||
// init the new controller instance.
|
||||
b.init(ctx)
|
||||
|
||||
// execute the responsible method for that handler.
|
||||
methodFunc.Interface().(func())()
|
||||
|
||||
// finally, execute the controller.
|
||||
b.exec()
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package controllers
|
||||
|
||||
// Index is our index example controller.
|
||||
type Index struct {
|
||||
Controller
|
||||
}
|
||||
|
||||
func (c *Index) Get() {
|
||||
c.Tmpl = "index.html"
|
||||
c.Data["title"] = "Index page"
|
||||
c.Data["message"] = "Hello world!"
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/_examples/tutorial/mvc-from-scratch/persistence"
|
||||
)
|
||||
|
||||
// User is our user example controller.
|
||||
type User struct {
|
||||
Controller
|
||||
|
||||
// All fields that are tagged with iris:"persistence"`
|
||||
// are being persistence and kept between the different requests,
|
||||
// meaning that these data will not be reset-ed on each new request,
|
||||
// they will be the same for all requests.
|
||||
CreatedAt time.Time `iris:"persistence"`
|
||||
Title string `iris:"persistence"`
|
||||
DB *persistence.Database `iris:"persistence"`
|
||||
}
|
||||
|
||||
func NewUserController(db *persistence.Database) *User {
|
||||
return &User{
|
||||
CreatedAt: time.Now(),
|
||||
Title: "User page",
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Get serves using the User controller when HTTP Method is "GET".
|
||||
func (c *User) Get() {
|
||||
c.Tmpl = "user/index.html"
|
||||
c.Data["title"] = c.Title
|
||||
c.Data["username"] = "kataras " + c.Params.Get("userid")
|
||||
c.Data["connstring"] = c.DB.Connstring
|
||||
c.Data["uptime"] = time.Now().Sub(c.CreatedAt).Seconds()
|
||||
}
|
||||
|
||||
/* Can use more than one, the factory will make sure
|
||||
that the correct http methods are being registered for this
|
||||
controller, uncommend these if you want:
|
||||
|
||||
func (c *User) Post() {}
|
||||
func (c *User) Put() {}
|
||||
func (c *User) Delete() {}
|
||||
func (c *User) Connect() {}
|
||||
func (c *User) Head() {}
|
||||
func (c *User) Patch() {}
|
||||
func (c *User) Options() {}
|
||||
func (c *User) Trace() {}
|
||||
*/
|
||||
|
||||
/*
|
||||
func (c *User) All() {}
|
||||
// OR
|
||||
func (c *User) Any() {}
|
||||
*/
|
||||
@@ -1,24 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/_examples/tutorial/mvc-from-scratch/controllers"
|
||||
"github.com/kataras/iris/_examples/tutorial/mvc-from-scratch/persistence"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
db := persistence.OpenDatabase("a fake db")
|
||||
|
||||
controllers.RegisterController(app, "/", new(controllers.Index))
|
||||
|
||||
controllers.RegisterController(app, "/user/{userid:int}",
|
||||
controllers.NewUserController(db))
|
||||
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/user/42
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// User is an example model.
|
||||
type User struct {
|
||||
ID int64
|
||||
Username string
|
||||
Firstname string
|
||||
Lastname string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package persistence
|
||||
|
||||
// Database is our imaginary storage.
|
||||
type Database struct {
|
||||
Connstring string
|
||||
}
|
||||
|
||||
func OpenDatabase(connstring string) *Database {
|
||||
return &Database{Connstring: connstring}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{.title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>{{.message}}</h1>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,17 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{.title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1> Hello {{.username}} </h1>
|
||||
|
||||
|
||||
All fields inside a controller that are pointers or they tagged as `iris:"persistence"` are marked as persistence data, meaning
|
||||
that they will not be reset-ed on each new request.
|
||||
<h3>Persistence data from *DB.Connstring: {{.connstring}} </h3>
|
||||
<h3>Persistence data from CreatedAt `iris:"persistence"`: {{.uptime}} seconds </h3>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
@@ -26,12 +25,12 @@ func main() {
|
||||
// register static assets request path and system directory
|
||||
app.StaticWeb("/js", "./static/assets/js")
|
||||
|
||||
h := func(ctx context.Context) {
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.ViewData("", page{PageID: "index page"})
|
||||
ctx.View("index.html")
|
||||
}
|
||||
|
||||
h2 := func(ctx context.Context) {
|
||||
h2 := func(ctx iris.Context) {
|
||||
ctx.ViewData("", page{PageID: "other page"})
|
||||
ctx.View("other.html")
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"html/template"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -52,7 +51,7 @@ func newApp(db *DB) *iris.Application {
|
||||
// Serve static files (css)
|
||||
app.StaticWeb("/static", "./resources")
|
||||
|
||||
indexHandler := func(ctx context.Context) {
|
||||
indexHandler := func(ctx iris.Context) {
|
||||
ctx.ViewData("URL_COUNT", db.Len())
|
||||
ctx.View("index.html")
|
||||
}
|
||||
@@ -60,7 +59,7 @@ func newApp(db *DB) *iris.Application {
|
||||
|
||||
// find and execute a short url by its key
|
||||
// used on http://localhost:8080/u/dsaoj41u321dsa
|
||||
execShortURL := func(ctx context.Context, key string) {
|
||||
execShortURL := func(ctx iris.Context, key string) {
|
||||
if key == "" {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
return
|
||||
@@ -75,11 +74,11 @@ func newApp(db *DB) *iris.Application {
|
||||
|
||||
ctx.Redirect(value, iris.StatusTemporaryRedirect)
|
||||
}
|
||||
app.Get("/u/{shortkey}", func(ctx context.Context) {
|
||||
app.Get("/u/{shortkey}", func(ctx iris.Context) {
|
||||
execShortURL(ctx, ctx.Params().Get("shortkey"))
|
||||
})
|
||||
|
||||
app.Post("/shorten", func(ctx context.Context) {
|
||||
app.Post("/shorten", func(ctx iris.Context) {
|
||||
formValue := ctx.FormValue("url")
|
||||
if formValue == "" {
|
||||
ctx.ViewData("FORM_RESULT", "You need to a enter a URL")
|
||||
@@ -107,7 +106,7 @@ func newApp(db *DB) *iris.Application {
|
||||
indexHandler(ctx) // no redirect, we need the FORM_RESULT.
|
||||
})
|
||||
|
||||
app.Post("/clear_cache", func(ctx context.Context) {
|
||||
app.Post("/clear_cache", func(ctx iris.Context) {
|
||||
db.Clear()
|
||||
ctx.Redirect("/")
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -19,7 +18,7 @@ func main() {
|
||||
// set the view engine target to ./templates folder
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
app.Use(func(ctx context.Context) {
|
||||
app.Use(func(ctx iris.Context) {
|
||||
// set the title, current time and a layout in order to be used if and when the next handler(s) calls the .Render function
|
||||
ctx.ViewData("Title", DefaultTitle)
|
||||
now := time.Now().Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
|
||||
@@ -29,14 +28,14 @@ func main() {
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ViewData("BodyMessage", "a sample text here... setted by the route handler")
|
||||
if err := ctx.View("index.html"); err != nil {
|
||||
ctx.Application().Logger().Infof(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/about", func(ctx context.Context) {
|
||||
app.Get("/about", func(ctx iris.Context) {
|
||||
ctx.ViewData("Title", "My About Page")
|
||||
ctx.ViewData("BodyMessage", "about text here... setted by the route handler")
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -23,7 +22,7 @@ type page struct {
|
||||
Title, Name string
|
||||
}
|
||||
|
||||
func hi(ctx context.Context) {
|
||||
func hi(ctx iris.Context) {
|
||||
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
||||
ctx.View("hi.html")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -21,7 +20,7 @@ func main() {
|
||||
// - {{ yield }}
|
||||
// - {{ current }}
|
||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
|
||||
ctx.ViewData("Name", "iris") // the .Name inside the ./templates/hi.html
|
||||
ctx.Gzip(true) // enable gzip for big files
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -34,7 +33,7 @@ func main() {
|
||||
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8")) // defaults to that but you can change it.
|
||||
}
|
||||
|
||||
func hi(ctx context.Context) {
|
||||
func hi(ctx iris.Context) {
|
||||
ctx.ViewData("Title", "Hi Page")
|
||||
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
|
||||
// ctx.ViewData("", myCcustomStruct{})
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
type mypage struct {
|
||||
@@ -16,7 +15,7 @@ func main() {
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Layout("layout.html"))
|
||||
// TIP: append .Reload(true) to reload the templates on each request.
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Gzip(true)
|
||||
ctx.ViewData("", mypage{"My Page title", "Hello world!"})
|
||||
ctx.View("mypage.html")
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -16,7 +15,7 @@ func main() {
|
||||
|
||||
app.RegisterView(tmpl)
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
if err := ctx.View("page1.html"); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.Writef(err.Error())
|
||||
@@ -24,7 +23,7 @@ func main() {
|
||||
})
|
||||
|
||||
// remove the layout for a specific route
|
||||
app.Get("/nolayout", func(ctx context.Context) {
|
||||
app.Get("/nolayout", func(ctx iris.Context) {
|
||||
ctx.ViewLayout(iris.NoLayout)
|
||||
if err := ctx.View("page1.html"); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
@@ -35,10 +34,10 @@ func main() {
|
||||
// set a layout for a party, .Layout should be BEFORE any Get or other Handle party's method
|
||||
my := app.Party("/my").Layout("layouts/mylayout.html")
|
||||
{ // both of these will use the layouts/mylayout.html as their layout.
|
||||
my.Get("/", func(ctx context.Context) {
|
||||
my.Get("/", func(ctx iris.Context) {
|
||||
ctx.View("page1.html")
|
||||
})
|
||||
my.Get("/other", func(ctx context.Context) {
|
||||
my.Get("/other", func(ctx iris.Context) {
|
||||
ctx.View("page1.html")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -30,7 +29,7 @@ func main() {
|
||||
mypath6Route := app.Get("/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}", writePathHandler)
|
||||
mypath6Route.Name = "my-page6"
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
// for /mypath6...
|
||||
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
|
||||
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
||||
@@ -39,7 +38,7 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/redirect/{namedRoute}", func(ctx context.Context) {
|
||||
app.Get("/redirect/{namedRoute}", func(ctx iris.Context) {
|
||||
routeName := ctx.Params().Get("namedRoute")
|
||||
r := app.GetRoute(routeName)
|
||||
if r == nil {
|
||||
@@ -63,6 +62,6 @@ func main() {
|
||||
|
||||
}
|
||||
|
||||
func writePathHandler(ctx context.Context) {
|
||||
func writePathHandler(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s.", ctx.Path())
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
)
|
||||
|
||||
@@ -48,7 +47,7 @@ func main() {
|
||||
mypath6Route := subdomain.Get("/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}", emptyHandler)
|
||||
mypath6Route.Name = "my-page6"
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
// for username5./mypath6...
|
||||
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
|
||||
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
||||
@@ -61,7 +60,7 @@ func main() {
|
||||
app.Run(iris.Addr(host))
|
||||
}
|
||||
|
||||
func emptyHandler(ctx context.Context) {
|
||||
func emptyHandler(ctx iris.Context) {
|
||||
ctx.Writef("Hello from subdomain: %s , you're in path: %s", ctx.Subdomain(), ctx.Path())
|
||||
}
|
||||
|
||||
|
||||
@@ -4,15 +4,13 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?
|
||||
})
|
||||
|
||||
@@ -39,7 +37,7 @@ func setupWebsocket(app *iris.Application) {
|
||||
|
||||
// serve the javascript built'n client-side library,
|
||||
// see weboskcets.html script tags, this path is used.
|
||||
app.Any("/iris-ws.js", func(ctx context.Context) {
|
||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||
ctx.Write(websocket.ClientSource)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
@@ -19,7 +17,7 @@ type clientPage struct {
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(view.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
app.RegisterView(iris.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
|
||||
ws := websocket.New(websocket.Config{})
|
||||
|
||||
@@ -29,13 +27,13 @@ func main() {
|
||||
|
||||
// serve the javascript built'n client-side library,
|
||||
// see weboskcets.html script tags, this path is used.
|
||||
app.Any("/iris-ws.js", func(ctx context.Context) {
|
||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||
ctx.Write(websocket.ClientSource)
|
||||
})
|
||||
|
||||
app.StaticWeb("/js", "./static/js") // serve our custom javascript code
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ViewData("", clientPage{"Client Page", "localhost:8080"})
|
||||
ctx.View("client.html")
|
||||
})
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
@@ -26,7 +24,7 @@ type clientPage struct {
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.RegisterView(view.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
app.RegisterView(iris.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
|
||||
ws := websocket.New(websocket.Config{
|
||||
// to enable binary messages (useful for protobuf):
|
||||
@@ -39,7 +37,7 @@ func main() {
|
||||
|
||||
app.StaticWeb("/js", "./static/js") // serve our custom javascript code
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.ViewData("", clientPage{"Client Page", "localhost:8080"})
|
||||
ctx.View("client.html")
|
||||
})
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
@@ -20,7 +18,7 @@ type clientPage struct {
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(view.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
app.RegisterView(iris.HTML("./templates", ".html")) // select the html engine to serve templates
|
||||
|
||||
ws := websocket.New(websocket.Config{})
|
||||
|
||||
@@ -30,12 +28,12 @@ func main() {
|
||||
|
||||
// serve the javascript built'n client-side library,
|
||||
// see weboskcets.html script tags, this path is used.
|
||||
app.Any("/iris-ws.js", func(ctx context.Context) {
|
||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||
ctx.Write(websocket.ClientSource)
|
||||
})
|
||||
|
||||
app.StaticWeb("/js", "./static/js")
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
// send our custom javascript source file before client really asks for that
|
||||
// using the go v1.8's HTTP/2 Push.
|
||||
// Note that you have to listen using ListenTLS in order this to work.
|
||||
@@ -49,7 +47,7 @@ func main() {
|
||||
var myChatRoom = "room1"
|
||||
|
||||
ws.OnConnection(func(c websocket.Connection) {
|
||||
// Context returns the (upgraded) context.Context of this connection
|
||||
// Context returns the (upgraded) iris.Context of this connection
|
||||
// avoid using it, you normally don't need it,
|
||||
// websocket has everything you need to authenticate the user BUT if it's necessary
|
||||
// then you use it to receive user information, for example: from headers.
|
||||
|
||||
Reference in New Issue
Block a user