From 674622f8147fb61528c84cb153e7bfc6b23740ec Mon Sep 17 00:00:00 2001 From: kataras Date: Mon, 28 Aug 2017 12:26:45 +0300 Subject: [PATCH] Add a simple Caddy+Iris tutorial :+1: Former-commit-id: 8761afce72aa35b91c9b5a958f1cafc027aabddd --- README.md | 98 +++++++++---------- _examples/tutorial/caddy/Caddyfile | 9 ++ _examples/tutorial/caddy/README.md | 24 +++++ _examples/tutorial/caddy/server1/main.go | 42 ++++++++ .../tutorial/caddy/server1/views/index.html | 3 + .../caddy/server1/views/shared/layout.html | 11 +++ _examples/tutorial/caddy/server2/main.go | 56 +++++++++++ 7 files changed, 194 insertions(+), 49 deletions(-) create mode 100644 _examples/tutorial/caddy/Caddyfile create mode 100644 _examples/tutorial/caddy/README.md create mode 100644 _examples/tutorial/caddy/server1/main.go create mode 100644 _examples/tutorial/caddy/server1/views/index.html create mode 100644 _examples/tutorial/caddy/server1/views/shared/layout.html create mode 100644 _examples/tutorial/caddy/server2/main.go diff --git a/README.md b/README.md index 705875d9..8ab4db69 100644 --- a/README.md +++ b/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 - - - - Hello Page - - -

{{.message}}

- - -``` - ```go // file: main.go package main @@ -129,43 +116,19 @@ func main() { app.Run(iris.Addr(":8080")) } ``` -
-Fan of the MVC Architectural Pattern? Click here -```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 + + + + Hello Page + + +

{{.message}}

+ + ``` -
- ```sh $ go run main.go > Now listening on: http://localhost:8080 @@ -204,6 +167,43 @@ func main() { +
+Fan of the MVC Architectural Pattern? Click here + +```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) +} +``` + +
+
Why a new web framework diff --git a/_examples/tutorial/caddy/Caddyfile b/_examples/tutorial/caddy/Caddyfile new file mode 100644 index 00000000..ecbee979 --- /dev/null +++ b/_examples/tutorial/caddy/Caddyfile @@ -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 +} \ No newline at end of file diff --git a/_examples/tutorial/caddy/README.md b/_examples/tutorial/caddy/README.md new file mode 100644 index 00000000..f3d7cfb6 --- /dev/null +++ b/_examples/tutorial/caddy/README.md @@ -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. \ No newline at end of file diff --git a/_examples/tutorial/caddy/server1/main.go b/_examples/tutorial/caddy/server1/main.go new file mode 100644 index 00000000..be821797 --- /dev/null +++ b/_examples/tutorial/caddy/server1/main.go @@ -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!" +} diff --git a/_examples/tutorial/caddy/server1/views/index.html b/_examples/tutorial/caddy/server1/views/index.html new file mode 100644 index 00000000..3d4a81f6 --- /dev/null +++ b/_examples/tutorial/caddy/server1/views/index.html @@ -0,0 +1,3 @@ +
+ {{.Message}} +
\ No newline at end of file diff --git a/_examples/tutorial/caddy/server1/views/shared/layout.html b/_examples/tutorial/caddy/server1/views/shared/layout.html new file mode 100644 index 00000000..141a75f7 --- /dev/null +++ b/_examples/tutorial/caddy/server1/views/shared/layout.html @@ -0,0 +1,11 @@ + + + + {{.Layout.Title}} + + + + {{ yield }} + + + \ No newline at end of file diff --git a/_examples/tutorial/caddy/server2/main.go b/_examples/tutorial/caddy/server2/main.go new file mode 100644 index 00000000..72d9e2fd --- /dev/null +++ b/_examples/tutorial/caddy/server2/main.go @@ -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) +}