mirror of
https://github.com/kataras/iris.git
synced 2025-12-27 14:57:05 +00:00
ok almost finished. We're good at deadlines eventually. Tomorrow at 23:59 an article will be published same time with the dev branch merge to master
Former-commit-id: 42c1bf88cedbddf3cc01366ab769139546902e71
This commit is contained in:
@@ -112,6 +112,11 @@ Navigate through examples for a better understanding.
|
||||
* [per-route](routing/writing-a-middleware/per-route/main.go)
|
||||
* [globally](routing/writing-a-middleware/globally/main.go)
|
||||
|
||||
### hero
|
||||
|
||||
- [Basic](hero/basic/main.go)
|
||||
- [Overview](hero/overview)
|
||||
|
||||
### MVC
|
||||
|
||||

|
||||
@@ -313,7 +318,8 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
|
||||
|
||||
- [Bind JSON](http_request/read-json/main.go)
|
||||
- [Bind Form](http_request/read-form/main.go)
|
||||
- [Upload/Read Files](http_request/upload-files/main.go)
|
||||
- [Upload/Read File](http_request/upload-file/main.go)
|
||||
- [Upload multiple files with an easy way](http_request/upload-files/main.go)
|
||||
|
||||
> The `context.Request()` returns the same *http.Request you already know, these examples show some places where the Context uses this object. Besides that you can use it as you did before iris.
|
||||
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
|
||||

