1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-01 01:07:06 +00:00

reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker

Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-06-07 15:26:06 +03:00
parent 9fdcb4c7fb
commit ed45c77be5
328 changed files with 4262 additions and 41621 deletions

View File

@@ -0,0 +1,9 @@
example.com {
header / Server "Iris"
proxy / example.com:9091 # localhost:9091
}
api.example.com {
header / Server "Iris"
proxy / api.example.com:9092 # localhost:9092
}

24
_examples/caddy/README.md Normal file
View File

@@ -0,0 +1,24 @@
# Caddy loves Iris
The `Caddyfile` shows how you can use caddy to listen on ports 80 & 443 and sit in front of iris webserver(s) that serving on a different port (9091 and 9092 in this case; see Caddyfile).
## Running our two web servers
1. Go to `$GOPATH/src/github.com/kataras/iris/_examples/caddy/server1`
2. Open a terminal window and execute `go run main.go`
3. Go to `$GOPATH/src/github.com/kataras/iris/_examples/caddy/server2`
4. Open a new terminal window and execute `go run main.go`
## Caddy installation
1. Download caddy: https://caddyserver.com/download
2. Extract its contents where the `Caddyfile` is located, the `$GOPATH/src/github.com/kataras/iris/_examples/caddy` in this case
3. Open, read and modify the `Caddyfile` to see by yourself how easy it is to configure the servers
4. Run `caddy` directly or open a terminal window and execute `caddy`
5. Go to `https://example.com` and `https://api.example.com/user/42`
## Notes
Iris has the `app.Run(iris.AutoTLS(":443", "example.com", "mail@example.com"))` which does
the exactly same thing but caddy is a great tool that helps you when you run multiple web servers from one host machine, i.e iris, apache, tomcat.

View File

@@ -0,0 +1,49 @@
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
)
func main() {
app := iris.New()
templates := iris.HTML("./views", ".html").Layout("shared/layout.html")
app.RegisterView(templates)
mvc.New(app).Handle(new(Controller))
// http://localhost:9091
app.Listen(":9091")
}
// Layout contains all the binding properties for the shared/layout.html
type Layout struct {
Title string
}
// Controller is our example controller, request-scoped, each request has its own instance.
type Controller struct {
Layout Layout
}
// BeginRequest is the first method fired when client requests from this Controller's root path.
func (c *Controller) BeginRequest(ctx iris.Context) {
c.Layout.Title = "Home Page"
}
// EndRequest is the last method fired.
// It's here just to complete the BaseController
// in order to be tell iris to call the `BeginRequest` before the main method.
func (c *Controller) EndRequest(ctx iris.Context) {}
// Get handles GET http://localhost:9091
func (c *Controller) Get() mvc.View {
return mvc.View{
Name: "index.html",
Data: iris.Map{
"Layout": c.Layout,
"Message": "Welcome to my website!",
},
}
}

View File

@@ -0,0 +1,3 @@
<div>
{{.Message}}
</div>

View File

@@ -0,0 +1,11 @@
<html>
<head>
<title>{{.Layout.Title}}</title>
</head>
<body>
{{ yield }}
</body>
</html>

View File

@@ -0,0 +1,68 @@
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
)
type postValue func(string) string
func main() {
app := iris.New()
mvc.New(app.Party("/user")).Register(
func(ctx iris.Context) postValue {
return ctx.PostValue
}).Handle(new(UserController))
// GET http://localhost:9092/user
// GET http://localhost:9092/user/42
// POST http://localhost:9092/user
// PUT http://localhost:9092/user/42
// DELETE http://localhost:9092/user/42
// GET http://localhost:9092/user/followers/42
app.Listen(":9092")
}
// UserController is our user example controller.
type UserController struct{}
// Get handles GET /user
func (c *UserController) Get() string {
return "Select all users"
}
// User is our test User model, nothing tremendous here.
type User struct{ ID int64 }
// GetBy handles GET /user/42, equal to .Get("/user/{id:int64}")
func (c *UserController) GetBy(id int64) User {
// Select User by ID == $id.
return User{id}
}
// Post handles POST /user
func (c *UserController) Post(post postValue) string {
username := post("username")
return "Create by user with username: " + username
}
// PutBy handles PUT /user/42
func (c *UserController) PutBy(id int) string {
// Update user by ID == $id
return "User updated"
}
// DeleteBy handles DELETE /user/42
func (c *UserController) DeleteBy(id int) bool {
// Delete user by ID == %id
//
// when boolean then true = iris.StatusOK, false = iris.StatusNotFound
return true
}
// GetFollowersBy handles GET /user/followers/42
func (c *UserController) GetFollowersBy(id int) []User {
// Select all followers by user ID == $id
return []User{ /* ... */ }
}