mirror of
https://github.com/kataras/iris.git
synced 2026-01-17 00:45:55 +00:00
8.4.0 | New MVC Features | Refactor examples and godoc for go 1.9 use. Read HISTORY.md.
Former-commit-id: 90c05e743052bc3722e7fefaa0cbb0ed5153a1fb
This commit is contained in:
@@ -3,8 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
type Visitor struct {
|
||||
@@ -17,16 +15,16 @@ func main() {
|
||||
app := iris.New()
|
||||
|
||||
// set the view html template engine
|
||||
app.RegisterView(view.HTML("./templates", ".html").Reload(true))
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
if err := ctx.View("form.html"); err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.WriteString(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Post("/form_action", func(ctx context.Context) {
|
||||
app.Post("/form_action", func(ctx iris.Context) {
|
||||
visitor := Visitor{}
|
||||
err := ctx.ReadForm(&visitor)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
type Company struct {
|
||||
@@ -11,7 +10,7 @@ type Company struct {
|
||||
Other string
|
||||
}
|
||||
|
||||
func MyHandler(ctx context.Context) {
|
||||
func MyHandler(ctx iris.Context) {
|
||||
c := &Company{}
|
||||
if err := ctx.ReadJSON(c); err != nil {
|
||||
ctx.StatusCode(iris.StatusBadRequest)
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
)
|
||||
|
||||
@@ -28,7 +27,7 @@ func main() {
|
||||
|
||||
app.Use(customLogger)
|
||||
|
||||
h := func(ctx context.Context) {
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
app.Get("/", h)
|
||||
@@ -40,12 +39,12 @@ func main() {
|
||||
// http errors have their own handlers, therefore
|
||||
// registering a middleare should be done manually.
|
||||
/*
|
||||
app.OnErrorCode(404 ,customLogger, func(ctx context.Context) {
|
||||
app.OnErrorCode(404 ,customLogger, func(ctx iris.Context) {
|
||||
ctx.Writef("My Custom 404 error page ")
|
||||
})
|
||||
*/
|
||||
// or catch all http errors:
|
||||
app.OnAnyErrorCode(customLogger, func(ctx context.Context) {
|
||||
app.OnAnyErrorCode(customLogger, func(ctx iris.Context) {
|
||||
// this should be added to the logs, at the end because of the `logger.Config#MessageContextKey`
|
||||
ctx.Values().Set("logger_message",
|
||||
"a dynamic message passed to the logs")
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/middleware/logger"
|
||||
)
|
||||
|
||||
@@ -18,11 +17,11 @@ func main() {
|
||||
defer close()
|
||||
|
||||
app.Use(r)
|
||||
app.OnAnyErrorCode(r, func(ctx context.Context) {
|
||||
app.OnAnyErrorCode(r, func(ctx iris.Context) {
|
||||
ctx.HTML("<h1> Error: Please try <a href ='/'> this </a> instead.</h1>")
|
||||
})
|
||||
|
||||
h := func(ctx context.Context) {
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
|
||||
@@ -66,7 +65,7 @@ var excludeExtensions = [...]string{
|
||||
".svg",
|
||||
}
|
||||
|
||||
func newRequestLogger() (h context.Handler, close func() error) {
|
||||
func newRequestLogger() (h iris.Handler, close func() error) {
|
||||
close = func() error { return nil }
|
||||
|
||||
c := logger.Config{
|
||||
@@ -93,7 +92,7 @@ func newRequestLogger() (h context.Handler, close func() error) {
|
||||
|
||||
// we don't want to use the logger
|
||||
// to log requests to assets and etc
|
||||
c.AddSkipper(func(ctx context.Context) bool {
|
||||
c.AddSkipper(func(ctx iris.Context) bool {
|
||||
path := ctx.Path()
|
||||
for _, ext := range excludeExtensions {
|
||||
if strings.HasSuffix(path, ext) {
|
||||
|
||||
@@ -9,17 +9,15 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.RegisterView(view.HTML("./templates", ".html"))
|
||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||
|
||||
// Serve the form.html to the user
|
||||
app.Get("/upload", func(ctx context.Context) {
|
||||
app.Get("/upload", func(ctx iris.Context) {
|
||||
//create a token (optionally)
|
||||
|
||||
now := time.Now().Unix()
|
||||
@@ -33,8 +31,8 @@ func main() {
|
||||
})
|
||||
|
||||
// Handle the post request from the upload_form.html to the server
|
||||
app.Post("/upload", context.LimitRequestBodySize(10<<20),
|
||||
func(ctx context.Context) {
|
||||
app.Post("/upload", iris.LimitRequestBodySize(10<<20),
|
||||
func(ctx iris.Context) {
|
||||
// or use ctx.SetMaxRequestBodySize(10 << 20)
|
||||
//to limit the uploaded file(s) size.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user