|
||||
|
||||
## References
|
||||
## 3. Per-Request - Dynamic Dependencies
|
||||
|
||||
- [explore](https://github.com/kataras/explore)
|
||||

|
||||
73
_examples/http_request/upload-file/main.go
Normal file
73
_examples/http_request/upload-file/main.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.RegisterView(iris.HTML("./templates", ".html"))
|
||||
|
||||
// Serve the upload_form.html to the client.
|
||||
app.Get("/upload", func(ctx iris.Context) {
|
||||
// create a token (optionally).
|
||||
|
||||
now := time.Now().Unix()
|
||||
h := md5.New()
|
||||
io.WriteString(h, strconv.FormatInt(now, 10))
|
||||
token := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
// render the form with the token for any use you'd like.
|
||||
// ctx.ViewData("", token)
|
||||
// or add second argument to the `View` method.
|
||||
// Token will be passed as {{.}} in the template.
|
||||
ctx.View("upload_form.html", token)
|
||||
})
|
||||
|
||||
// Handle the post request from the upload_form.html to the server
|
||||
app.Post("/upload", func(ctx iris.Context) {
|
||||
// iris.LimitRequestBodySize(32 <<20) as middleware to a route
|
||||
// or use ctx.SetMaxRequestBodySize(32 << 20)
|
||||
// to limit the whole request body size,
|
||||
//
|
||||
// or let the configuration option at app.Run for global setting
|
||||
// for POST/PUT methods, including uploads of course.
|
||||
|
||||
// Get the file from the request.
|
||||
file, info, err := ctx.FormFile("uploadfile")
|
||||
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
||||
return
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
fname := info.Filename
|
||||
|
||||
// Create a file with the same name
|
||||
// assuming that you have a folder named 'uploads'
|
||||
out, err := os.OpenFile("./uploads/"+fname,
|
||||
os.O_WRONLY|os.O_CREATE, 0666)
|
||||
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
||||
return
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
io.Copy(out, file)
|
||||
})
|
||||
|
||||
// start the server at http://localhost:8080 with post limit at 32 MB.
|
||||
app.Run(iris.Addr(":8080"), iris.WithPostMaxMemory(32<<20))
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Upload file</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data"
|
||||
action="http://127.0.0.1:8080/upload" method="POST">
|
||||
<input type="file" name="uploadfile" /> <input type="hidden"
|
||||
name="token" value="{{.}}" /> <input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
0
_examples/http_request/upload-file/uploads/.gitkeep
Normal file
0
_examples/http_request/upload-file/uploads/.gitkeep
Normal file
@@ -4,8 +4,9 @@ import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"mime/multipart"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
@@ -26,46 +27,39 @@ func main() {
|
||||
token := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
// render the form with the token for any use you'd like.
|
||||
ctx.ViewData("", token)
|
||||
ctx.View("upload_form.html")
|
||||
ctx.View("upload_form.html", token)
|
||||
})
|
||||
|
||||
// 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", func(ctx iris.Context) {
|
||||
// iris.LimitRequestBodySize(32 <<20) as middleware to a route
|
||||
// or use ctx.SetMaxRequestBodySize(32 << 20)
|
||||
// to limit the whole request body size,
|
||||
//
|
||||
// or let the configuration option at app.Run for global setting
|
||||
// for POST/PUT methods, including uploads of course.
|
||||
// UploadFormFiles
|
||||
// uploads any number of incoming files (multiple property on the form input).
|
||||
//
|
||||
|
||||
// Get the file from the request.
|
||||
file, info, err := ctx.FormFile("uploadfile")
|
||||
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
||||
return
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
fname := info.Filename
|
||||
|
||||
// Create a file with the same name
|
||||
// assuming that you have a folder named 'uploads'
|
||||
out, err := os.OpenFile("./uploads/"+fname,
|
||||
os.O_WRONLY|os.O_CREATE, 0666)
|
||||
|
||||
if err != nil {
|
||||
ctx.StatusCode(iris.StatusInternalServerError)
|
||||
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
||||
return
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
io.Copy(out, file)
|
||||
// second argument is totally optionally,
|
||||
// it can be used to change a file's name based on the request,
|
||||
// at this example we will showcase how to use it
|
||||
// by prefixing the uploaded file with the current user's ip.
|
||||
ctx.UploadFormFiles("./uploads", beforeSave)
|
||||
})
|
||||
|
||||
// start the server at http://localhost:8080 with post limit at 32 MB.
|
||||
app.Run(iris.Addr(":8080"), iris.WithPostMaxMemory(32<<20))
|
||||
}
|
||||
|
||||
func beforeSave(ctx iris.Context, file *multipart.FileHeader) {
|
||||
ip := ctx.RemoteAddr()
|
||||
// make sure you format the ip in a way
|
||||
// that can be used for a file name (simple case):
|
||||
ip = strings.Replace(ip, ".", "_", -1)
|
||||
ip = strings.Replace(ip, ":", "_", -1)
|
||||
|
||||
// you can use the time.Now, to prefix or suffix the files
|
||||
// based on the current time as well, as an exercise.
|
||||
// i.e unixTime := time.Now().Unix()
|
||||
// prefix the Filename with the $IP-
|
||||
// no need for more actions, internal uploader will use this
|
||||
// name to save the file into the "./uploads" folder.
|
||||
file.Filename = ip + "-" + file.Filename
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<body>
|
||||
<form enctype="multipart/form-data"
|
||||
action="http://127.0.0.1:8080/upload" method="POST">
|
||||
<input type="file" name="uploadfile" /> <input type="hidden"
|
||||
<input type="file" name="uploadfile" multiple/> <input type="hidden"
|
||||
name="token" value="{{.}}" /> <input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
//
|
||||
|
||||
//line base.qtpl:3
|
||||
|
||||
package templates
|
||||
|
||||
//line base.qtpl:3
|
||||
|
||||
import (
|
||||
qtio422016 "io"
|
||||
|
||||
@@ -15,12 +17,14 @@ import (
|
||||
)
|
||||
|
||||
//line base.qtpl:3
|
||||
|
||||
var (
|
||||
_ = qtio422016.Copy
|
||||
_ = qt422016.AcquireByteBuffer
|
||||
)
|
||||
|
||||
//line base.qtpl:4
|
||||
|
||||
type Partial interface {
|
||||
//line base.qtpl:4
|
||||
Body() string
|
||||
@@ -29,11 +33,13 @@ type Partial interface {
|
||||
//line base.qtpl:4
|
||||
WriteBody(qq422016 qtio422016.Writer)
|
||||
//line base.qtpl:4
|
||||
|
||||
}
|
||||
|
||||
// Template writes a template implementing the Partial interface.
|
||||
|
||||
//line base.qtpl:11
|
||||
|
||||
func StreamTemplate(qw422016 *qt422016.Writer, p Partial) {
|
||||
//line base.qtpl:11
|
||||
qw422016.N().S(`
|
||||
@@ -61,9 +67,11 @@ func StreamTemplate(qw422016 *qt422016.Writer, p Partial) {
|
||||
</html>
|
||||
`)
|
||||
//line base.qtpl:30
|
||||
|
||||
}
|
||||
|
||||
//line base.qtpl:30
|
||||
|
||||
func WriteTemplate(qq422016 qtio422016.Writer, p Partial) {
|
||||
//line base.qtpl:30
|
||||
qw422016 := qt422016.AcquireWriter(qq422016)
|
||||
@@ -72,9 +80,11 @@ func WriteTemplate(qq422016 qtio422016.Writer, p Partial) {
|
||||
//line base.qtpl:30
|
||||
qt422016.ReleaseWriter(qw422016)
|
||||
//line base.qtpl:30
|
||||
|
||||
}
|
||||
|
||||
//line base.qtpl:30
|
||||
|
||||
func Template(p Partial) string {
|
||||
//line base.qtpl:30
|
||||
qb422016 := qt422016.AcquireByteBuffer()
|
||||
@@ -87,21 +97,27 @@ func Template(p Partial) string {
|
||||
//line base.qtpl:30
|
||||
return qs422016
|
||||
//line base.qtpl:30
|
||||
|
||||
}
|
||||
|
||||
// Base template implementation. Other pages may inherit from it if they need
|
||||
// overriding only certain Partial methods.
|
||||
|
||||
//line base.qtpl:35
|
||||
|
||||
type Base struct{}
|
||||
|
||||
//line base.qtpl:36
|
||||
|
||||
func (b *Base) StreamBody(qw422016 *qt422016.Writer) {
|
||||
//line base.qtpl:36
|
||||
qw422016.N().S(`This is the base body`) }
|
||||
|
||||
qw422016.N().S(`This is the base body`)
|
||||
}
|
||||
|
||||
//line base.qtpl:36
|
||||
//line base.qtpl:36
|
||||
|
||||
func (b *Base) WriteBody(qq422016 qtio422016.Writer) {
|
||||
//line base.qtpl:36
|
||||
qw422016 := qt422016.AcquireWriter(qq422016)
|
||||
@@ -110,9 +126,11 @@ func (b *Base) WriteBody(qq422016 qtio422016.Writer) {
|
||||
//line base.qtpl:36
|
||||
qt422016.ReleaseWriter(qw422016)
|
||||
//line base.qtpl:36
|
||||
|
||||
}
|
||||
|
||||
//line base.qtpl:36
|
||||
|
||||
func (b *Base) Body() string {
|
||||
//line base.qtpl:36
|
||||
qb422016 := qt422016.AcquireByteBuffer()
|
||||
@@ -125,4 +143,5 @@ func (b *Base) Body() string {
|
||||
//line base.qtpl:36
|
||||
return qs422016
|
||||
//line base.qtpl:36
|
||||
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
//
|
||||
|
||||
//line hello.qtpl:3
|
||||
|
||||
package templates
|
||||
|
||||
//line hello.qtpl:3
|
||||
|
||||
import (
|
||||
qtio422016 "io"
|
||||
|
||||
@@ -15,17 +17,20 @@ import (
|
||||
)
|
||||
|
||||
//line hello.qtpl:3
|
||||
|
||||
var (
|
||||
_ = qtio422016.Copy
|
||||
_ = qt422016.AcquireByteBuffer
|
||||
)
|
||||
|
||||
//line hello.qtpl:4
|
||||
|
||||
type Hello struct {
|
||||
Vars map[string]interface{}
|
||||
}
|
||||
|
||||
//line hello.qtpl:9
|
||||
|
||||
func (h *Hello) StreamBody(qw422016 *qt422016.Writer) {
|
||||
//line hello.qtpl:9
|
||||
qw422016.N().S(`
|
||||
@@ -43,9 +48,11 @@ func (h *Hello) StreamBody(qw422016 *qt422016.Writer) {
|
||||
</div>
|
||||
`)
|
||||
//line hello.qtpl:14
|
||||
|
||||
}
|
||||
|
||||
//line hello.qtpl:14
|
||||
|
||||
func (h *Hello) WriteBody(qq422016 qtio422016.Writer) {
|
||||
//line hello.qtpl:14
|
||||
qw422016 := qt422016.AcquireWriter(qq422016)
|
||||
@@ -54,9 +61,11 @@ func (h *Hello) WriteBody(qq422016 qtio422016.Writer) {
|
||||
//line hello.qtpl:14
|
||||
qt422016.ReleaseWriter(qw422016)
|
||||
//line hello.qtpl:14
|
||||
|
||||
}
|
||||
|
||||
//line hello.qtpl:14
|
||||
|
||||
func (h *Hello) Body() string {
|
||||
//line hello.qtpl:14
|
||||
qb422016 := qt422016.AcquireByteBuffer()
|
||||
@@ -69,4 +78,5 @@ func (h *Hello) Body() string {
|
||||
//line hello.qtpl:14
|
||||
return qs422016
|
||||
//line hello.qtpl:14
|
||||
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
//
|
||||
|
||||
//line index.qtpl:3
|
||||
|
||||
package templates
|
||||
|
||||
//line index.qtpl:3
|
||||
|
||||
import (
|
||||
qtio422016 "io"
|
||||
|
||||
@@ -15,15 +17,18 @@ import (
|
||||
)
|
||||
|
||||
//line index.qtpl:3
|
||||
|
||||
var (
|
||||
_ = qtio422016.Copy
|
||||
_ = qt422016.AcquireByteBuffer
|
||||
)
|
||||
|
||||
//line index.qtpl:4
|
||||
|
||||
type Index struct{}
|
||||
|
||||
//line index.qtpl:7
|
||||
|
||||
func (i *Index) StreamBody(qw422016 *qt422016.Writer) {
|
||||
//line index.qtpl:7
|
||||
qw422016.N().S(`
|
||||
@@ -33,9 +38,11 @@ func (i *Index) StreamBody(qw422016 *qt422016.Writer) {
|
||||
</div>
|
||||
`)
|
||||
//line index.qtpl:12
|
||||
|
||||
}
|
||||
|
||||
//line index.qtpl:12
|
||||
|
||||
func (i *Index) WriteBody(qq422016 qtio422016.Writer) {
|
||||
//line index.qtpl:12
|
||||
qw422016 := qt422016.AcquireWriter(qq422016)
|
||||
@@ -44,9 +51,11 @@ func (i *Index) WriteBody(qq422016 qtio422016.Writer) {
|
||||
//line index.qtpl:12
|
||||
qt422016.ReleaseWriter(qw422016)
|
||||
//line index.qtpl:12
|
||||
|
||||
}
|
||||
|
||||
//line index.qtpl:12
|
||||
|
||||
func (i *Index) Body() string {
|
||||
//line index.qtpl:12
|
||||
qb422016 := qt422016.AcquireByteBuffer()
|
||||
@@ -59,4 +68,5 @@ func (i *Index) Body() string {
|
||||
//line index.qtpl:12
|
||||
return qs422016
|
||||
//line index.qtpl:12
|
||||
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ import (
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Logger().SetLevel("debug")
|
||||
mvc.Configure(app.Party("/todo"), TodoApp)
|
||||
mvc.Configure(app.Party("/basic"), basicMVC)
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
func TodoApp(app *mvc.Application) {
|
||||
func basicMVC(app *mvc.Application) {
|
||||
// You can use normal middlewares at MVC apps of course.
|
||||
app.Router.Use(func(ctx iris.Context) {
|
||||
ctx.Application().Logger().Infof("Path: %s", ctx.Path())
|
||||
@@ -32,16 +32,16 @@ func TodoApp(app *mvc.Application) {
|
||||
&prefixedLogger{prefix: "DEV"},
|
||||
)
|
||||
|
||||
// GET: http://localhost:8080/todo
|
||||
// GET: http://localhost:8080/todo/custom
|
||||
app.Handle(new(TodoController))
|
||||
// GET: http://localhost:8080/basic
|
||||
// GET: http://localhost:8080/basic/custom
|
||||
app.Handle(new(basicController))
|
||||
|
||||
// All dependencies of the parent *mvc.Application
|
||||
// are cloned to this new child,
|
||||
// thefore it has access to the same session as well.
|
||||
// GET: http://localhost:8080/todo/sub
|
||||
// GET: http://localhost:8080/basic/sub
|
||||
app.Party("/sub").
|
||||
Handle(new(TodoSubController))
|
||||
Handle(new(basicSubController))
|
||||
}
|
||||
|
||||
// If controller's fields (or even its functions) expecting an interface
|
||||
@@ -64,39 +64,39 @@ func (s *prefixedLogger) Log(msg string) {
|
||||
fmt.Printf("%s: %s\n", s.prefix, msg)
|
||||
}
|
||||
|
||||
type TodoController struct {
|
||||
type basicController struct {
|
||||
Logger LoggerService
|
||||
|
||||
Session *sessions.Session
|
||||
}
|
||||
|
||||
func (c *TodoController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
func (c *basicController) BeforeActivation(b mvc.BeforeActivation) {
|
||||
b.Handle("GET", "/custom", "Custom")
|
||||
}
|
||||
|
||||
func (c *TodoController) AfterActivation(a mvc.AfterActivation) {
|
||||
func (c *basicController) AfterActivation(a mvc.AfterActivation) {
|
||||
if a.Singleton() {
|
||||
panic("TodoController should be stateless, a request-scoped, we have a 'Session' which depends on the context.")
|
||||
panic("basicController should be stateless, a request-scoped, we have a 'Session' which depends on the context.")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TodoController) Get() string {
|
||||
func (c *basicController) Get() string {
|
||||
count := c.Session.Increment("count", 1)
|
||||
|
||||
body := fmt.Sprintf("Hello from TodoController\nTotal visits from you: %d", count)
|
||||
body := fmt.Sprintf("Hello from basicController\nTotal visits from you: %d", count)
|
||||
c.Logger.Log(body)
|
||||
return body
|
||||
}
|
||||
|
||||
func (c *TodoController) Custom() string {
|
||||
func (c *basicController) Custom() string {
|
||||
return "custom"
|
||||
}
|
||||
|
||||
type TodoSubController struct {
|
||||
type basicSubController struct {
|
||||
Session *sessions.Session
|
||||
}
|
||||
|
||||
func (c *TodoSubController) Get() string {
|
||||
func (c *basicSubController) Get() string {
|
||||
count, _ := c.Session.GetIntDefault("count", 1)
|
||||
return fmt.Sprintf("Hello from TodoSubController.\nRead-only visits count: %d", count)
|
||||
return fmt.Sprintf("Hello from basicSubController.\nRead-only visits count: %d", count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user