mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
Add More Examples & Categorized in Folders & TOC
Former-commit-id: ce4d711a75a4ba08ffab075e6baa88724725885b
This commit is contained in:
23
_examples/examples/beginner/favicon/main.go
Normal file
23
_examples/examples/beginner/favicon/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
// This will serve the ./static/favicons/iris_favicon_32_32.ico to: localhost:8080/favicon.ico
|
||||
app.Favicon("./static/favicons/iris_favicon_32_32.ico")
|
||||
|
||||
// app.Favicon("./static/favicons/iris_favicon_32_32.ico", "/favicon_32_32.ico")
|
||||
// This will serve the ./static/favicons/iris_favicon_32_32.ico to: localhost:8080/favicon_32_32.ico
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
ctx.HTML(iris.StatusOK, `You should see the favicon now at the side of your browser,
|
||||
if not, please refresh or clear the browser's cache.`)
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
16
_examples/examples/beginner/file-server/main.go
Normal file
16
_examples/examples/beginner/file-server/main.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
// first parameter is the request path
|
||||
// second is the operating system directory
|
||||
app.StaticWeb("/static", "./assets")
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
20
_examples/examples/beginner/hello-world/main.go
Normal file
20
_examples/examples/beginner/hello-world/main.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// Adapt the "httprouter", faster,
|
||||
// but it has limits on named path parameters' validation,
|
||||
// you can adapt "gorillamux" if you need regexp path validation!
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
app.HandleFunc("GET", "/", func(ctx *iris.Context) {
|
||||
ctx.Writef("hello world\n")
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
42
_examples/examples/beginner/read-form/main.go
Normal file
42
_examples/examples/beginner/read-form/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// 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 (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/view"
|
||||
)
|
||||
|
||||
type Visitor struct {
|
||||
Username string
|
||||
Mail string
|
||||
Data []string `form:"mydata"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// output startup banner and error logs on os.Stdout
|
||||
app.Adapt(iris.DevLogger())
|
||||
// set the router, you can choose gorillamux too
|
||||
app.Adapt(httprouter.New())
|
||||
// set the view html template engine
|
||||
app.Adapt(view.HTML("./templates", ".html"))
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
if err := ctx.Render("form.html", nil); err != nil {
|
||||
ctx.Log(iris.DevMode, err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Post("/form_action", func(ctx *iris.Context) {
|
||||
visitor := Visitor{}
|
||||
err := ctx.ReadForm(&visitor)
|
||||
if err != nil {
|
||||
ctx.Log(iris.DevMode, "Error when reading form: "+err.Error())
|
||||
}
|
||||
|
||||
ctx.Writef("Visitor: %#v", visitor)
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
19
_examples/examples/beginner/read-form/templates/form.html
Normal file
19
_examples/examples/beginner/read-form/templates/form.html
Normal file
@@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/form_action" method="post">
|
||||
<input type="text" name="Username" /> <br /> <input type="text"
|
||||
name="Mail" /><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>
|
||||
36
_examples/examples/beginner/read-json/main.go
Normal file
36
_examples/examples/beginner/read-json/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
type Company struct {
|
||||
Name string
|
||||
City string
|
||||
Other string
|
||||
}
|
||||
|
||||
func MyHandler(ctx *iris.Context) {
|
||||
c := &Company{}
|
||||
if err := ctx.ReadJSON(c); err != nil {
|
||||
ctx.Log(iris.DevMode, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("Company: %#v\n", c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// output startup banner and error logs on os.Stdout
|
||||
app.Adapt(iris.DevLogger())
|
||||
// set the router, you can choose gorillamux too
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
// use postman or whatever to do a POST request
|
||||
// to the http://localhost:8080 with BODY: JSON PAYLOAD
|
||||
// and Content-Type to application/json
|
||||
app.Post("/", MyHandler)
|
||||
app.Listen(":8080")
|
||||
}
|
||||
69
_examples/examples/beginner/routes-using-gorillamux/main.go
Normal file
69
_examples/examples/beginner/routes-using-gorillamux/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/gorillamux" // import the gorillamux adaptor
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(iris.DevLogger()) // writes both prod and dev logs to the os.Stdout
|
||||
app.Adapt(gorillamux.New()) // uses the gorillamux for routing and reverse routing
|
||||
|
||||
// set a custom 404 handler
|
||||
app.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
|
||||
ctx.HTML(iris.StatusNotFound, "<h1> custom http error page </h1>")
|
||||
})
|
||||
|
||||
app.Get("/healthcheck", h)
|
||||
|
||||
gamesMiddleware := func(ctx *iris.Context) {
|
||||
println(ctx.Method() + ": " + ctx.Path())
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
games := app.Party("/games", gamesMiddleware)
|
||||
{ // braces are optional of course, it's just a style of code
|
||||
games.Get("/{gameID:[0-9]+}/clans", h)
|
||||
games.Get("/{gameID:[0-9]+}/clans/clan/{publicID:[0-9]+}", h)
|
||||
games.Get("/{gameID:[0-9]+}/clans/search", h)
|
||||
|
||||
games.Put("/{gameID:[0-9]+}/players/{publicID:[0-9]+}", h)
|
||||
games.Put("/{gameID:[0-9]+}/clans/clan/{publicID:[0-9]+}", h)
|
||||
|
||||
games.Post("/{gameID:[0-9]+}/clans", h)
|
||||
games.Post("/{gameID:[0-9]+}/players", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{publicID:[0-9]+}/leave", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/application", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/application/:action", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/invitation", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/invitation/:action", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/delete", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/promote", h)
|
||||
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/demote", h)
|
||||
}
|
||||
|
||||
myroute := app.Get("/anything/{anythingparameter:.*}", func(ctx *iris.Context) {
|
||||
s := ctx.Param("anythingparameter")
|
||||
ctx.Writef("The path after /anything is: %s", s)
|
||||
}) // .ChangeName("myroute")
|
||||
|
||||
app.Get("/reverse_myroute", func(ctx *iris.Context) {
|
||||
// reverse routing snippet using templates:
|
||||
// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux)
|
||||
// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter)
|
||||
|
||||
myrouteRequestPath := app.Path(myroute.Name(), "anythingparameter", "something/here")
|
||||
ctx.Writef("Should be '/anything/something/here': %s", myrouteRequestPath)
|
||||
})
|
||||
|
||||
p := app.Party("mysubdomain.")
|
||||
// http://mysubdomain.myhost.com/
|
||||
p.Get("/", h)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func h(ctx *iris.Context) {
|
||||
ctx.HTML(iris.StatusOK, "<h1>Path<h1/>"+ctx.Path())
|
||||
}
|
||||
39
_examples/examples/beginner/routes-using-httprouter/main.go
Normal file
39
_examples/examples/beginner/routes-using-httprouter/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
func hello(ctx *iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(iris.DevLogger())
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
app.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
|
||||
ctx.HTML(iris.StatusNotFound, "<h1>Custom not found handler </h1>")
|
||||
})
|
||||
|
||||
app.Get("/", hello)
|
||||
app.Get("/users/:userid", func(ctx *iris.Context) {
|
||||
ctx.Writef("Hello user with id: %s", ctx.Param("userid"))
|
||||
})
|
||||
|
||||
app.Get("/myfiles/*file", func(ctx *iris.Context) {
|
||||
ctx.HTML(iris.StatusOK, "Hello, the dynamic path after /myfiles is:<br/> <b>"+ctx.Param("file")+"</b>")
|
||||
})
|
||||
|
||||
app.Get("/users/:userid/messages/:messageid", func(ctx *iris.Context) {
|
||||
ctx.HTML(iris.StatusOK, `Message from user with id:<br/> <b>`+ctx.Param("userid")+`</b>,
|
||||
message id: <b>`+ctx.Param("messageid")+`</b>`)
|
||||
})
|
||||
|
||||
// http://127.0.0.1:8080/users/42
|
||||
// http://127.0.0.1:8080/myfiles/mydirectory/myfile.zip
|
||||
// http://127.0.0.1:8080/users/42/messages/1
|
||||
app.Listen(":8080")
|
||||
}
|
||||
BIN
_examples/examples/beginner/send-files/files/first.zip
Normal file
BIN
_examples/examples/beginner/send-files/files/first.zip
Normal file
Binary file not shown.
21
_examples/examples/beginner/send-files/main.go
Normal file
21
_examples/examples/beginner/send-files/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// output startup banner and error logs on os.Stdout
|
||||
app.Adapt(iris.DevLogger())
|
||||
// set the router, you can choose gorillamux too
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
app.Get("/servezip", func(c *iris.Context) {
|
||||
file := "./files/first.zip"
|
||||
c.SendFile(file, "c.zip")
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
37
_examples/examples/beginner/write-json/main.go
Normal file
37
_examples/examples/beginner/write-json/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
)
|
||||
|
||||
// User bind struct
|
||||
type User struct {
|
||||
Firstname string `json:"firstname"`
|
||||
Lastname string `json:"lastname"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
app.Post("/decode", func(ctx *iris.Context) {
|
||||
var user User
|
||||
ctx.ReadJSON(&user)
|
||||
|
||||
ctx.Writef("%s %s is %d years old!", user.Firstname, user.Lastname, user.Age)
|
||||
})
|
||||
|
||||
app.Get("/encode", func(ctx *iris.Context) {
|
||||
peter := User{
|
||||
Firstname: "John",
|
||||
Lastname: "Doe",
|
||||
Age: 25,
|
||||
}
|
||||
|
||||
ctx.JSON(iris.StatusOK, peter)
|
||||
})
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
Reference in New Issue
Block a user