1
0
mirror of https://github.com/kataras/iris.git synced 2026-02-12 13:45:56 +00:00

Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded

Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
kataras
2017-07-10 18:32:42 +03:00
parent 2d4c2779a7
commit 9f85b74fc9
344 changed files with 4842 additions and 5174 deletions

View File

@@ -0,0 +1 @@
The `context.Request()` returns the same *http.Request you already know, the examples show some places where the Context uses this object. Besides that you can use it as you did before iris.

View File

@@ -0,0 +1,41 @@
// package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/view"
)
type Visitor struct {
Username string
Mail string
Data []string `form:"mydata"`
}
func main() {
app := iris.New()
// set the view html template engine
app.RegisterView(view.HTML("./templates", ".html").Reload(true))
app.Get("/", func(ctx context.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) {
visitor := Visitor{}
err := ctx.ReadForm(&visitor)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString(err.Error())
}
ctx.Writef("Visitor: %#v", visitor)
})
app.Run(iris.Addr(":8080"))
}

View File

@@ -0,0 +1,22 @@
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="/form_action" method="post">
Username: <input type="text" name="Username" /> <br />
Mail: <input type="text" name="Mail" /> <br />
Select one or more: <br/>
<select multiple="multiple" name="mydata">
<option value='one'>One</option>
<option value='two'>Two</option>
<option value='three'>Three</option>
<option value='four'>Four</option>
</select>
<hr />
<input type="submit" value="Send data" />
</form>
</body>
</html>

View File

@@ -0,0 +1,44 @@
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
type Company struct {
Name string
City string
Other string
}
func MyHandler(ctx context.Context) {
c := &Company{}
if err := ctx.ReadJSON(c); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}
ctx.Writef("Received: %#v\n", c)
}
func main() {
app := iris.New()
app.Post("/", MyHandler)
// use Postman or whatever to do a POST request
// to the http://localhost:8080 with RAW BODY:
/*
{
"Name": "iris-Go",
"City": "New York",
"Other": "Something here"
}
*/
// and Content-Type to application/json
//
// The response should be:
// Received: &main.Company{Name:"iris-Go", City:"New York", Other:"Something here"}
app.Run(iris.Addr(":8080"))
}

View File

@@ -0,0 +1,52 @@
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/middleware/logger"
)
func main() {
app := iris.New()
customLogger := logger.New(logger.Config{
// Status displays status code
Status: true,
// IP displays request's remote address
IP: true,
// Method displays the http method
Method: true,
// Path displays the request path
Path: true,
})
app.Use(customLogger)
app.Get("/", func(ctx context.Context) {
ctx.Writef("hello")
})
app.Get("/1", func(ctx context.Context) {
ctx.Writef("hello")
})
app.Get("/2", func(ctx context.Context) {
ctx.Writef("hello")
})
// log http errors should be done manually
errorLogger := logger.New()
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
errorLogger(ctx)
ctx.Writef("My Custom 404 error page ")
})
// http://localhost:8080
// http://localhost:8080/1
// http://localhost:8080/2
// http://lcoalhost:8080/notfoundhere
// see the output on the console.
app.Run(iris.Addr(":8080"))
}

View File

@@ -0,0 +1,70 @@
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
"strconv"
"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"))
// Serve the form.html to the user
app.Get("/upload", func(ctx context.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 like
ctx.ViewData("", token)
ctx.View("upload_form.html")
})
// Handle the post request from the upload_form.html to the server
app.Post("/upload", context.LimitRequestBodySize(10<<20),
func(ctx context.Context) {
// or use ctx.SetMaxRequestBodySize(10 << 20)
//to limit the uploaded file(s) size.
// 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
app.Run(iris.Addr(":8080"))
}

View File

@@ -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>