mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
Add a simple Caddy+Iris tutorial 👍
Former-commit-id: 8761afce72aa35b91c9b5a958f1cafc027aabddd
This commit is contained in:
9
_examples/tutorial/caddy/Caddyfile
Normal file
9
_examples/tutorial/caddy/Caddyfile
Normal 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/tutorial/caddy/README.md
Normal file
24
_examples/tutorial/caddy/README.md
Normal 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/tutorial/caddy/server1`
|
||||
2. Open a terminal window and execute `go run main.go`
|
||||
3. Go to `$GOPATH/src/github.com/kataras/iris/_examples/tutorial/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/tutorial/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.
|
||||
42
_examples/tutorial/caddy/server1/main.go
Normal file
42
_examples/tutorial/caddy/server1/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
templates := iris.HTML("./views", ".html").Layout("shared/layout.html")
|
||||
app.RegisterView(templates)
|
||||
|
||||
app.Controller("/", new(Controller))
|
||||
|
||||
// http://localhost:9091
|
||||
app.Run(iris.Addr(":9091"))
|
||||
}
|
||||
|
||||
// Layout contains all the binding properties for the shared/layout.html
|
||||
type Layout struct {
|
||||
Title string
|
||||
}
|
||||
|
||||
// Controller is our example controller.
|
||||
type Controller struct {
|
||||
iris.Controller
|
||||
|
||||
Layout Layout `iris:"model"`
|
||||
}
|
||||
|
||||
// BeginRequest is the first method fires when client requests from this Controller's path.
|
||||
func (c *Controller) BeginRequest(ctx iris.Context) {
|
||||
c.Controller.BeginRequest(ctx)
|
||||
|
||||
c.Layout.Title = "Home Page"
|
||||
}
|
||||
|
||||
// Get handles GET http://localhost:9091
|
||||
func (c *Controller) Get() {
|
||||
c.Tmpl = "index.html"
|
||||
c.Data["Message"] = "Welcome to my website!"
|
||||
}
|
||||
3
_examples/tutorial/caddy/server1/views/index.html
Normal file
3
_examples/tutorial/caddy/server1/views/index.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<div>
|
||||
{{.Message}}
|
||||
</div>
|
||||
11
_examples/tutorial/caddy/server1/views/shared/layout.html
Normal file
11
_examples/tutorial/caddy/server1/views/shared/layout.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{.Layout.Title}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{{ yield }}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
56
_examples/tutorial/caddy/server2/main.go
Normal file
56
_examples/tutorial/caddy/server2/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
app := iris.New()
|
||||
|
||||
app.Controller("/user", 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.Run(iris.Addr(":9092"))
|
||||
}
|
||||
|
||||
// UserController is our user example controller.
|
||||
type UserController struct {
|
||||
iris.Controller
|
||||
}
|
||||
|
||||
// Get handles GET /user
|
||||
func (c *UserController) Get() {
|
||||
c.Ctx.Writef("Select all users")
|
||||
}
|
||||
|
||||
// GetBy handles GET /user/42
|
||||
func (c *UserController) GetBy(id int) {
|
||||
c.Ctx.Writef("Select user by ID: %d", id)
|
||||
}
|
||||
|
||||
// Post handles POST /user
|
||||
func (c *UserController) Post() {
|
||||
username := c.Ctx.PostValue("username")
|
||||
c.Ctx.Writef("Create by user with username: %s", username)
|
||||
}
|
||||
|
||||
// PutBy handles PUT /user/42
|
||||
func (c *UserController) PutBy(id int) {
|
||||
c.Ctx.Writef("Update user by ID: %d", id)
|
||||
}
|
||||
|
||||
// DeleteBy handles DELETE /user/42
|
||||
func (c *UserController) DeleteBy(id int) {
|
||||
c.Ctx.Writef("Delete user by ID: %d", id)
|
||||
}
|
||||
|
||||
// GetFollowersBy handles GET /user/followers/42
|
||||
func (c *UserController) GetFollowersBy(id int) {
|
||||
c.Ctx.Writef("Select all followers by user ID: %d", id)
|
||||
}
|
||||
Reference in New Issue
Block a user