mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01: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:
parent
a2de506f80
commit
42b123975c
|
@ -3,7 +3,9 @@ os:
|
||||||
- linux
|
- linux
|
||||||
- osx
|
- osx
|
||||||
go:
|
go:
|
||||||
- go1.8
|
# - go1.8 works of course but
|
||||||
|
# we must encourage users to update to the latest go version,
|
||||||
|
# so examples are running on go 1.9 mode.
|
||||||
- tip
|
- tip
|
||||||
go_import_path: github.com/kataras/iris
|
go_import_path: github.com/kataras/iris
|
||||||
install:
|
install:
|
||||||
|
|
15
HISTORY.md
15
HISTORY.md
|
@ -21,6 +21,17 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene
|
||||||
|
|
||||||
# Su, 27 August 2017 | v8.4.0
|
# Su, 27 August 2017 | v8.4.0
|
||||||
|
|
||||||
|
## Miscellaneous
|
||||||
|
|
||||||
|
- Update `vendor blackfriday` package to its latest version, 2.0.0
|
||||||
|
- Update [documentation](https://godoc.org/github.com/kataras/iris) for go 1.9
|
||||||
|
- Update [_examples](_examples) folder for go 1.9
|
||||||
|
- Update examples inside https://github.com/iris-contrib/middleware for go 1.9
|
||||||
|
- Update https://github.com/kataras/iris-contrib/examples for go 1.9
|
||||||
|
- Update https://iris-go.com/v8/recipe for go 1.9
|
||||||
|
|
||||||
|
## Router
|
||||||
|
|
||||||
Add a new macro type for path parameters, `long`, it's the go type `int64`.
|
Add a new macro type for path parameters, `long`, it's the go type `int64`.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -29,7 +40,7 @@ app.Get("/user/{id:long}", func(ctx context.Context) {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
And the promise we gave to you some days ago.
|
## MVC
|
||||||
|
|
||||||
The ability to pre-calculate, register and map different (relative) paths inside a single controller
|
The ability to pre-calculate, register and map different (relative) paths inside a single controller
|
||||||
with zero performance cost.
|
with zero performance cost.
|
||||||
|
@ -1413,7 +1424,7 @@ General
|
||||||
- Several enhancements for the typescript transpiler, view engine, websocket server and sessions manager
|
- Several enhancements for the typescript transpiler, view engine, websocket server and sessions manager
|
||||||
- All `Listen` methods replaced with a single `Run` method, see [here](https://github.com/kataras/iris/tree/master/_examples/beginner/listening)
|
- All `Listen` methods replaced with a single `Run` method, see [here](https://github.com/kataras/iris/tree/master/_examples/beginner/listening)
|
||||||
- Configuration, easier to modify the defaults, see [here](https://github.com/kataras/iris/tree/master/_examples/beginner/cofiguration)
|
- Configuration, easier to modify the defaults, see [here](https://github.com/kataras/iris/tree/master/_examples/beginner/cofiguration)
|
||||||
- `HandlerFunc` removed, just `Handler` of `func(context.Context)` where context.Context derives from `import "github.com/kataras/iris/context"` (on August this import path will be optional)
|
- `HandlerFunc` removed, just `Handler` of `func(context.Context)` where context.Context derives from `import "github.com/kataras/iris/context"` (**NEW**: this import path is optional, use `iris.Context` if you've installed Go 1.9)
|
||||||
- Simplify API, i.e: instead of `Handle,HandleFunc,Use,UseFunc,Done,DoneFunc,UseGlobal,UseGlobalFunc` use `Handle,Use,Done,UseGlobal`.
|
- Simplify API, i.e: instead of `Handle,HandleFunc,Use,UseFunc,Done,DoneFunc,UseGlobal,UseGlobalFunc` use `Handle,Use,Done,UseGlobal`.
|
||||||
- Response time decreased even more (9-35%, depends on the application)
|
- Response time decreased even more (9-35%, depends on the application)
|
||||||
- The `Adaptors` idea replaced with a more structural design pattern, but you have to apply these changes:
|
- The `Adaptors` idea replaced with a more structural design pattern, but you have to apply these changes:
|
||||||
|
|
33
README.md
33
README.md
|
@ -72,14 +72,11 @@ Iris may have reached version 8, but we're not stopping there. We have many feat
|
||||||
- [A URL Shortener Service using Go, Iris and Bolt](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7)
|
- [A URL Shortener Service using Go, Iris and Bolt](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7)
|
||||||
- [Why I preferred Go over Node.js for simple Web Application](https://medium.com/@tigranbs/why-i-preferred-go-over-node-js-for-simple-web-application-d4a549e979b9)
|
- [Why I preferred Go over Node.js for simple Web Application](https://medium.com/@tigranbs/why-i-preferred-go-over-node-js-for-simple-web-application-d4a549e979b9)
|
||||||
* [Versioning](#-version)
|
* [Versioning](#-version)
|
||||||
* [When should I upgrade?](#should-i-upgrade-my-iris)
|
|
||||||
* [Where can I find older versions?](#where-can-i-find-older-versions)
|
|
||||||
* [Get Hired](#-get-hired)
|
|
||||||
* [People](#-people)
|
* [People](#-people)
|
||||||
|
|
||||||
### 🚀 Installation
|
### 🚀 Installation
|
||||||
|
|
||||||
The only requirement is the [Go Programming Language](https://golang.org/dl/), at least version 1.8
|
The only requirement is the [Go Programming Language](https://golang.org/dl/), at least version 1.8 but **1.9** is highly recommended.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ go get -u github.com/kataras/iris
|
$ go get -u github.com/kataras/iris
|
||||||
|
@ -90,10 +87,8 @@ $ go get -u github.com/kataras/iris
|
||||||
```go
|
```go
|
||||||
// file: main.go
|
// file: main.go
|
||||||
package main
|
package main
|
||||||
import (
|
import "github.com/kataras/iris"
|
||||||
"github.com/kataras/iris"
|
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
// Load all templates from the "./templates" folder
|
// Load all templates from the "./templates" folder
|
||||||
|
@ -103,14 +98,14 @@ func main() {
|
||||||
|
|
||||||
// Method: GET
|
// Method: GET
|
||||||
// Resource: http://localhost:8080
|
// Resource: http://localhost:8080
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
// Bind: {{.message}} with "Hello world!"
|
// Bind: {{.message}} with "Hello world!"
|
||||||
ctx.ViewData("message", "Hello world!")
|
ctx.ViewData("message", "Hello world!")
|
||||||
// Render template file: ./templates/hello.html
|
// Render template file: ./templates/hello.html
|
||||||
ctx.View("hello.html")
|
ctx.View("hello.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start the server using a network address and block.
|
// Start the server using a network address.
|
||||||
app.Run(iris.Addr(":8080"))
|
app.Run(iris.Addr(":8080"))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -133,24 +128,27 @@ $ go run main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Hello World with Go 1.9</summary>
|
<summary>Hello World with Go 1.8</summary>
|
||||||
|
|
||||||
Go 1.9 just released.
|
Iris declares all of its type alias at the same file in order to be easy to be discovered.
|
||||||
|
|
||||||
Dcumentation and examples will be updated soon to use the already-type aliases inside the framework, such as `iris.Context` instead of the origin package.
|
> If you just upgraded to go 1.9 from 1.8 you can always search for a compatible type alias at the [context.go](context.go) file and opposite, if you use go 1.8 and you're new to Iris you can see [that](context.go) file to see the compatible packages.
|
||||||
|
|
||||||
If you've installed [Go 1.9](https://golang.org/dl) then you can omit the `github.com/kataras/iris/context` package from the imports statement.
|
If Go 1.8 remains the basic host for your go apps then you should declare and use the `github.com/kataras/iris/context` package on your source file's imports statement.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/kataras/iris"
|
import (
|
||||||
|
"github.com/kataras/iris"
|
||||||
|
"github.com/kataras/iris/context"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||||
|
|
||||||
app.Get("/", func(ctx iris.Context) {
|
app.Get("/", func(ctx context.Context) {
|
||||||
ctx.ViewData("message", "Hello world!")
|
ctx.ViewData("message", "Hello world!")
|
||||||
ctx.View("hello.html")
|
ctx.View("hello.html")
|
||||||
})
|
})
|
||||||
|
@ -352,6 +350,7 @@ Testers should upgrade immediately, if you're willing to use _iris_ in productio
|
||||||
|
|
||||||
Previous versions can be found at [releases page](https://github.com/kataras/iris/releases).
|
Previous versions can be found at [releases page](https://github.com/kataras/iris/releases).
|
||||||
|
|
||||||
|
<!--
|
||||||
### 😃 Get Hired
|
### 😃 Get Hired
|
||||||
|
|
||||||
Below you'll find a list of open positions that require at least **experience with the Iris web framework**.
|
Below you'll find a list of open positions that require at least **experience with the Iris web framework**.
|
||||||
|
@ -362,6 +361,8 @@ Below you'll find a list of open positions that require at least **experience wi
|
||||||
|
|
||||||
Employers that are looking for brilliant Software Engineers with good experience on Go Programming Language and Iris can put their startup's or company's name here or, if privacy is the key, [contact with us](mailto:kataras2006@hotmail.com?subject=Employer%20That%20Hires%20Smart%20Devs) to suggest some good and well-tested freelancers that suits your needs.
|
Employers that are looking for brilliant Software Engineers with good experience on Go Programming Language and Iris can put their startup's or company's name here or, if privacy is the key, [contact with us](mailto:kataras2006@hotmail.com?subject=Employer%20That%20Hires%20Smart%20Devs) to suggest some good and well-tested freelancers that suits your needs.
|
||||||
|
|
||||||
|
-->
|
||||||
|
|
||||||
### 🥇 People
|
### 🥇 People
|
||||||
|
|
||||||
The original author of _Iris_ is [@kataras](https://github.com/kataras), you can reach him via
|
The original author of _Iris_ is [@kataras](https://github.com/kataras), you can reach him via
|
||||||
|
|
|
@ -123,7 +123,7 @@ the template file via `Data` field.
|
||||||
|
|
||||||
Access to the template layout via the `Layout` 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`,
|
Flow as you used to, `Controllers` can be registered to any `Party`,
|
||||||
including Subdomains, the Party's begin and done handlers work as expected.
|
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)
|
- [Stream Writer](http_responsewriter/stream-writer/main.go)
|
||||||
- [Transactions](http_responsewriter/transactions/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
|
### ORM
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/middleware/basicauth"
|
"github.com/kataras/iris/middleware/basicauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +24,7 @@ func newApp() *iris.Application {
|
||||||
app.Get("/mysecret", authentication, h)
|
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
|
// to party
|
||||||
|
|
||||||
|
@ -49,7 +48,7 @@ func main() {
|
||||||
app.Run(iris.Addr(":8080"))
|
app.Run(iris.Addr(":8080"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func h(ctx context.Context) {
|
func h(ctx iris.Context) {
|
||||||
username, password, _ := ctx.Request().BasicAuth()
|
username, password, _ := ctx.Request().BasicAuth()
|
||||||
// third parameter it will be always true because the middleware
|
// third parameter it will be always true because the middleware
|
||||||
// makes sure for that, otherwise this handler will not be executed.
|
// makes sure for that, otherwise this handler will not be executed.
|
||||||
|
|
|
@ -25,7 +25,6 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
|
|
||||||
|
@ -95,7 +94,7 @@ func init() {
|
||||||
// the URL query string. If you provide it in a different way,
|
// the URL query string. If you provide it in a different way,
|
||||||
// assign your own function to this variable that returns the provider
|
// assign your own function to this variable that returns the provider
|
||||||
// name for your request.
|
// 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"
|
// try to get it from the url param "provider"
|
||||||
if p := ctx.URLParam("provider"); p != "" {
|
if p := ctx.URLParam("provider"); p != "" {
|
||||||
return p, nil
|
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.
|
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)
|
url, err := GetAuthURL(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.StatusCode(iris.StatusBadRequest)
|
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
|
I would recommend using the BeginAuthHandler instead of doing all of these steps
|
||||||
yourself, but that's entirely up to you.
|
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)
|
providerName, err := GetProviderName(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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.
|
// 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
|
// This state is sent to the provider and can be retrieved during the
|
||||||
// callback.
|
// callback.
|
||||||
var SetState = func(ctx context.Context) string {
|
var SetState = func(ctx iris.Context) string {
|
||||||
state := ctx.URLParam("state")
|
state := ctx.URLParam("state")
|
||||||
if len(state) > 0 {
|
if len(state) > 0 {
|
||||||
return state
|
return state
|
||||||
|
@ -186,7 +185,7 @@ var SetState = func(ctx context.Context) string {
|
||||||
// GetState gets the state returned by the provider during the callback.
|
// GetState gets the state returned by the provider during the callback.
|
||||||
// This is used to prevent CSRF attacks, see
|
// This is used to prevent CSRF attacks, see
|
||||||
// http://tools.ietf.org/html/rfc6749#section-10.12
|
// 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")
|
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.
|
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)
|
providerName, err := GetProviderName(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return goth.User{}, err
|
return goth.User{}, err
|
||||||
|
@ -237,7 +236,7 @@ var CompleteUserAuth = func(ctx context.Context) (goth.User, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout invalidates a user session.
|
// Logout invalidates a user session.
|
||||||
func Logout(ctx context.Context) error {
|
func Logout(ctx iris.Context) error {
|
||||||
providerName, err := GetProviderName(ctx)
|
providerName, err := GetProviderName(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -363,7 +362,7 @@ func main() {
|
||||||
|
|
||||||
// start of the router
|
// 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)
|
user, err := CompleteUserAuth(ctx)
|
||||||
if err != nil {
|
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)
|
Logout(ctx)
|
||||||
ctx.Redirect("/", iris.StatusTemporaryRedirect)
|
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
|
// try to get the user without re-authenticating
|
||||||
if gothUser, err := CompleteUserAuth(ctx); err == nil {
|
if gothUser, err := CompleteUserAuth(ctx); err == nil {
|
||||||
ctx.ViewData("", gothUser)
|
ctx.ViewData("", gothUser)
|
||||||
|
@ -394,7 +393,7 @@ func main() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
|
|
||||||
ctx.ViewData("", providerIndex)
|
ctx.ViewData("", providerIndex)
|
||||||
|
|
||||||
|
|
3
_examples/cache/simple/main.go
vendored
3
_examples/cache/simple/main.go
vendored
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/cache"
|
"github.com/kataras/iris/cache"
|
||||||
)
|
)
|
||||||
|
@ -67,7 +66,7 @@ func main() {
|
||||||
app.Run(iris.Addr(":8080"))
|
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
|
// tap multiple times the browser's refresh button and you will
|
||||||
// see this println only once every 10 seconds.
|
// see this println only once every 10 seconds.
|
||||||
println("Handler executed. Content refreshed.")
|
println("Handler executed. Content refreshed.")
|
||||||
|
|
|
@ -2,12 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Hello!</b>")
|
ctx.HTML("<b>Hello!</b>")
|
||||||
})
|
})
|
||||||
// [...]
|
// [...]
|
||||||
|
|
|
@ -2,13 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Hello!</b>")
|
ctx.HTML("<b>Hello!</b>")
|
||||||
})
|
})
|
||||||
// [...]
|
// [...]
|
||||||
|
|
|
@ -2,12 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Hello!</b>")
|
ctx.HTML("<b>Hello!</b>")
|
||||||
})
|
})
|
||||||
// [...]
|
// [...]
|
||||||
|
|
|
@ -2,12 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Hello!</b>")
|
ctx.HTML("<b>Hello!</b>")
|
||||||
})
|
})
|
||||||
// [...]
|
// [...]
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -13,14 +12,14 @@ func main() {
|
||||||
app.Use(ionMiddleware)
|
app.Use(ionMiddleware)
|
||||||
|
|
||||||
// Method GET: http://localhost:8080/
|
// Method GET: http://localhost:8080/
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<h1> Home </h1>")
|
ctx.HTML("<h1> Home </h1>")
|
||||||
// this will print an error,
|
// this will print an error,
|
||||||
// this route's handler will never be executed because the middleware's criteria not passed.
|
// this route's handler will never be executed because the middleware's criteria not passed.
|
||||||
})
|
})
|
||||||
|
|
||||||
// Method GET: http://localhost:8080/ok
|
// Method GET: http://localhost:8080/ok
|
||||||
app.Get("/ok", func(ctx context.Context) {
|
app.Get("/ok", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello world!")
|
ctx.Writef("Hello world!")
|
||||||
// this will print "OK. Hello world!".
|
// this will print "OK. Hello world!".
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -13,12 +12,12 @@ func main() {
|
||||||
app.Use(ionMiddleware)
|
app.Use(ionMiddleware)
|
||||||
|
|
||||||
// Method GET: http://localhost:8080/
|
// Method GET: http://localhost:8080/
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("Home")
|
ctx.HTML("Home")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Method GET: http://localhost:8080/ok
|
// 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>")
|
ctx.HTML("<b>Hello world!</b>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -14,7 +13,7 @@ func main() {
|
||||||
// app.Favicon("./static/favicons/favicon.ico.ico", "/favicon_16_16.ico")
|
// 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
|
// 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>.
|
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,
|
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,
|
because some browsers make requests to the /favicon.ico automatically,
|
||||||
|
|
|
@ -2,13 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
file := "./files/first.zip"
|
file := "./files/first.zip"
|
||||||
ctx.SendFile(file, "c.zip")
|
ctx.SendFile(file, "c.zip")
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,8 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"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
|
// 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 {
|
func newApp() *iris.Application {
|
||||||
app := iris.New()
|
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.ViewData("Page", page)
|
||||||
ctx.View("index.html")
|
ctx.View("index.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
// or just serve index.html as it is:
|
// 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)
|
// ctx.ServeFile("index.html", false)
|
||||||
// })
|
// })
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// $ go get -u github.com/jteeuwen/go-bindata/...
|
// $ go get -u github.com/jteeuwen/go-bindata/...
|
||||||
|
@ -17,9 +15,9 @@ var page = struct {
|
||||||
|
|
||||||
func newApp() *iris.Application {
|
func newApp() *iris.Application {
|
||||||
app := iris.New()
|
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.ViewData("Page", page)
|
||||||
ctx.View("index.html")
|
ctx.View("index.html")
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
// +build !go1.9
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/middleware/logger"
|
"github.com/kataras/iris/middleware/logger"
|
||||||
"github.com/kataras/iris/middleware/recover"
|
"github.com/kataras/iris/middleware/recover"
|
||||||
|
@ -20,21 +17,21 @@ func main() {
|
||||||
|
|
||||||
// Method: GET
|
// Method: GET
|
||||||
// Resource: http://localhost:8080/
|
// Resource: http://localhost:8080/
|
||||||
app.Handle("GET", "/", func(ctx context.Context) {
|
app.Handle("GET", "/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Welcome!</b>")
|
ctx.HTML("<b>Welcome!</b>")
|
||||||
})
|
})
|
||||||
|
|
||||||
// same as app.Handle("GET", "/ping", [...])
|
// same as app.Handle("GET", "/ping", [...])
|
||||||
// Method: GET
|
// Method: GET
|
||||||
// Resource: http://localhost:8080/ping
|
// Resource: http://localhost:8080/ping
|
||||||
app.Get("/ping", func(ctx context.Context) {
|
app.Get("/ping", func(ctx iris.Context) {
|
||||||
ctx.WriteString("pong")
|
ctx.WriteString("pong")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Method: GET
|
// Method: GET
|
||||||
// Resource: http://localhost:8080/hello
|
// Resource: http://localhost:8080/hello
|
||||||
app.Get("/hello", func(ctx context.Context) {
|
app.Get("/hello", func(ctx iris.Context) {
|
||||||
ctx.JSON(context.Map{"message": "Hello iris web framework."})
|
ctx.JSON(iris.Map{"message": "Hello iris web framework."})
|
||||||
})
|
})
|
||||||
|
|
||||||
// http://localhost:8080
|
// 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"
|
"net/http"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from the server")
|
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())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,17 +4,16 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from the server")
|
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())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,17 +4,16 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from the server")
|
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())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,17 +4,16 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from the server")
|
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())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ import (
|
||||||
"github.com/valyala/tcplisten"
|
"github.com/valyala/tcplisten"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// $ go get github.com/valyala/tcplisten
|
// $ go get github.com/valyala/tcplisten
|
||||||
|
@ -26,7 +25,7 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Hello World!</b>")
|
ctx.HTML("<b>Hello World!</b>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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>")
|
ctx.HTML("<h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Before continue:
|
// Before continue:
|
||||||
|
@ -26,7 +25,7 @@ func main() {
|
||||||
app.Shutdown(ctx)
|
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>")
|
ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/core/host"
|
"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
|
}) // 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)
|
ctx.Writef("Counter value = %d", counterValue)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<h1>Hello World!</h1>")
|
ctx.HTML("<h1>Hello World!</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<h1>Hello World!</h1>")
|
ctx.HTML("<h1>Hello World!</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -3,21 +3,20 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from SECURE SERVER!")
|
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!")
|
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")
|
ctx.Redirect("/test2")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/core/host"
|
"github.com/kataras/iris/core/host"
|
||||||
)
|
)
|
||||||
|
@ -12,11 +11,11 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from the SECURE server")
|
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")
|
ctx.Writef("Hello from the SECURE server on path /mypath")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,13 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/core/host"
|
"github.com/kataras/iris/core/host"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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>")
|
ctx.HTML("<h1>Hello, try to refresh the page after ~10 secs</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Visitor struct {
|
type Visitor struct {
|
||||||
|
@ -17,16 +15,16 @@ func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
// set the view html template engine
|
// 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 {
|
if err := ctx.View("form.html"); err != nil {
|
||||||
ctx.StatusCode(iris.StatusInternalServerError)
|
ctx.StatusCode(iris.StatusInternalServerError)
|
||||||
ctx.WriteString(err.Error())
|
ctx.WriteString(err.Error())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Post("/form_action", func(ctx context.Context) {
|
app.Post("/form_action", func(ctx iris.Context) {
|
||||||
visitor := Visitor{}
|
visitor := Visitor{}
|
||||||
err := ctx.ReadForm(&visitor)
|
err := ctx.ReadForm(&visitor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Company struct {
|
type Company struct {
|
||||||
|
@ -11,7 +10,7 @@ type Company struct {
|
||||||
Other string
|
Other string
|
||||||
}
|
}
|
||||||
|
|
||||||
func MyHandler(ctx context.Context) {
|
func MyHandler(ctx iris.Context) {
|
||||||
c := &Company{}
|
c := &Company{}
|
||||||
if err := ctx.ReadJSON(c); err != nil {
|
if err := ctx.ReadJSON(c); err != nil {
|
||||||
ctx.StatusCode(iris.StatusBadRequest)
|
ctx.StatusCode(iris.StatusBadRequest)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/middleware/logger"
|
"github.com/kataras/iris/middleware/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,7 +27,7 @@ func main() {
|
||||||
|
|
||||||
app.Use(customLogger)
|
app.Use(customLogger)
|
||||||
|
|
||||||
h := func(ctx context.Context) {
|
h := func(ctx iris.Context) {
|
||||||
ctx.Writef("Hello from %s", ctx.Path())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
}
|
}
|
||||||
app.Get("/", h)
|
app.Get("/", h)
|
||||||
|
@ -40,12 +39,12 @@ func main() {
|
||||||
// http errors have their own handlers, therefore
|
// http errors have their own handlers, therefore
|
||||||
// registering a middleare should be done manually.
|
// 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 ")
|
ctx.Writef("My Custom 404 error page ")
|
||||||
})
|
})
|
||||||
*/
|
*/
|
||||||
// or catch all http errors:
|
// 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`
|
// this should be added to the logs, at the end because of the `logger.Config#MessageContextKey`
|
||||||
ctx.Values().Set("logger_message",
|
ctx.Values().Set("logger_message",
|
||||||
"a dynamic message passed to the logs")
|
"a dynamic message passed to the logs")
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/middleware/logger"
|
"github.com/kataras/iris/middleware/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,11 +17,11 @@ func main() {
|
||||||
defer close()
|
defer close()
|
||||||
|
|
||||||
app.Use(r)
|
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>")
|
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())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +65,7 @@ var excludeExtensions = [...]string{
|
||||||
".svg",
|
".svg",
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRequestLogger() (h context.Handler, close func() error) {
|
func newRequestLogger() (h iris.Handler, close func() error) {
|
||||||
close = func() error { return nil }
|
close = func() error { return nil }
|
||||||
|
|
||||||
c := logger.Config{
|
c := logger.Config{
|
||||||
|
@ -93,7 +92,7 @@ func newRequestLogger() (h context.Handler, close func() error) {
|
||||||
|
|
||||||
// we don't want to use the logger
|
// we don't want to use the logger
|
||||||
// to log requests to assets and etc
|
// to log requests to assets and etc
|
||||||
c.AddSkipper(func(ctx context.Context) bool {
|
c.AddSkipper(func(ctx iris.Context) bool {
|
||||||
path := ctx.Path()
|
path := ctx.Path()
|
||||||
for _, ext := range excludeExtensions {
|
for _, ext := range excludeExtensions {
|
||||||
if strings.HasSuffix(path, ext) {
|
if strings.HasSuffix(path, ext) {
|
||||||
|
|
|
@ -9,17 +9,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.RegisterView(view.HTML("./templates", ".html"))
|
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||||
|
|
||||||
// Serve the form.html to the user
|
// 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)
|
//create a token (optionally)
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
@ -33,8 +31,8 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handle the post request from the upload_form.html to the server
|
// Handle the post request from the upload_form.html to the server
|
||||||
app.Post("/upload", context.LimitRequestBodySize(10<<20),
|
app.Post("/upload", iris.LimitRequestBodySize(10<<20),
|
||||||
func(ctx context.Context) {
|
func(ctx iris.Context) {
|
||||||
// or use ctx.SetMaxRequestBodySize(10 << 20)
|
// or use ctx.SetMaxRequestBodySize(10 << 20)
|
||||||
//to limit the uploaded file(s) size.
|
//to limit the uploaded file(s) size.
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,11 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris/_examples/http_responsewriter/quicktemplate/templates"
|
"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`.
|
// 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.Gzip(true)
|
||||||
ctx.ContentType("text/html")
|
ctx.ContentType("text/html")
|
||||||
templates.WriteTemplate(ctx.ResponseWriter(), tmpl)
|
templates.WriteTemplate(ctx.ResponseWriter(), tmpl)
|
||||||
|
|
|
@ -3,11 +3,11 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris/_examples/http_responsewriter/quicktemplate/templates"
|
"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.
|
// 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 := make(map[string]interface{})
|
||||||
// vars["message"] = "Hello World!"
|
// vars["message"] = "Hello World!"
|
||||||
// vars["name"] = ctx.Params().Get("name")
|
// vars["name"] = ctx.Params().Get("name")
|
||||||
|
|
|
@ -3,11 +3,11 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris/_examples/http_responsewriter/quicktemplate/templates"
|
"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.
|
// 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{}
|
tmpl := &templates.Index{}
|
||||||
|
|
||||||
// render the template
|
// render the template
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"time" // showcase the delay
|
"time" // showcase the delay
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -14,7 +13,7 @@ func main() {
|
||||||
|
|
||||||
timeWaitForCloseStream := 4 * time.Second
|
timeWaitForCloseStream := 4 * time.Second
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
i := 0
|
i := 0
|
||||||
// goroutine in order to no block and just wait,
|
// goroutine in order to no block and just wait,
|
||||||
// goroutine is OPTIONAL and not a very good option but it depends on the needs
|
// goroutine is OPTIONAL and not a very good option but it depends on the needs
|
||||||
|
@ -35,7 +34,7 @@ func main() {
|
||||||
time.Sleep(timeWaitForCloseStream)
|
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.
|
// Send the response in chunks and wait for a second between each chunk.
|
||||||
ctx.StreamWriter(func(w io.Writer) bool {
|
ctx.StreamWriter(func(w io.Writer) bool {
|
||||||
for i := 1; i <= 4; i++ {
|
for i := 1; i <= 4; i++ {
|
||||||
|
|
|
@ -26,7 +26,7 @@ func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
// Read
|
// Read
|
||||||
app.Post("/decode", func(ctx context.Context) {
|
app.Post("/decode", func(ctx iris.Context) {
|
||||||
var user User
|
var user User
|
||||||
ctx.ReadJSON(&user)
|
ctx.ReadJSON(&user)
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
app.Get("/encode", func(ctx context.Context) {
|
app.Get("/encode", func(ctx iris.Context) {
|
||||||
peter := User{
|
peter := User{
|
||||||
Firstname: "John",
|
Firstname: "John",
|
||||||
Lastname: "Doe",
|
Lastname: "Doe",
|
||||||
|
@ -48,28 +48,28 @@ func main() {
|
||||||
|
|
||||||
// Other content types,
|
// 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.
|
// useful when you want force-download of contents of raw bytes form.
|
||||||
ctx.Binary([]byte("Some binary data here."))
|
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")
|
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}
|
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"})
|
ctx.JSONP(map[string]string{"hello": "jsonp"}, context.JSONP{Callback: "callbackName"})
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/xml", func(ctx context.Context) {
|
app.Get("/xml", func(ctx iris.Context) {
|
||||||
ctx.XML(ExampleXML{One: "hello", Two: "xml"}) // or context.Map{"One":"hello"...}
|
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"))
|
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// get a filename based on the date, file logs works that way the most times
|
// 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.
|
// attach the file as logger, remember, iris' app logger is just an io.Writer.
|
||||||
app.Logger().SetOutput(newLogFile())
|
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
|
// for the sake of simplicity, in order see the logs at the ./_today_.txt
|
||||||
ctx.Application().Logger().Info("Request path: " + ctx.Path())
|
ctx.Application().Logger().Info("Request path: " + ctx.Path())
|
||||||
ctx.Writef("hello")
|
ctx.Writef("hello")
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/middleware/i18n"
|
"github.com/kataras/iris/middleware/i18n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,7 +16,7 @@ func newApp() *iris.Application {
|
||||||
"el-GR": "./locales/locale_el-GR.ini",
|
"el-GR": "./locales/locale_el-GR.ini",
|
||||||
"zh-CN": "./locales/locale_zh-CN.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:
|
// it tries to find the language by:
|
||||||
// ctx.Values().GetString("language")
|
// ctx.Values().GetString("language")
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/middleware/pprof"
|
"github.com/kataras/iris/middleware/pprof"
|
||||||
)
|
)
|
||||||
|
@ -10,7 +9,7 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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>")
|
ctx.HTML("<h1> Please click <a href='/debug/pprof'>here</a>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/middleware/recaptcha"
|
"github.com/kataras/iris/middleware/recaptcha"
|
||||||
)
|
)
|
||||||
|
@ -34,12 +33,12 @@ var htmlForm = `<form action="/comment" method="POST">
|
||||||
<input type="submit" name="button" value="Verify">
|
<input type="submit" name="button" value="Verify">
|
||||||
</form>`
|
</form>`
|
||||||
|
|
||||||
func showRecaptchaForm(ctx context.Context) {
|
func showRecaptchaForm(ctx iris.Context) {
|
||||||
contents := fmt.Sprintf(htmlForm, publicDataSiteKey)
|
contents := fmt.Sprintf(htmlForm, publicDataSiteKey)
|
||||||
ctx.HTML(contents)
|
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 (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/middleware/recover"
|
"github.com/kataras/iris/middleware/recover"
|
||||||
)
|
)
|
||||||
|
@ -14,7 +13,7 @@ func main() {
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
// let's simmilate a panic every next request
|
// let's simmilate a panic every next request
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
i++
|
i++
|
||||||
if i%2 == 0 {
|
if i%2 == 0 {
|
||||||
panic("a panic here")
|
panic("a panic here")
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kataras/iris/context"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/mvc"
|
"github.com/kataras/iris/mvc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ type AuthController struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginRequest saves login state to the context, the user id.
|
// 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)
|
c.SessionController.BeginRequest(ctx)
|
||||||
|
|
||||||
if userID := c.Session.Get(sessionIDKey); userID != nil {
|
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,
|
// AllowUser will check if this client is a logged user,
|
||||||
// if not then it will redirect that guest to the login page
|
// if not then it will redirect that guest to the login page
|
||||||
// otherwise it will allow the execution of the next handler.
|
// 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 {
|
if ctx.Values().Get(sessionIDKey) != nil {
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
return
|
return
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
@ -55,14 +54,14 @@ func main() {
|
||||||
app.Logger().Fatalf("orm failed to initialized User table: %v", err)
|
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()}
|
user := &User{Username: "kataras", Salt: "hash---", Password: "hashed", CreatedAt: time.Now(), UpdatedAt: time.Now()}
|
||||||
orm.Insert(user)
|
orm.Insert(user)
|
||||||
|
|
||||||
ctx.Writef("user inserted: %#v", 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}
|
user := User{ID: 1}
|
||||||
if ok, _ := orm.Get(&user); ok {
|
if ok, _ := orm.Get(&user); ok {
|
||||||
ctx.Writef("user found: %#v", user)
|
ctx.Writef("user found: %#v", user)
|
||||||
|
|
|
@ -2,8 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// User is just a bindable object structure.
|
// User is just a bindable object structure.
|
||||||
|
@ -22,10 +20,10 @@ func main() {
|
||||||
// Define templates using the std html/template engine.
|
// Define templates using the std html/template engine.
|
||||||
// Parse and load all files inside "./views" folder with ".html" file extension.
|
// Parse and load all files inside "./views" folder with ".html" file extension.
|
||||||
// Reload the templates on each request (development mode).
|
// 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.
|
// 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.
|
// .Values are used to communicate between handlers, middleware.
|
||||||
errMessage := ctx.Values().GetString("error")
|
errMessage := ctx.Values().GetString("error")
|
||||||
if errMessage != "" {
|
if errMessage != "" {
|
||||||
|
@ -36,22 +34,22 @@ func main() {
|
||||||
ctx.Writef("(Unexpected) internal server error")
|
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.Application().Logger().Infof("Begin request for path: %s", ctx.Path())
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
})
|
})
|
||||||
// app.Done(func(ctx context.Context) {]})
|
// app.Done(func(ctx iris.Context) {]})
|
||||||
app.Subdomain("wtf.").Post("/decode", func(ctx context.Context) {})
|
app.Subdomain("wtf.").Post("/decode", func(ctx iris.Context) {})
|
||||||
app.Subdomain("wtf.").Post("/decode", func(ctx context.Context) {})
|
app.Subdomain("wtf.").Post("/decode", func(ctx iris.Context) {})
|
||||||
// Method POST: http://localhost:8080/decode
|
// Method POST: http://localhost:8080/decode
|
||||||
app.Post("/decode", func(ctx context.Context) {
|
app.Post("/decode", func(ctx iris.Context) {
|
||||||
var user User
|
var user User
|
||||||
ctx.ReadJSON(&user)
|
ctx.ReadJSON(&user)
|
||||||
ctx.Writef("%s %s is %d years old and comes from %s", user.Firstname, user.Lastname, user.Age, user.City)
|
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
|
// Method GET: http://localhost:8080/encode
|
||||||
app.Get("/encode", func(ctx context.Context) {
|
app.Get("/encode", func(ctx iris.Context) {
|
||||||
doe := User{
|
doe := User{
|
||||||
Username: "Johndoe",
|
Username: "Johndoe",
|
||||||
Firstname: "John",
|
Firstname: "John",
|
||||||
|
@ -78,7 +76,7 @@ func main() {
|
||||||
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8"))
|
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())
|
ctx.Application().Logger().Infof("Path: %s | IP: %s", ctx.Path(), ctx.RemoteAddr())
|
||||||
|
|
||||||
// .Next is required to move forward to the chain of handlers,
|
// .Next is required to move forward to the chain of handlers,
|
||||||
|
@ -86,7 +84,7 @@ func logThisMiddleware(ctx context.Context) {
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
func profileByUsername(ctx context.Context) {
|
func profileByUsername(ctx iris.Context) {
|
||||||
// .Params are used to get dynamic path parameters.
|
// .Params are used to get dynamic path parameters.
|
||||||
username := ctx.Params().Get("username")
|
username := ctx.Params().Get("username")
|
||||||
ctx.ViewData("Username", username)
|
ctx.ViewData("Username", username)
|
||||||
|
@ -95,7 +93,7 @@ func profileByUsername(ctx context.Context) {
|
||||||
ctx.View("users/profile.html")
|
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...
|
userID := ctx.Params().Get("id") // Or convert directly using: .Values().GetInt/GetInt64 etc...
|
||||||
// your own db fetch here instead of user :=...
|
// your own db fetch here instead of user :=...
|
||||||
user := User{Username: "username" + userID}
|
user := User{Username: "username" + userID}
|
||||||
|
@ -103,7 +101,7 @@ func getUserByID(ctx context.Context) {
|
||||||
ctx.XML(user)
|
ctx.XML(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUser(ctx context.Context) {
|
func createUser(ctx iris.Context) {
|
||||||
var user User
|
var user User
|
||||||
err := ctx.ReadForm(&user)
|
err := ctx.ReadForm(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -14,23 +13,23 @@ func main() {
|
||||||
|
|
||||||
// GET -> HTTP Method
|
// GET -> HTTP Method
|
||||||
// / -> Path
|
// / -> 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.
|
// 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
|
// 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)
|
// 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 /
|
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", "/", [...])`)
|
ctx.Writef(`Same as app.Handle("GET", "/", [...])`)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/donate", donateHandler, donateFinishHandler)
|
app.Get("/donate", donateHandler, donateFinishHandler)
|
||||||
|
|
||||||
// Pssst, don't forget dynamic-path example for more "magic"!
|
// 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")
|
userID, err := ctx.Params().GetInt("userid")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -45,15 +44,15 @@ func main() {
|
||||||
"user_id": userID,
|
"user_id": userID,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
// app.Post("/", func(ctx context.Context){}) -> for POST http method.
|
// app.Post("/", func(ctx iris.Context){}) -> for POST http method.
|
||||||
// app.Put("/", func(ctx context.Context){})-> for "PUT" http method.
|
// app.Put("/", func(ctx iris.Context){})-> for "PUT" http method.
|
||||||
// app.Delete("/", func(ctx context.Context){})-> for "DELETE" http method.
|
// app.Delete("/", func(ctx iris.Context){})-> for "DELETE" http method.
|
||||||
// app.Options("/", func(ctx context.Context){})-> for "OPTIONS" http method.
|
// app.Options("/", func(ctx iris.Context){})-> for "OPTIONS" http method.
|
||||||
// app.Trace("/", func(ctx context.Context){})-> for "TRACE" http method.
|
// app.Trace("/", func(ctx iris.Context){})-> for "TRACE" http method.
|
||||||
// app.Head("/", func(ctx context.Context){})-> for "HEAD" http method.
|
// app.Head("/", func(ctx iris.Context){})-> for "HEAD" http method.
|
||||||
// app.Connect("/", func(ctx context.Context){})-> for "CONNECT" http method.
|
// app.Connect("/", func(ctx iris.Context){})-> for "CONNECT" http method.
|
||||||
// app.Patch("/", func(ctx context.Context){})-> for "PATCH" http method.
|
// app.Patch("/", func(ctx iris.Context){})-> for "PATCH" http method.
|
||||||
// app.Any("/", func(ctx context.Context){}) for all http methods.
|
// 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.
|
// More than one route can contain the same path with a different http mapped method.
|
||||||
// You can catch any route creation errors with:
|
// You can catch any route creation errors with:
|
||||||
|
@ -64,13 +63,13 @@ func main() {
|
||||||
|
|
||||||
adminRoutes := app.Party("/admin", adminMiddleware)
|
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())
|
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.
|
// adminRoutes.Layout("/views/layouts/admin.html") // set a view layout for these routes, see more at view examples.
|
||||||
|
|
||||||
// GET: http://localhost:8080/admin
|
// 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.StatusCode(iris.StatusOK) // default is 200 == iris.StatusOK
|
||||||
ctx.HTML("<h1>Hello from admin/</h1>")
|
ctx.HTML("<h1>Hello from admin/</h1>")
|
||||||
|
@ -79,11 +78,11 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// GET: http://localhost:8080/admin/login
|
// 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
|
// 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.
|
{ // braces are optional, it's just type of style, to group the routes visually.
|
||||||
|
|
||||||
// http://v1.localhost:8080
|
// 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>")
|
ctx.HTML("Version 1 API. go to <a href='" + ctx.Path() + "/api" + "'>/api/users</a>")
|
||||||
})
|
})
|
||||||
|
|
||||||
usersAPI := v1.Party("/api/users")
|
usersAPI := v1.Party("/api/users")
|
||||||
{
|
{
|
||||||
// http://v1.localhost:8080/api/users
|
// http://v1.localhost:8080/api/users
|
||||||
usersAPI.Get("/", func(ctx context.Context) {
|
usersAPI.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("All users")
|
ctx.Writef("All users")
|
||||||
})
|
})
|
||||||
// http://v1.localhost:8080/api/users/42
|
// 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"))
|
ctx.Writef("user with id: %s", ctx.Params().Get("userid"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -113,7 +112,7 @@ func main() {
|
||||||
// wildcard subdomains.
|
// wildcard subdomains.
|
||||||
wildcardSubdomain := app.Party("*.")
|
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())
|
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"))
|
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.
|
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 " +
|
ctx.Writef("Just like an inline handler, but it can be " +
|
||||||
"used by other package, anywhere in your project.")
|
"used by other package, anywhere in your project.")
|
||||||
|
|
||||||
// let's pass a value to the next handler
|
// let's pass a value to the next handler
|
||||||
// Values is the way handlers(or middleware) are communicating between each other.
|
// 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.
|
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
|
// 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.
|
// 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")
|
donateURL := ctx.Values().GetString("donate_url")
|
||||||
|
@ -160,7 +159,7 @@ func donateFinishHandler(ctx context.Context) {
|
||||||
ctx.Writef("\n\nDonate sent(?).")
|
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>.")
|
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"
|
"sync"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +24,7 @@ var owner = &Owner{
|
||||||
// Let's implement a context which will give us access
|
// Let's implement a context which will give us access
|
||||||
// to the client's Session with a trivial `ctx.Session()` call.
|
// to the client's Session with a trivial `ctx.Session()` call.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
context.Context
|
iris.Context
|
||||||
session *sessions.Session
|
session *sessions.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +48,7 @@ var contextPool = sync.Pool{New: func() interface{} {
|
||||||
return &Context{}
|
return &Context{}
|
||||||
}}
|
}}
|
||||||
|
|
||||||
func acquire(original context.Context) *Context {
|
func acquire(original iris.Context) *Context {
|
||||||
ctx := contextPool.Get().(*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.Context = original // set the context to the original one in order to have access to iris's implementation.
|
||||||
ctx.session = nil // reset the session
|
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,
|
// Handler will convert our handler of func(*Context) to an iris Handler,
|
||||||
// in order to be compatible with the HTTP API.
|
// in order to be compatible with the HTTP API.
|
||||||
func Handler(h func(*Context)) context.Handler {
|
func Handler(h func(*Context)) iris.Handler {
|
||||||
return func(original context.Context) {
|
return func(original iris.Context) {
|
||||||
ctx := acquire(original)
|
ctx := acquire(original)
|
||||||
h(ctx)
|
h(ctx)
|
||||||
release(ctx)
|
release(ctx)
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// In this example you'll just see one use case of .WrapRouter.
|
// In this example you'll just see one use case of .WrapRouter.
|
||||||
|
@ -20,15 +19,15 @@ func newApp() *iris.Application {
|
||||||
|
|
||||||
app := iris.New()
|
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>")
|
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)
|
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"))
|
ctx.Writef("Hello %s", ctx.Params().Get("username"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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.
|
// 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
|
// 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.
|
// 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.
|
// 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.
|
// 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"))
|
ctx.Writef("Hello %s", ctx.Params().Get("name"))
|
||||||
}) // type is missing = {name:string}
|
}) // type is missing = {name:string}
|
||||||
|
|
||||||
|
@ -116,7 +115,7 @@ func main() {
|
||||||
// http://localhost:8080/profile/id>=1
|
// http://localhost:8080/profile/id>=1
|
||||||
// this will throw 404 even if it's found as route on : /profile/0, /profile/blabla, /profile/-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.
|
// 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,
|
// second parameter is the error but it will always nil because we use macros,
|
||||||
// the validaton already happened.
|
// the validaton already happened.
|
||||||
id, _ := ctx.Params().GetInt("id")
|
id, _ := ctx.Params().GetInt("id")
|
||||||
|
@ -124,7 +123,7 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// to change the error code per route's macro evaluator:
|
// 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")
|
id, _ := ctx.Params().GetInt("id")
|
||||||
friendid, _ := ctx.Params().GetInt("friendid")
|
friendid, _ := ctx.Params().GetInt("friendid")
|
||||||
ctx.Writef("Hello id: %d looking for friend id: ", id, 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
|
// http://localhost:8080/game/a-zA-Z/level/0-9
|
||||||
// remember, alphabetical is lowercase or uppercase letters only.
|
// 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"))
|
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!")
|
ctx.Writef("static and dynamic paths are not conflicted anymore!")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -144,18 +143,18 @@ func main() {
|
||||||
// which its value is only lowercase letters.
|
// which its value is only lowercase letters.
|
||||||
|
|
||||||
// http://localhost:8080/lowercase/anylowercase
|
// 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"))
|
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
|
// 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"))
|
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/
|
// http://localhost:8080/myfiles/any/directory/here/
|
||||||
// this is the only macro type that accepts any number of path segments.
|
// 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"))
|
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:
|
}) // for wildcard path (any number of path segments) without validation you can use:
|
||||||
// /myfiles/*
|
// /myfiles/*
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -42,30 +41,30 @@ func main() {
|
||||||
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
|
||||||
}
|
}
|
||||||
|
|
||||||
func h(ctx context.Context) {
|
func h(ctx iris.Context) {
|
||||||
param := ctx.Params().Get("p")
|
param := ctx.Params().Get("p")
|
||||||
ctx.WriteString(param)
|
ctx.WriteString(param)
|
||||||
}
|
}
|
||||||
|
|
||||||
func staticWildcardH(ctx context.Context) {
|
func staticWildcardH(ctx iris.Context) {
|
||||||
param := ctx.Params().Get("p")
|
param := ctx.Params().Get("p")
|
||||||
ctx.WriteString("from staticWildcardH: param=" + param)
|
ctx.WriteString("from staticWildcardH: param=" + param)
|
||||||
}
|
}
|
||||||
|
|
||||||
func other(ctx context.Context) {
|
func other(ctx iris.Context) {
|
||||||
param := ctx.Params().Get("paramother")
|
param := ctx.Params().Get("paramother")
|
||||||
ctx.Writef("from other: %s", param)
|
ctx.Writef("from other: %s", param)
|
||||||
}
|
}
|
||||||
|
|
||||||
func other2(ctx context.Context) {
|
func other2(ctx iris.Context) {
|
||||||
param := ctx.Params().Get("paramothersecond")
|
param := ctx.Params().Get("paramothersecond")
|
||||||
ctx.Writef("from other2: %s", param)
|
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())
|
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())
|
ctx.Writef("from the static path(/other2/static2): %s", ctx.Path())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,26 +2,25 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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>")
|
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`)
|
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.Values().Set("message", "this is the error message")
|
||||||
ctx.StatusCode(500)
|
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"))
|
ctx.Writef("Hello %s", ctx.Params().Get("firstname"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"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) {
|
func registerErrors(app *iris.Application) {
|
||||||
// set a custom 404 handler
|
// set a custom 404 handler
|
||||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
|
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||||
ctx.HTML(notFoundHTML)
|
ctx.HTML(notFoundHTML)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerGamesRoutes(app *iris.Application) {
|
func registerGamesRoutes(app *iris.Application) {
|
||||||
gamesMiddleware := func(ctx context.Context) {
|
gamesMiddleware := func(ctx iris.Context) {
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +106,7 @@ func newApp() *iris.Application {
|
||||||
// and sends back the same body
|
// and sends back the same body
|
||||||
// remember, we have limit to that body in order
|
// remember, we have limit to that body in order
|
||||||
// to protect ourselves from "over heating".
|
// 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
|
// get request body
|
||||||
b, err := ioutil.ReadAll(ctx.Request().Body)
|
b, err := ioutil.ReadAll(ctx.Request().Body)
|
||||||
// if is larger then send a bad request status
|
// if is larger then send a bad request status
|
||||||
|
@ -123,7 +122,7 @@ func newApp() *iris.Application {
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func h(ctx context.Context) {
|
func h(ctx iris.Context) {
|
||||||
method := ctx.Method() // the http method requested a server's resource.
|
method := ctx.Method() // the http method requested a server's resource.
|
||||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -44,7 +43,7 @@ func main() {
|
||||||
|
|
||||||
usersRoutes := app.Party("/users")
|
usersRoutes := app.Party("/users")
|
||||||
// GET: http://localhost:8080/users/help
|
// 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 / -- fetch all users\n")
|
||||||
ctx.Writef("GET /$ID -- fetch a user by id\n")
|
ctx.Writef("GET /$ID -- fetch a user by id\n")
|
||||||
ctx.Writef("POST / -- create new user\n")
|
ctx.Writef("POST / -- create new user\n")
|
||||||
|
@ -53,32 +52,32 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// GET: http://localhost:8080/users
|
// GET: http://localhost:8080/users
|
||||||
usersRoutes.Get("/", func(ctx context.Context) {
|
usersRoutes.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("get all users")
|
ctx.Writef("get all users")
|
||||||
})
|
})
|
||||||
|
|
||||||
// GET: http://localhost:8080/users/42
|
// GET: http://localhost:8080/users/42
|
||||||
// **/users/42 and /users/help works after iris version 7.0.5**
|
// **/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")
|
id, _ := ctx.Params().GetInt("id")
|
||||||
ctx.Writef("get user by id: %d", id)
|
ctx.Writef("get user by id: %d", id)
|
||||||
})
|
})
|
||||||
|
|
||||||
// POST: http://localhost:8080/users
|
// 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")
|
username, password := ctx.PostValue("username"), ctx.PostValue("password")
|
||||||
ctx.Writef("create user for username= %s and password= %s", username, password)
|
ctx.Writef("create user for username= %s and password= %s", username, password)
|
||||||
})
|
})
|
||||||
|
|
||||||
// PUT: http://localhost:8080/users
|
// 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.
|
id, _ := ctx.Params().GetInt("id") // or .Get to get its string represatantion.
|
||||||
username := ctx.PostValue("username")
|
username := ctx.PostValue("username")
|
||||||
ctx.Writef("update user for id= %d and new username= %s", id, username)
|
ctx.Writef("update user for id= %d and new username= %s", id, username)
|
||||||
})
|
})
|
||||||
|
|
||||||
// DELETE: http://localhost:8080/users/42
|
// 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")
|
id, _ := ctx.Params().GetInt("id")
|
||||||
ctx.Writef("delete user by id: %d", 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.
|
method := ctx.Method() // the http method requested a server's resource.
|
||||||
subdomain := ctx.Subdomain() // the subdomain, if any.
|
subdomain := ctx.Subdomain() // the subdomain, if any.
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/core/router"
|
"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"}}
|
// you normally don't need it because of the {{ urlpath "routename" "path" "values" "here"}}
|
||||||
rv := router.NewRoutePathReverser(app)
|
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")
|
paramValue := ctx.Params().Get("anythingparameter")
|
||||||
ctx.Writef("The path after /anything is: %s", paramValue)
|
ctx.Writef("The path after /anything is: %s", paramValue)
|
||||||
})
|
})
|
||||||
|
@ -20,13 +19,13 @@ func main() {
|
||||||
myroute.Name = "myroute"
|
myroute.Name = "myroute"
|
||||||
|
|
||||||
// useful for links, although iris' view engine has the {{ urlpath "routename" "path values"}} already.
|
// 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")
|
myrouteRequestPath := rv.Path(myroute.Name, "any/path")
|
||||||
ctx.HTML("Should be <b>/anything/any/path</b>: " + myrouteRequestPath)
|
ctx.HTML("Should be <b>/anything/any/path</b>: " + myrouteRequestPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
// execute a route, similar to redirect but without redirect :)
|
// 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.
|
ctx.Exec("GET", "/anything/any/path") // like it was called by the client.
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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())
|
ctx.Writef("Hello %s with method: %s", ctx.Values().GetString("username"), ctx.Method())
|
||||||
|
|
||||||
if from := ctx.Values().GetString("from"); from != "" {
|
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() {
|
if none.IsOnline() {
|
||||||
none.Method = iris.MethodNone
|
none.Method = iris.MethodNone
|
||||||
|
@ -28,7 +27,7 @@ func main() {
|
||||||
app.RefreshRouter()
|
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
|
// same as navigating to "http://localhost:8080/invisible/iris" when /change has being invoked and route state changed
|
||||||
// from "offline" to "online"
|
// from "offline" to "online"
|
||||||
ctx.Values().Set("from", "/execute") // values and session can be shared when calling Exec from a "foreign" context.
|
ctx.Values().Set("from", "/execute") // values and session can be shared when calling Exec from a "foreign" context.
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
"github.com/kataras/iris/sessions/sessiondb/boltdb"
|
"github.com/kataras/iris/sessions/sessiondb/boltdb"
|
||||||
|
@ -33,10 +32,10 @@ func main() {
|
||||||
// the rest of the code stays the same.
|
// the rest of the code stays the same.
|
||||||
app := iris.New()
|
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")
|
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)
|
s := sess.Start(ctx)
|
||||||
//set session values
|
//set session values
|
||||||
s.Set("name", "iris")
|
s.Set("name", "iris")
|
||||||
|
@ -45,7 +44,7 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
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")
|
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
// set session values
|
// set session values
|
||||||
|
@ -55,36 +54,36 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString("name")
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
sess.Start(ctx).Delete("name")
|
sess.Start(ctx).Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
sess.Start(ctx).Clear()
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
sess.Destroy(ctx)
|
sess.Destroy(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/update", func(ctx context.Context) {
|
app.Get("/update", func(ctx iris.Context) {
|
||||||
// updates expire date with a new date
|
// updates expire date with a new date
|
||||||
sess.ShiftExpiration(ctx)
|
sess.ShiftExpiration(ctx)
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
"github.com/kataras/iris/sessions/sessiondb/file"
|
"github.com/kataras/iris/sessions/sessiondb/file"
|
||||||
|
@ -29,10 +28,10 @@ func main() {
|
||||||
// the rest of the code stays the same.
|
// the rest of the code stays the same.
|
||||||
app := iris.New()
|
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")
|
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)
|
s := sess.Start(ctx)
|
||||||
//set session values
|
//set session values
|
||||||
s.Set("name", "iris")
|
s.Set("name", "iris")
|
||||||
|
@ -41,7 +40,7 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
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")
|
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
// set session values
|
// set session values
|
||||||
|
@ -51,36 +50,36 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString("name")
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
sess.Start(ctx).Delete("name")
|
sess.Start(ctx).Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
sess.Start(ctx).Clear()
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
sess.Destroy(ctx)
|
sess.Destroy(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/update", func(ctx context.Context) {
|
app.Get("/update", func(ctx iris.Context) {
|
||||||
// updates expire date with a new date
|
// updates expire date with a new date
|
||||||
sess.ShiftExpiration(ctx)
|
sess.ShiftExpiration(ctx)
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
"github.com/kataras/iris/sessions/sessiondb/leveldb"
|
"github.com/kataras/iris/sessions/sessiondb/leveldb"
|
||||||
|
@ -34,10 +33,10 @@ func main() {
|
||||||
// the rest of the code stays the same.
|
// the rest of the code stays the same.
|
||||||
app := iris.New()
|
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")
|
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)
|
s := sess.Start(ctx)
|
||||||
//set session values
|
//set session values
|
||||||
s.Set("name", "iris")
|
s.Set("name", "iris")
|
||||||
|
@ -46,7 +45,7 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
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")
|
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
// set session values
|
// set session values
|
||||||
|
@ -56,36 +55,36 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString("name")
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
sess.Start(ctx).Delete("name")
|
sess.Start(ctx).Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
sess.Start(ctx).Clear()
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
sess.Destroy(ctx)
|
sess.Destroy(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/update", func(ctx context.Context) {
|
app.Get("/update", func(ctx iris.Context) {
|
||||||
// updates expire date with a new date
|
// updates expire date with a new date
|
||||||
sess.ShiftExpiration(ctx)
|
sess.ShiftExpiration(ctx)
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
"github.com/kataras/iris/sessions/sessiondb/redis"
|
"github.com/kataras/iris/sessions/sessiondb/redis"
|
||||||
|
@ -40,10 +39,10 @@ func main() {
|
||||||
// the rest of the code stays the same.
|
// the rest of the code stays the same.
|
||||||
app := iris.New()
|
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")
|
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)
|
s := sess.Start(ctx)
|
||||||
//set session values
|
//set session values
|
||||||
s.Set("name", "iris")
|
s.Set("name", "iris")
|
||||||
|
@ -52,29 +51,29 @@ func main() {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString("name")
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
sess.Start(ctx).Delete("name")
|
sess.Start(ctx).Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
sess.Start(ctx).Clear()
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
sess.Destroy(ctx)
|
sess.Destroy(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/update", func(ctx context.Context) {
|
app.Get("/update", func(ctx iris.Context) {
|
||||||
// updates expire date with a new date
|
// updates expire date with a new date
|
||||||
sess.ShiftExpiration(ctx)
|
sess.ShiftExpiration(ctx)
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
)
|
)
|
||||||
|
@ -11,13 +10,13 @@ func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
sess := sessions.New(sessions.Config{Cookie: "myappsessionid"})
|
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 := sess.Start(ctx)
|
||||||
s.SetFlash("name", "iris")
|
s.SetFlash("name", "iris")
|
||||||
ctx.Writef("Message setted, is available for the next request")
|
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)
|
s := sess.Start(ctx)
|
||||||
name := s.GetFlashString("name")
|
name := s.GetFlashString("name")
|
||||||
if name == "" {
|
if name == "" {
|
||||||
|
@ -27,7 +26,7 @@ func main() {
|
||||||
ctx.Writef("Hello %s", name)
|
ctx.Writef("Hello %s", name)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/test", func(ctx context.Context) {
|
app.Get("/test", func(ctx iris.Context) {
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
name := s.GetFlashString("name")
|
name := s.GetFlashString("name")
|
||||||
if name == "" {
|
if name == "" {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
)
|
)
|
||||||
|
@ -12,7 +11,7 @@ var (
|
||||||
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
|
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
|
||||||
)
|
)
|
||||||
|
|
||||||
func secret(ctx context.Context) {
|
func secret(ctx iris.Context) {
|
||||||
|
|
||||||
// Check if user is authenticated
|
// Check if user is authenticated
|
||||||
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
||||||
|
@ -24,7 +23,7 @@ func secret(ctx context.Context) {
|
||||||
ctx.WriteString("The cake is a lie!")
|
ctx.WriteString("The cake is a lie!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func login(ctx context.Context) {
|
func login(ctx iris.Context) {
|
||||||
session := sess.Start(ctx)
|
session := sess.Start(ctx)
|
||||||
|
|
||||||
// Authentication goes here
|
// Authentication goes here
|
||||||
|
@ -34,7 +33,7 @@ func login(ctx context.Context) {
|
||||||
session.Set("authenticated", true)
|
session.Set("authenticated", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func logout(ctx context.Context) {
|
func logout(ctx iris.Context) {
|
||||||
session := sess.Start(ctx)
|
session := sess.Start(ctx)
|
||||||
|
|
||||||
// Revoke users authentication
|
// Revoke users authentication
|
||||||
|
|
|
@ -7,7 +7,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
|
|
||||||
|
@ -30,10 +29,10 @@ func newApp() *iris.Application {
|
||||||
Decode: secureCookie.Decode,
|
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")
|
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
|
//set session values
|
||||||
s := mySessions.Start(ctx)
|
s := mySessions.Start(ctx)
|
||||||
|
@ -43,7 +42,7 @@ func newApp() *iris.Application {
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
s := mySessions.Start(ctx)
|
s := mySessions.Start(ctx)
|
||||||
name := s.GetString("name")
|
name := s.GetString("name")
|
||||||
|
@ -51,23 +50,23 @@ func newApp() *iris.Application {
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
s := mySessions.Start(ctx)
|
s := mySessions.Start(ctx)
|
||||||
s.Delete("name")
|
s.Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
mySessions.Start(ctx).Clear()
|
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
|
// updates expire date with a new date
|
||||||
mySessions.ShiftExpiration(ctx)
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
mySessions.Destroy(ctx)
|
mySessions.Destroy(ctx)
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
)
|
)
|
||||||
|
@ -31,10 +30,10 @@ func main() {
|
||||||
// want to be crazy safe? Take a look at the "securecookie" example folder.
|
// 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")
|
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.
|
//set session values.
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
|
@ -52,29 +51,29 @@ func main() {
|
||||||
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
// 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
|
// get a specific value, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString("name")
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
sess.Start(ctx).Delete("name")
|
sess.Start(ctx).Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
sess.Start(ctx).Clear()
|
sess.Start(ctx).Clear()
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/update", func(ctx context.Context) {
|
app.Get("/update", func(ctx iris.Context) {
|
||||||
// updates expire date
|
// updates expire date
|
||||||
sess.ShiftExpiration(ctx)
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
sess.Destroy(ctx)
|
sess.Destroy(ctx)
|
||||||
|
@ -91,7 +90,7 @@ func main() {
|
||||||
//
|
//
|
||||||
// Use `SetImmutable` consistently, it's slower than `Set`.
|
// Use `SetImmutable` consistently, it's slower than `Set`.
|
||||||
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
// 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"}}
|
business := []businessModel{{Name: "Edward"}, {Name: "value 2"}}
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
s.SetImmutable("businessEdit", business)
|
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")
|
valSlice := sess.Start(ctx).Get("businessEdit")
|
||||||
if valSlice == nil {
|
if valSlice == nil {
|
||||||
ctx.HTML("please navigate to the <a href='/set_immutable'>/set_immutable</a> first")
|
ctx.HTML("please navigate to the <a href='/set_immutable'>/set_immutable</a> first")
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -17,18 +16,18 @@ func main() {
|
||||||
|
|
||||||
dashboard := app.Party("dashboard.")
|
dashboard := app.Party("dashboard.")
|
||||||
{
|
{
|
||||||
dashboard.Get("/", func(ctx context.Context) {
|
dashboard.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("HEY FROM dashboard")
|
ctx.Writef("HEY FROM dashboard")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
system := app.Party("system.")
|
system := app.Party("system.")
|
||||||
{
|
{
|
||||||
system.Get("/", func(ctx context.Context) {
|
system.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("HEY FROM system")
|
ctx.Writef("HEY FROM system")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("HEY FROM frontend /")
|
ctx.Writef("HEY FROM frontend /")
|
||||||
})
|
})
|
||||||
// http://domain.local:80
|
// http://domain.local:80
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -15,26 +14,26 @@ func main() {
|
||||||
admin := app.Party("admin.")
|
admin := app.Party("admin.")
|
||||||
{
|
{
|
||||||
// admin.mydomain.com
|
// admin.mydomain.com
|
||||||
admin.Get("/", func(c context.Context) {
|
admin.Get("/", func(c iris.Context) {
|
||||||
c.Writef("INDEX FROM admin.mydomain.com")
|
c.Writef("INDEX FROM admin.mydomain.com")
|
||||||
})
|
})
|
||||||
// admin.mydomain.com/hey
|
// 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")
|
c.Writef("HEY FROM admin.mydomain.com/hey")
|
||||||
})
|
})
|
||||||
// admin.mydomain.com/hey2
|
// 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")
|
c.Writef("HEY SECOND FROM admin.mydomain.com/hey")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// mydomain.com/
|
// mydomain.com/
|
||||||
app.Get("/", func(c context.Context) {
|
app.Get("/", func(c iris.Context) {
|
||||||
c.Writef("INDEX FROM no-subdomain hey")
|
c.Writef("INDEX FROM no-subdomain hey")
|
||||||
})
|
})
|
||||||
|
|
||||||
// mydomain.com/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")
|
c.Writef("HEY FROM no-subdomain hey")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"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.
|
// 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 := app.Party("admin.")
|
||||||
{
|
{
|
||||||
// admin.mydomain.com
|
// admin.mydomain.com
|
||||||
admin.Get("/", func(ctx context.Context) {
|
admin.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("INDEX FROM admin.mydomain.com")
|
ctx.Writef("INDEX FROM admin.mydomain.com")
|
||||||
})
|
})
|
||||||
// admin.mydomain.com/hey
|
// 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")
|
ctx.Writef("HEY FROM admin.mydomain.com/hey")
|
||||||
})
|
})
|
||||||
// admin.mydomain.com/hey2
|
// 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")
|
ctx.Writef("HEY SECOND FROM admin.mydomain.com/hey")
|
||||||
})
|
})
|
||||||
}*/
|
}*/
|
||||||
|
@ -43,11 +42,11 @@ func main() {
|
||||||
dynamicSubdomains.Get("/something/{paramfirst}", dynamicSubdomainHandlerWithParam)
|
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())
|
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())
|
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
|
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()
|
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("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:
|
// 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
|
// 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()
|
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("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"))
|
ctx.Writef("The paramfirst is: %s", ctx.Params().Get("paramfirst"))
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newApp() *iris.Application {
|
func newApp() *iris.Application {
|
||||||
|
@ -53,7 +52,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func info(ctx context.Context) {
|
func info(ctx iris.Context) {
|
||||||
method := ctx.Method()
|
method := ctx.Method()
|
||||||
subdomain := ctx.Subdomain()
|
subdomain := ctx.Subdomain()
|
||||||
path := ctx.Path()
|
path := ctx.Path()
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/middleware/basicauth"
|
"github.com/kataras/iris/middleware/basicauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,7 +14,7 @@ func newApp() *iris.Application {
|
||||||
|
|
||||||
authentication := basicauth.New(authConfig)
|
authentication := basicauth.New(authConfig)
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })
|
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
|
||||||
|
|
||||||
// to party
|
// to party
|
||||||
|
|
||||||
|
@ -33,7 +32,7 @@ func newApp() *iris.Application {
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
func h(ctx context.Context) {
|
func h(ctx iris.Context) {
|
||||||
username, password, _ := ctx.Request().BasicAuth()
|
username, password, _ := ctx.Request().BasicAuth()
|
||||||
// third parameter it will be always true because the middleware
|
// third parameter it will be always true because the middleware
|
||||||
// makes sure for that, otherwise this handler will not be executed.
|
// 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"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/websocket"
|
"github.com/kataras/iris/websocket"
|
||||||
)
|
)
|
||||||
|
@ -26,12 +25,12 @@ func main() {
|
||||||
// register static assets request path and system directory
|
// register static assets request path and system directory
|
||||||
app.StaticWeb("/js", "./static/assets/js")
|
app.StaticWeb("/js", "./static/assets/js")
|
||||||
|
|
||||||
h := func(ctx context.Context) {
|
h := func(ctx iris.Context) {
|
||||||
ctx.ViewData("", page{PageID: "index page"})
|
ctx.ViewData("", page{PageID: "index page"})
|
||||||
ctx.View("index.html")
|
ctx.View("index.html")
|
||||||
}
|
}
|
||||||
|
|
||||||
h2 := func(ctx context.Context) {
|
h2 := func(ctx iris.Context) {
|
||||||
ctx.ViewData("", page{PageID: "other page"})
|
ctx.ViewData("", page{PageID: "other page"})
|
||||||
ctx.View("other.html")
|
ctx.View("other.html")
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -52,7 +51,7 @@ func newApp(db *DB) *iris.Application {
|
||||||
// Serve static files (css)
|
// Serve static files (css)
|
||||||
app.StaticWeb("/static", "./resources")
|
app.StaticWeb("/static", "./resources")
|
||||||
|
|
||||||
indexHandler := func(ctx context.Context) {
|
indexHandler := func(ctx iris.Context) {
|
||||||
ctx.ViewData("URL_COUNT", db.Len())
|
ctx.ViewData("URL_COUNT", db.Len())
|
||||||
ctx.View("index.html")
|
ctx.View("index.html")
|
||||||
}
|
}
|
||||||
|
@ -60,7 +59,7 @@ func newApp(db *DB) *iris.Application {
|
||||||
|
|
||||||
// find and execute a short url by its key
|
// find and execute a short url by its key
|
||||||
// used on http://localhost:8080/u/dsaoj41u321dsa
|
// used on http://localhost:8080/u/dsaoj41u321dsa
|
||||||
execShortURL := func(ctx context.Context, key string) {
|
execShortURL := func(ctx iris.Context, key string) {
|
||||||
if key == "" {
|
if key == "" {
|
||||||
ctx.StatusCode(iris.StatusBadRequest)
|
ctx.StatusCode(iris.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
@ -75,11 +74,11 @@ func newApp(db *DB) *iris.Application {
|
||||||
|
|
||||||
ctx.Redirect(value, iris.StatusTemporaryRedirect)
|
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"))
|
execShortURL(ctx, ctx.Params().Get("shortkey"))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Post("/shorten", func(ctx context.Context) {
|
app.Post("/shorten", func(ctx iris.Context) {
|
||||||
formValue := ctx.FormValue("url")
|
formValue := ctx.FormValue("url")
|
||||||
if formValue == "" {
|
if formValue == "" {
|
||||||
ctx.ViewData("FORM_RESULT", "You need to a enter a URL")
|
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.
|
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()
|
db.Clear()
|
||||||
ctx.Redirect("/")
|
ctx.Redirect("/")
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -19,7 +18,7 @@ func main() {
|
||||||
// set the view engine target to ./templates folder
|
// set the view engine target to ./templates folder
|
||||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
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
|
// 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)
|
ctx.ViewData("Title", DefaultTitle)
|
||||||
now := time.Now().Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
|
now := time.Now().Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
|
||||||
|
@ -29,14 +28,14 @@ func main() {
|
||||||
ctx.Next()
|
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")
|
ctx.ViewData("BodyMessage", "a sample text here... setted by the route handler")
|
||||||
if err := ctx.View("index.html"); err != nil {
|
if err := ctx.View("index.html"); err != nil {
|
||||||
ctx.Application().Logger().Infof(err.Error())
|
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("Title", "My About Page")
|
||||||
ctx.ViewData("BodyMessage", "about text here... setted by the route handler")
|
ctx.ViewData("BodyMessage", "about text here... setted by the route handler")
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -23,7 +22,7 @@ type page struct {
|
||||||
Title, Name string
|
Title, Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func hi(ctx context.Context) {
|
func hi(ctx iris.Context) {
|
||||||
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
||||||
ctx.View("hi.html")
|
ctx.View("hi.html")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -21,7 +20,7 @@ func main() {
|
||||||
// - {{ yield }}
|
// - {{ yield }}
|
||||||
// - {{ current }}
|
// - {{ current }}
|
||||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
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.ViewData("Name", "iris") // the .Name inside the ./templates/hi.html
|
||||||
ctx.Gzip(true) // enable gzip for big files
|
ctx.Gzip(true) // enable gzip for big files
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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.
|
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("Title", "Hi Page")
|
||||||
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
|
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
|
||||||
// ctx.ViewData("", myCcustomStruct{})
|
// ctx.ViewData("", myCcustomStruct{})
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type mypage struct {
|
type mypage struct {
|
||||||
|
@ -16,7 +15,7 @@ func main() {
|
||||||
app.RegisterView(iris.HTML("./templates", ".html").Layout("layout.html"))
|
app.RegisterView(iris.HTML("./templates", ".html").Layout("layout.html"))
|
||||||
// TIP: append .Reload(true) to reload the templates on each request.
|
// 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.Gzip(true)
|
||||||
ctx.ViewData("", mypage{"My Page title", "Hello world!"})
|
ctx.ViewData("", mypage{"My Page title", "Hello world!"})
|
||||||
ctx.View("mypage.html")
|
ctx.View("mypage.html")
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -16,7 +15,7 @@ func main() {
|
||||||
|
|
||||||
app.RegisterView(tmpl)
|
app.RegisterView(tmpl)
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
if err := ctx.View("page1.html"); err != nil {
|
if err := ctx.View("page1.html"); err != nil {
|
||||||
ctx.StatusCode(iris.StatusInternalServerError)
|
ctx.StatusCode(iris.StatusInternalServerError)
|
||||||
ctx.Writef(err.Error())
|
ctx.Writef(err.Error())
|
||||||
|
@ -24,7 +23,7 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// remove the layout for a specific route
|
// 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)
|
ctx.ViewLayout(iris.NoLayout)
|
||||||
if err := ctx.View("page1.html"); err != nil {
|
if err := ctx.View("page1.html"); err != nil {
|
||||||
ctx.StatusCode(iris.StatusInternalServerError)
|
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
|
// 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")
|
my := app.Party("/my").Layout("layouts/mylayout.html")
|
||||||
{ // both of these will use the layouts/mylayout.html as their layout.
|
{ // 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")
|
ctx.View("page1.html")
|
||||||
})
|
})
|
||||||
my.Get("/other", func(ctx context.Context) {
|
my.Get("/other", func(ctx iris.Context) {
|
||||||
ctx.View("page1.html")
|
ctx.View("page1.html")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -30,7 +29,7 @@ func main() {
|
||||||
mypath6Route := app.Get("/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}", writePathHandler)
|
mypath6Route := app.Get("/mypath6/{paramfirst}/{paramsecond}/statichere/{paramThirdAfterStatic}", writePathHandler)
|
||||||
mypath6Route.Name = "my-page6"
|
mypath6Route.Name = "my-page6"
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
// for /mypath6...
|
// for /mypath6...
|
||||||
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
|
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
|
||||||
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
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")
|
routeName := ctx.Params().Get("namedRoute")
|
||||||
r := app.GetRoute(routeName)
|
r := app.GetRoute(routeName)
|
||||||
if r == nil {
|
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())
|
ctx.Writef("Hello from %s.", ctx.Path())
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/core/router"
|
"github.com/kataras/iris/core/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,7 +47,7 @@ func main() {
|
||||||
mypath6Route := subdomain.Get("/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}", emptyHandler)
|
mypath6Route := subdomain.Get("/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}", emptyHandler)
|
||||||
mypath6Route.Name = "my-page6"
|
mypath6Route.Name = "my-page6"
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
// for username5./mypath6...
|
// for username5./mypath6...
|
||||||
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
|
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
|
||||||
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
||||||
|
@ -61,7 +60,7 @@ func main() {
|
||||||
app.Run(iris.Addr(host))
|
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())
|
ctx.Writef("Hello from subdomain: %s , you're in path: %s", ctx.Subdomain(), ctx.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/websocket"
|
"github.com/kataras/iris/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?
|
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,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// 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)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/websocket"
|
"github.com/kataras/iris/websocket"
|
||||||
)
|
)
|
||||||
|
@ -19,7 +17,7 @@ type clientPage struct {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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{})
|
ws := websocket.New(websocket.Config{})
|
||||||
|
|
||||||
|
@ -29,13 +27,13 @@ func main() {
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// 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)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.StaticWeb("/js", "./static/js") // serve our custom javascript code
|
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.ViewData("", clientPage{"Client Page", "localhost:8080"})
|
||||||
ctx.View("client.html")
|
ctx.View("client.html")
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,8 +4,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/websocket"
|
"github.com/kataras/iris/websocket"
|
||||||
)
|
)
|
||||||
|
@ -26,7 +24,7 @@ type clientPage struct {
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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{
|
ws := websocket.New(websocket.Config{
|
||||||
// to enable binary messages (useful for protobuf):
|
// to enable binary messages (useful for protobuf):
|
||||||
|
@ -39,7 +37,7 @@ func main() {
|
||||||
|
|
||||||
app.StaticWeb("/js", "./static/js") // serve our custom javascript code
|
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.ViewData("", clientPage{"Client Page", "localhost:8080"})
|
||||||
ctx.View("client.html")
|
ctx.View("client.html")
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,8 +7,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/view"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/websocket"
|
"github.com/kataras/iris/websocket"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +18,7 @@ type clientPage struct {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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{})
|
ws := websocket.New(websocket.Config{})
|
||||||
|
|
||||||
|
@ -30,12 +28,12 @@ func main() {
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// 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)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.StaticWeb("/js", "./static/js")
|
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
|
// send our custom javascript source file before client really asks for that
|
||||||
// using the go v1.8's HTTP/2 Push.
|
// using the go v1.8's HTTP/2 Push.
|
||||||
// Note that you have to listen using ListenTLS in order this to work.
|
// Note that you have to listen using ListenTLS in order this to work.
|
||||||
|
@ -49,7 +47,7 @@ func main() {
|
||||||
var myChatRoom = "room1"
|
var myChatRoom = "room1"
|
||||||
|
|
||||||
ws.OnConnection(func(c websocket.Connection) {
|
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,
|
// avoid using it, you normally don't need it,
|
||||||
// websocket has everything you need to authenticate the user BUT if it's necessary
|
// 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.
|
// then you use it to receive user information, for example: from headers.
|
||||||
|
|
|
@ -16,8 +16,6 @@ import (
|
||||||
// README.md: Hello World with Go 1.9
|
// README.md: Hello World with Go 1.9
|
||||||
// core/host/supervisor.go
|
// core/host/supervisor.go
|
||||||
// context.go
|
// context.go
|
||||||
// _examples/hello-world/main_go19.go
|
|
||||||
// _examples/tutorial/mvc-from-scratch/README.md
|
|
||||||
type (
|
type (
|
||||||
// Context is the midle-man server's "object" for the clients.
|
// Context is the midle-man server's "object" for the clients.
|
||||||
//
|
//
|
||||||
|
|
185
doc.go
185
doc.go
|
@ -39,7 +39,7 @@ Current Version
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
|
|
||||||
The only requirement is the Go Programming Language, at least version 1.8
|
The only requirement is the Go Programming Language, at least version 1.8 but 1.9 is highly recommended.
|
||||||
|
|
||||||
$ go get -u github.com/kataras/iris
|
$ go get -u github.com/kataras/iris
|
||||||
|
|
||||||
|
@ -49,10 +49,7 @@ Example code:
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris"
|
||||||
"github.com/kataras/iris"
|
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
// User is just a bindable object structure.
|
// User is just a bindable object structure.
|
||||||
type User struct {
|
type User struct {
|
||||||
|
@ -72,7 +69,7 @@ Example code:
|
||||||
app.RegisterView(iris.HTML("./views", ".html").Reload(true))
|
app.RegisterView(iris.HTML("./views", ".html").Reload(true))
|
||||||
|
|
||||||
// Register custom handler for specific http errors.
|
// 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.
|
// .Values are used to communicate between handlers, middleware.
|
||||||
errMessage := ctx.Values().GetString("error")
|
errMessage := ctx.Values().GetString("error")
|
||||||
if errMessage != "" {
|
if errMessage != "" {
|
||||||
|
@ -83,22 +80,22 @@ Example code:
|
||||||
ctx.Writef("(Unexpected) internal server error")
|
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.Application().Logger().Infof("Begin request for path: %s", ctx.Path())
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
})
|
})
|
||||||
|
|
||||||
// app.Done(func(ctx context.Context) {})
|
// app.Done(func(ctx iris.Context) {})
|
||||||
|
|
||||||
// Method POST: http://localhost:8080/decode
|
// Method POST: http://localhost:8080/decode
|
||||||
app.Post("/decode", func(ctx context.Context) {
|
app.Post("/decode", func(ctx iris.Context) {
|
||||||
var user User
|
var user User
|
||||||
ctx.ReadJSON(&user)
|
ctx.ReadJSON(&user)
|
||||||
ctx.Writef("%s %s is %d years old and comes from %s", user.Firstname, user.Lastname, user.Age, user.City)
|
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
|
// Method GET: http://localhost:8080/encode
|
||||||
app.Get("/encode", func(ctx context.Context) {
|
app.Get("/encode", func(ctx iris.Context) {
|
||||||
doe := User{
|
doe := User{
|
||||||
Username: "Johndoe",
|
Username: "Johndoe",
|
||||||
Firstname: "John",
|
Firstname: "John",
|
||||||
|
@ -125,7 +122,7 @@ Example code:
|
||||||
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8"))
|
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())
|
ctx.Application().Logger().Infof("Path: %s | IP: %s", ctx.Path(), ctx.RemoteAddr())
|
||||||
|
|
||||||
// .Next is required to move forward to the chain of handlers,
|
// .Next is required to move forward to the chain of handlers,
|
||||||
|
@ -133,7 +130,7 @@ Example code:
|
||||||
ctx.Next()
|
ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
func profileByUsername(ctx context.Context) {
|
func profileByUsername(ctx iris.Context) {
|
||||||
// .Params are used to get dynamic path parameters.
|
// .Params are used to get dynamic path parameters.
|
||||||
username := ctx.Params().Get("username")
|
username := ctx.Params().Get("username")
|
||||||
ctx.ViewData("Username", username)
|
ctx.ViewData("Username", username)
|
||||||
|
@ -142,7 +139,7 @@ Example code:
|
||||||
ctx.View("users/profile.html")
|
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...
|
userID := ctx.Params().Get("id") // Or convert directly using: .Values().GetInt/GetInt64 etc...
|
||||||
// your own db fetch here instead of user :=...
|
// your own db fetch here instead of user :=...
|
||||||
user := User{Username: "username" + userID}
|
user := User{Username: "username" + userID}
|
||||||
|
@ -150,7 +147,7 @@ Example code:
|
||||||
ctx.XML(user)
|
ctx.XML(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUser(ctx context.Context) {
|
func createUser(ctx iris.Context) {
|
||||||
var user User
|
var user User
|
||||||
err := ctx.ReadForm(&user)
|
err := ctx.ReadForm(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -232,7 +229,6 @@ Example code:
|
||||||
"github.com/valyala/tcplisten"
|
"github.com/valyala/tcplisten"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// $ go get github.com/valyala/tcplisten
|
// $ go get github.com/valyala/tcplisten
|
||||||
|
@ -241,7 +237,7 @@ Example code:
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.HTML("<b>Hello World!</b>")
|
ctx.HTML("<b>Hello World!</b>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -280,7 +276,6 @@ Example code:
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -295,7 +290,7 @@ Example code:
|
||||||
app.Shutdown(ctx)
|
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>")
|
ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -350,14 +345,13 @@ Example Code:
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
"github.com/kataras/iris/core/host"
|
"github.com/kataras/iris/core/host"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
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>")
|
ctx.HTML("<h1>Hello, try to refresh the page after ~10 secs</h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -406,7 +400,7 @@ Routing
|
||||||
All HTTP methods are supported, developers can also register handlers for same paths for different methods.
|
All HTTP methods are supported, developers can also register handlers for same paths for different methods.
|
||||||
The first parameter is the HTTP Method,
|
The first parameter is the HTTP Method,
|
||||||
second parameter is the request path of the route,
|
second parameter is the request path of the route,
|
||||||
third variadic parameter should contains one or more context.Handler executed
|
third variadic parameter should contains one or more iris.Handler executed
|
||||||
by the registered order when a user requests for that specific resouce path from the server.
|
by the registered order when a user requests for that specific resouce path from the server.
|
||||||
|
|
||||||
Example code:
|
Example code:
|
||||||
|
@ -414,14 +408,14 @@ Example code:
|
||||||
|
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Handle("GET", "/contact", func(ctx context.Context){
|
app.Handle("GET", "/contact", func(ctx iris.Context) {
|
||||||
ctx.HTML("<h1> Hello from /contact </h1>")
|
ctx.HTML("<h1> Hello from /contact </h1>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
In order to make things easier for the user, iris provides functions for all HTTP Methods.
|
In order to make things easier for the user, iris provides functions for all HTTP Methods.
|
||||||
The first parameter is the request path of the route,
|
The first parameter is the request path of the route,
|
||||||
second variadic parameter should contains one or more context.Handler executed
|
second variadic parameter should contains one or more iris.Handler executed
|
||||||
by the registered order when a user requests for that specific resouce path from the server.
|
by the registered order when a user requests for that specific resouce path from the server.
|
||||||
|
|
||||||
Example code:
|
Example code:
|
||||||
|
@ -459,7 +453,7 @@ Example code:
|
||||||
// register the route for all HTTP Methods
|
// register the route for all HTTP Methods
|
||||||
app.Any("/", handler)
|
app.Any("/", handler)
|
||||||
|
|
||||||
func handler(ctx context.Context){
|
func handler(ctx iris.Context){
|
||||||
ctx.Writef("Hello from method: %s and path: %s", ctx.Method(), ctx.Path())
|
ctx.Writef("Hello from method: %s and path: %s", ctx.Method(), ctx.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -497,11 +491,11 @@ Example code:
|
||||||
|
|
||||||
|
|
||||||
// when 404 then render the template $templatedir/errors/404.html
|
// when 404 then render the template $templatedir/errors/404.html
|
||||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context){
|
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context){
|
||||||
ctx.View("errors/404.html")
|
ctx.View("errors/404.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.OnErrorCode(500, func(ctx context.Context){
|
app.OnErrorCode(500, func(ctx iris.Context){
|
||||||
// ...
|
// ...
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -515,10 +509,7 @@ Example code:
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris"
|
||||||
"github.com/kataras/iris"
|
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -529,23 +520,23 @@ Example code:
|
||||||
|
|
||||||
// GET -> HTTP Method
|
// GET -> HTTP Method
|
||||||
// / -> Path
|
// / -> 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.
|
// 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
|
// 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)
|
// 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 /
|
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", "/", [...])`)
|
ctx.Writef(`Same as app.Handle("GET", "/", [...])`)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/donate", donateHandler, donateFinishHandler)
|
app.Get("/donate", donateHandler, donateFinishHandler)
|
||||||
|
|
||||||
// Pssst, don't forget dynamic-path example for more "magic"!
|
// 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")
|
userID, err := ctx.Params().GetInt("userid")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -560,15 +551,15 @@ Example code:
|
||||||
"user_id": userID,
|
"user_id": userID,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
// app.Post("/", func(ctx context.Context){}) -> for POST http method.
|
// app.Post("/", func(ctx iris.Context){}) -> for POST http method.
|
||||||
// app.Put("/", func(ctx context.Context){})-> for "PUT" http method.
|
// app.Put("/", func(ctx iris.Context){})-> for "PUT" http method.
|
||||||
// app.Delete("/", func(ctx context.Context){})-> for "DELETE" http method.
|
// app.Delete("/", func(ctx iris.Context){})-> for "DELETE" http method.
|
||||||
// app.Options("/", func(ctx context.Context){})-> for "OPTIONS" http method.
|
// app.Options("/", func(ctx iris.Context){})-> for "OPTIONS" http method.
|
||||||
// app.Trace("/", func(ctx context.Context){})-> for "TRACE" http method.
|
// app.Trace("/", func(ctx iris.Context){})-> for "TRACE" http method.
|
||||||
// app.Head("/", func(ctx context.Context){})-> for "HEAD" http method.
|
// app.Head("/", func(ctx iris.Context){})-> for "HEAD" http method.
|
||||||
// app.Connect("/", func(ctx context.Context){})-> for "CONNECT" http method.
|
// app.Connect("/", func(ctx iris.Context){})-> for "CONNECT" http method.
|
||||||
// app.Patch("/", func(ctx context.Context){})-> for "PATCH" http method.
|
// app.Patch("/", func(ctx iris.Context){})-> for "PATCH" http method.
|
||||||
// app.Any("/", func(ctx context.Context){}) for all http methods.
|
// 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.
|
// More than one route can contain the same path with a different http mapped method.
|
||||||
// You can catch any route creation errors with:
|
// You can catch any route creation errors with:
|
||||||
|
@ -579,13 +570,13 @@ Example code:
|
||||||
|
|
||||||
adminRoutes := app.Party("/admin", adminMiddleware)
|
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())
|
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.
|
// adminRoutes.Layout("/views/layouts/admin.html") // set a view layout for these routes, see more at view examples.
|
||||||
|
|
||||||
// GET: http://localhost:8080/admin
|
// 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.StatusCode(iris.StatusOK) // default is 200 == iris.StatusOK
|
||||||
ctx.HTML("<h1>Hello from admin/</h1>")
|
ctx.HTML("<h1>Hello from admin/</h1>")
|
||||||
|
@ -594,11 +585,11 @@ Example code:
|
||||||
})
|
})
|
||||||
|
|
||||||
// GET: http://localhost:8080/admin/login
|
// 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
|
// POST: http://localhost:8080/admin/login
|
||||||
adminRoutes.Post("/login", func(ctx context.Context) {
|
adminRoutes.Post("/login", func(ctx iris.Context) {
|
||||||
// [...]
|
// [...]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -608,18 +599,18 @@ Example code:
|
||||||
{ // braces are optional, it's just type of style, to group the routes visually.
|
{ // braces are optional, it's just type of style, to group the routes visually.
|
||||||
|
|
||||||
// http://v1.localhost:8080
|
// 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>")
|
ctx.HTML("Version 1 API. go to <a href='" + ctx.Path() + "/api" + "'>/api/users</a>")
|
||||||
})
|
})
|
||||||
|
|
||||||
usersAPI := v1.Party("/api/users")
|
usersAPI := v1.Party("/api/users")
|
||||||
{
|
{
|
||||||
// http://v1.localhost:8080/api/users
|
// http://v1.localhost:8080/api/users
|
||||||
usersAPI.Get("/", func(ctx context.Context) {
|
usersAPI.Get("/", func(ctx iris.Context) {
|
||||||
ctx.Writef("All users")
|
ctx.Writef("All users")
|
||||||
})
|
})
|
||||||
// http://v1.localhost:8080/api/users/42
|
// 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"))
|
ctx.Writef("user with id: %s", ctx.Params().Get("userid"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -628,7 +619,7 @@ Example code:
|
||||||
// wildcard subdomains.
|
// wildcard subdomains.
|
||||||
wildcardSubdomain := app.Party("*.")
|
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())
|
ctx.Writef("Subdomain can be anything, now you're here from: %s", ctx.Subdomain())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -652,22 +643,22 @@ Example code:
|
||||||
app.Run(iris.Addr(":8080"))
|
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.
|
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 " +
|
ctx.Writef("Just like an inline handler, but it can be " +
|
||||||
"used by other package, anywhere in your project.")
|
"used by other package, anywhere in your project.")
|
||||||
|
|
||||||
// let's pass a value to the next handler
|
// let's pass a value to the next handler
|
||||||
// Values is the way handlers(or middleware) are communicating between each other.
|
// 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.
|
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
|
// 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.
|
// 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")
|
donateURL := ctx.Values().GetString("donate_url")
|
||||||
|
@ -675,7 +666,7 @@ Example code:
|
||||||
ctx.Writef("\n\nDonate sent(?).")
|
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>.")
|
ctx.HTML("Custom route for 404 not found http code, here you can render a view, html, json <b>any valid response</b>.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -806,7 +797,7 @@ the template file via `Data` field.
|
||||||
|
|
||||||
Access to the template layout via the `Layout` 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` via the `Ctx` field.
|
||||||
|
|
||||||
Get the relative request path by using the controller's name via `RelPath()`.
|
Get the relative request path by using the controller's name via `RelPath()`.
|
||||||
|
|
||||||
|
@ -873,11 +864,10 @@ we've seen static routes, group of routes, subdomains, wildcard subdomains, a sm
|
||||||
with a single known paramete and custom http errors, now it's time to see wildcard parameters and macros.
|
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
|
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.
|
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.
|
|
||||||
|
|
||||||
iris has the easiest and the most powerful routing process you have ever meet.
|
Iris has the easiest and the most powerful routing process you have ever meet.
|
||||||
|
|
||||||
At the same time,
|
At the same time,
|
||||||
iris has its own interpeter(yes like a programming language)
|
iris has its own interpeter(yes like a programming language)
|
||||||
|
@ -959,7 +949,7 @@ Example code:
|
||||||
|
|
||||||
|
|
||||||
// you can use the "string" type which is valid for a single path parameter that can be anything.
|
// 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"))
|
ctx.Writef("Hello %s", ctx.Params().Get("name"))
|
||||||
}) // type is missing = {name:string}
|
}) // type is missing = {name:string}
|
||||||
|
|
||||||
|
@ -983,7 +973,7 @@ Example code:
|
||||||
// http://localhost:8080/profile/id>=1
|
// http://localhost:8080/profile/id>=1
|
||||||
// this will throw 404 even if it's found as route on : /profile/0, /profile/blabla, /profile/-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.
|
// 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,
|
// second parameter is the error but it will always nil because we use macros,
|
||||||
// the validaton already happened.
|
// the validaton already happened.
|
||||||
id, _ := ctx.Params().GetInt("id")
|
id, _ := ctx.Params().GetInt("id")
|
||||||
|
@ -991,7 +981,7 @@ Example code:
|
||||||
})
|
})
|
||||||
|
|
||||||
// to change the error code per route's macro evaluator:
|
// 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")
|
id, _ := ctx.Params().GetInt("id")
|
||||||
friendid, _ := ctx.Params().GetInt("friendid")
|
friendid, _ := ctx.Params().GetInt("friendid")
|
||||||
ctx.Writef("Hello id: %d looking for friend id: ", id, friendid)
|
ctx.Writef("Hello id: %d looking for friend id: ", id, friendid)
|
||||||
|
@ -999,7 +989,7 @@ Example code:
|
||||||
|
|
||||||
// http://localhost:8080/game/a-zA-Z/level/0-9
|
// http://localhost:8080/game/a-zA-Z/level/0-9
|
||||||
// remember, alphabetical is lowercase or uppercase letters only.
|
// 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"))
|
ctx.Writef("name: %s | level: %s", ctx.Params().Get("name"), ctx.Params().Get("level"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1007,18 +997,18 @@ Example code:
|
||||||
// which its value is only lowercase letters.
|
// which its value is only lowercase letters.
|
||||||
|
|
||||||
// http://localhost:8080/lowercase/anylowercase
|
// 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"))
|
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
|
// 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"))
|
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/
|
// http://localhost:8080/myfiles/any/directory/here/
|
||||||
// this is the only macro type that accepts any number of path segments.
|
// 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"))
|
ctx.Writef("path type accepts any number of path segments, path after /myfiles/ is: %s", ctx.Params().Get("directory"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1121,10 +1111,7 @@ Example code:
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris"
|
||||||
"github.com/kataras/iris"
|
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -1135,7 +1122,7 @@ Example code:
|
||||||
// app.Favicon("./static/favicons/ion_32_32.ico", "/favicon_48_48.ico")
|
// app.Favicon("./static/favicons/ion_32_32.ico", "/favicon_48_48.ico")
|
||||||
// This will serve the ./static/favicons/ion_32_32.ico to: localhost:8080/favicon_48_48.ico
|
// This will serve the ./static/favicons/ion_32_32.ico to: localhost:8080/favicon_48_48.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>.
|
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,
|
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,
|
because some browsers make requests to the /favicon.ico automatically,
|
||||||
|
@ -1158,7 +1145,7 @@ Example code:
|
||||||
|
|
||||||
// globally
|
// globally
|
||||||
// before any routes, appends the middleware to all routes
|
// before any routes, appends the middleware to all routes
|
||||||
app.Use(func(ctx context.Context){
|
app.Use(func(ctx iris.Context){
|
||||||
// ... any code here
|
// ... any code here
|
||||||
|
|
||||||
ctx.Next() // in order to continue to the next handler,
|
ctx.Next() // in order to continue to the next handler,
|
||||||
|
@ -1184,7 +1171,7 @@ Example code:
|
||||||
|
|
||||||
// per wildcard, dynamic subdomain
|
// per wildcard, dynamic subdomain
|
||||||
dynamicSub := app.Party(".*", firstMiddleware, secondMiddleware)
|
dynamicSub := app.Party(".*", firstMiddleware, secondMiddleware)
|
||||||
dynamicSub.Get("/", func(ctx context.Context){
|
dynamicSub.Get("/", func(ctx iris.Context){
|
||||||
ctx.Writef("Hello from subdomain: "+ ctx.Subdomain())
|
ctx.Writef("Hello from subdomain: "+ ctx.Subdomain())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1201,7 +1188,6 @@ Example code:
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -1226,7 +1212,7 @@ Example code:
|
||||||
app.Run(iris.Addr(":8080"))
|
app.Run(iris.Addr(":8080"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func h(ctx context.Context) {
|
func h(ctx iris.Context) {
|
||||||
ctx.Application().Logger().Infof(ctx.Path())
|
ctx.Application().Logger().Infof(ctx.Path())
|
||||||
ctx.Writef("Hello from %s", ctx.Path())
|
ctx.Writef("Hello from %s", ctx.Path())
|
||||||
}
|
}
|
||||||
|
@ -1236,7 +1222,7 @@ View Engine
|
||||||
|
|
||||||
|
|
||||||
iris supports 5 template engines out-of-the-box, developers can still use any external golang template engine,
|
iris supports 5 template engines out-of-the-box, developers can still use any external golang template engine,
|
||||||
as `context.ResponseWriter()` is an `io.Writer`.
|
as `context/context#ResponseWriter()` is an `io.Writer`.
|
||||||
|
|
||||||
All of these five template engines have common features with common API,
|
All of these five template engines have common features with common API,
|
||||||
like Layout, Template Funcs, Party-specific layout, partial rendering and more.
|
like Layout, Template Funcs, Party-specific layout, partial rendering and more.
|
||||||
|
@ -1261,10 +1247,7 @@ Example code:
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris"
|
||||||
"github.com/kataras/iris"
|
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New() // defaults to these
|
app := iris.New() // defaults to these
|
||||||
|
@ -1295,7 +1278,7 @@ Example code:
|
||||||
app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8")) // defaults to that but you can change it.
|
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("Title", "Hi Page")
|
||||||
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
|
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
|
||||||
// ctx.ViewData("", myCcustomStruct{})
|
// ctx.ViewData("", myCcustomStruct{})
|
||||||
|
@ -1312,10 +1295,7 @@ Example code:
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "github.com/kataras/iris"
|
||||||
"github.com/kataras/iris"
|
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -1335,7 +1315,7 @@ Example code:
|
||||||
Title, Name string
|
Title, Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func hi(ctx context.Context) {
|
func hi(ctx iris.Context) {
|
||||||
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
ctx.ViewData("", page{Title: "Hi Page", Name: "iris"})
|
||||||
ctx.View("hi.html")
|
ctx.View("hi.html")
|
||||||
}
|
}
|
||||||
|
@ -1388,7 +1368,6 @@ Example code:
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
)
|
)
|
||||||
|
@ -1398,7 +1377,7 @@ Example code:
|
||||||
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
|
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID})
|
||||||
)
|
)
|
||||||
|
|
||||||
func secret(ctx context.Context) {
|
func secret(ctx iris.Context) {
|
||||||
|
|
||||||
// Check if user is authenticated
|
// Check if user is authenticated
|
||||||
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
||||||
|
@ -1410,7 +1389,7 @@ Example code:
|
||||||
ctx.WriteString("The cake is a lie!")
|
ctx.WriteString("The cake is a lie!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func login(ctx context.Context) {
|
func login(ctx iris.Context) {
|
||||||
session := sess.Start(ctx)
|
session := sess.Start(ctx)
|
||||||
|
|
||||||
// Authentication goes here
|
// Authentication goes here
|
||||||
|
@ -1420,7 +1399,7 @@ Example code:
|
||||||
session.Set("authenticated", true)
|
session.Set("authenticated", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func logout(ctx context.Context) {
|
func logout(ctx iris.Context) {
|
||||||
session := sess.Start(ctx)
|
session := sess.Start(ctx)
|
||||||
|
|
||||||
// Revoke users authentication
|
// Revoke users authentication
|
||||||
|
@ -1464,7 +1443,6 @@ Example Code:
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/sessions"
|
"github.com/kataras/iris/sessions"
|
||||||
"github.com/kataras/iris/sessions/sessiondb/boltdb" // <- IMPORTANT
|
"github.com/kataras/iris/sessions/sessiondb/boltdb" // <- IMPORTANT
|
||||||
|
@ -1493,10 +1471,10 @@ Example Code:
|
||||||
// the rest of the code stays the same.
|
// the rest of the code stays the same.
|
||||||
app := iris.New()
|
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")
|
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)
|
s := sess.Start(ctx)
|
||||||
//set session values
|
//set session values
|
||||||
s.Set("name", "iris")
|
s.Set("name", "iris")
|
||||||
|
@ -1505,7 +1483,7 @@ Example Code:
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString("name"))
|
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")
|
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||||
s := sess.Start(ctx)
|
s := sess.Start(ctx)
|
||||||
// set session values
|
// set session values
|
||||||
|
@ -1515,36 +1493,36 @@ Example Code:
|
||||||
ctx.Writef("All ok session setted to: %s", s.GetString(key))
|
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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString("name")
|
name := sess.Start(ctx).GetString("name")
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// get a specific key, as string, if no found returns just an empty string
|
||||||
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
name := sess.Start(ctx).GetString(ctx.Params().Get("key"))
|
||||||
|
|
||||||
ctx.Writef("The name on the /set was: %s", 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
|
// delete a specific key
|
||||||
sess.Start(ctx).Delete("name")
|
sess.Start(ctx).Delete("name")
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/clear", func(ctx context.Context) {
|
app.Get("/clear", func(ctx iris.Context) {
|
||||||
// removes all entries
|
// removes all entries
|
||||||
sess.Start(ctx).Clear()
|
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
|
//destroy, removes the entire session data and cookie
|
||||||
sess.Destroy(ctx)
|
sess.Destroy(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/update", func(ctx context.Context) {
|
app.Get("/update", func(ctx iris.Context) {
|
||||||
// updates expire date with a new date
|
// updates expire date with a new date
|
||||||
sess.ShiftExpiration(ctx)
|
sess.ShiftExpiration(ctx)
|
||||||
})
|
})
|
||||||
|
@ -1571,7 +1549,6 @@ Example Server Code:
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/websocket"
|
"github.com/kataras/iris/websocket"
|
||||||
)
|
)
|
||||||
|
@ -1579,7 +1556,7 @@ Example Server Code:
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?
|
ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1606,7 +1583,7 @@ Example Server Code:
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// 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)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/typescript/editor"
|
"github.com/kataras/iris/typescript/editor"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +12,7 @@ func main() {
|
||||||
// when you edit a typescript file from the alm-tools
|
// when you edit a typescript file from the alm-tools
|
||||||
// it compiles it to javascript, have fun!
|
// it compiles it to javascript, have fun!
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.ServeFile("./www/index.html", false)
|
ctx.ServeFile("./www/index.html", false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris"
|
"github.com/kataras/iris"
|
||||||
"github.com/kataras/iris/context"
|
|
||||||
|
|
||||||
"github.com/kataras/iris/typescript"
|
"github.com/kataras/iris/typescript"
|
||||||
)
|
)
|
||||||
|
@ -17,7 +16,7 @@ func main() {
|
||||||
|
|
||||||
app.StaticWeb("/scripts", "./www") // serve the scripts
|
app.StaticWeb("/scripts", "./www") // serve the scripts
|
||||||
|
|
||||||
app.Get("/", func(ctx context.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.ServeFile("./www/index.html", false)
|
ctx.ServeFile("./www/index.html", false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user