mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Add a simple Caddy+Iris tutorial 👍
Former-commit-id: 8761afce72aa35b91c9b5a958f1cafc027aabddd
This commit is contained in:
parent
e2600450f7
commit
674622f814
98
README.md
98
README.md
|
@ -56,11 +56,10 @@ Iris may have reached version 8, but we're not stopping there. We have many feat
|
|||
* [Sessions](_examples/#sessions)
|
||||
* [Websockets](_examples/#websockets)
|
||||
* [Miscellaneous](_examples/#miscellaneous)
|
||||
* [POC: Convert the medium-sized project "Parrot" from native to Iris](https://github.com/iris-contrib/parrot)
|
||||
* [Typescript Automation Tools](typescript/#table-of-contents)
|
||||
* [Tutorial: Online Visitors](_examples/tutorial/online-visitors)
|
||||
* [Tutorial: URL Shortener using BoltDB](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7)
|
||||
* [Tutorial: How to turn your Android Device into a fully featured Web Server (**MUST**)](https://twitter.com/ThePracticalDev/status/892022594031017988)
|
||||
* [POC: Convert the medium-sized project "Parrot" from native to Iris](https://github.com/iris-contrib/parrot)
|
||||
* [Tutorial: Caddy](_examples/tutorial/caddy)
|
||||
* [Middleware](middleware/)
|
||||
* [Dockerize](https://github.com/iris-contrib/cloud-native-go)
|
||||
* [Community & Support](#-community)
|
||||
|
@ -85,18 +84,6 @@ $ go get -u github.com/kataras/iris
|
|||
|
||||
> _iris_ takes advantage of the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature. You get truly reproducible builds, as this method guards against upstream renames and deletes.
|
||||
|
||||
```html
|
||||
<!-- file: ./views/hello.html -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{.message}}</h1>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
```go
|
||||
// file: main.go
|
||||
package main
|
||||
|
@ -129,43 +116,19 @@ func main() {
|
|||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
```
|
||||
<details>
|
||||
<summary>Fan of the MVC Architectural Pattern? Click here</summary>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
app.Controller("/", new(Controller))
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
iris.Controller
|
||||
}
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080
|
||||
func (c *Controller) Get() {
|
||||
c.Data["message"] = "Hello world!"
|
||||
c.Tmpl = "hello.html"
|
||||
}
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/user/42
|
||||
func (c *Controller) GetBy(id int64) {
|
||||
c.Ctx.Writef("User ID: %d", id)
|
||||
}
|
||||
```html
|
||||
<!-- file: ./views/hello.html -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{.message}}</h1>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
```sh
|
||||
$ go run main.go
|
||||
> Now listening on: http://localhost:8080
|
||||
|
@ -204,6 +167,43 @@ func main() {
|
|||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Fan of the MVC Architectural Pattern? Click here</summary>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.RegisterView(iris.HTML("./views", ".html"))
|
||||
|
||||
app.Controller("/", new(Controller))
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
iris.Controller
|
||||
}
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080
|
||||
func (c *Controller) Get() {
|
||||
c.Data["message"] = "Hello world!"
|
||||
c.Tmpl = "hello.html"
|
||||
}
|
||||
|
||||
// Method: GET
|
||||
// Resource: http://localhost:8080/user/42
|
||||
func (c *Controller) GetUserBy(id int64) {
|
||||
c.Ctx.Writef("User ID: %d", id)
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Why a new web framework</summary>
|
||||
|
||||
|
|
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user