Skip to content

Commit

Permalink
Add the Iris v6 examples to the master branch, the default branch cha…
Browse files Browse the repository at this point in the history
…nged to 5.0.0 until tomorrow
  • Loading branch information
kataras committed Dec 31, 2016
1 parent 135798a commit 53172a9
Show file tree
Hide file tree
Showing 54 changed files with 172 additions and 219 deletions.
2 changes: 1 addition & 1 deletion AIO_examples/basic/backend/api/user.go
Expand Up @@ -8,7 +8,7 @@ type UserAPI struct {

// GET /users
func (u UserAPI) Get() {
u.Write("a list of all users")
u.WriteString("a list of all users")
}

// GET /users/:param1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,6 +8,6 @@ Examples are tested using Windows 7, Ubuntu 16.04 and LiteIDE as editor.

We have three branches

- [Iris current http/2](https://github.com/kataras/iris), master branch.
- [Iris http/2](https://github.com/kataras/iris), master branch -- not yet fully completed.
- [Iris version 5/fasthttp](https://github.com/kataras/iris/tree/5.0.0) examples, click [here](https://github.com/iris-contrib/examples/tree/5.0.0).
- [Iris version 4/fasthttp](https://github.com/kataras/iris/tree/4.0.0) examples, click [here](https://github.com/iris-contrib/examples/tree/4.0.0).
4 changes: 2 additions & 2 deletions api_handler_1/main.go
Expand Up @@ -12,13 +12,13 @@ type UserAPI struct {

// GET /users
func (u UserAPI) Get() {
u.Write("Get from /users")
u.WriteString("Get from /users")
// u.JSON(iris.StatusOK,myDb.AllUsers())
}

// GET /users/:param1 which its value passed to the id argument
func (u UserAPI) GetBy(id string) { // id equals to u.Param("param1")
u.Write("Get from /users/%s", id)
u.Writef("Get from /users/%s", id)
// u.JSON(iris.StatusOK, myDb.GetUserById(id))

}
Expand Down
5 changes: 2 additions & 3 deletions api_handler_2/main.go
Expand Up @@ -13,15 +13,14 @@ type UserAPI struct {

// GET /users
func (u UserAPI) Get() {
u.Write("Get from /users")
u.WriteString("Get from /users")
// u.JSON(iris.StatusOK,myDb.AllUsers())
}

// GET /:param1 which its value passed to the id argument
func (u UserAPI) GetBy(id string) { // id equals to u.Param("param1")
u.Write("Get from /users/%s", id)
u.Writef("Get from /users/%s", id)
// u.JSON(iris.StatusOK, myDb.GetUserById(id))

}

// PUT /users
Expand Down
4 changes: 2 additions & 2 deletions api_handler_3/main.go
Expand Up @@ -12,13 +12,13 @@ type UserAPI struct {

// GET /users
func (u UserAPI) Get() {
u.Write("Get from /users")
u.WriteString("Get from /users")
// u.JSON(iris.StatusOK,myDb.AllUsers())
}

// GET /users/:param1 which its value passed to the id argument
func (u UserAPI) GetBy(id string) { // id equals to u.Param("param1")
u.Write("Get from /users/%s", id)
u.Writef("Get from /users/%s", id)
// u.JSON(iris.StatusOK, myDb.GetUserById(id))

}
Expand Down
2 changes: 1 addition & 1 deletion bind_form/main.go
Expand Up @@ -29,7 +29,7 @@ func main() {
fmt.Println("Error when reading form: " + err.Error())
}
fmt.Printf("\n Visitor: %#v", visitor)
ctx.Write("%#v", visitor)
ctx.Writef("%#v", visitor)
})

iris.Listen(":8080")
Expand Down
2 changes: 1 addition & 1 deletion bind_form_django/main.go
Expand Up @@ -30,7 +30,7 @@ func main() {
fmt.Println("Error when reading form: " + err.Error())
}
fmt.Printf("\n Visitor: %#v", visitor)
ctx.Write("%#v", visitor)
ctx.Writef("%#v", visitor)
})

iris.Listen(":8080")
Expand Down
2 changes: 1 addition & 1 deletion bind_json/main.go
Expand Up @@ -18,7 +18,7 @@ func MyHandler(ctx *iris.Context) {
panic(err.Error())
} else {
fmt.Printf("Company: %#v\n", c)
ctx.Write("Company: %#v\n", c)
ctx.Writef("Company: %#v\n", c)
}
}

Expand Down
37 changes: 0 additions & 37 deletions custom_fasthttp_router/main.go

This file was deleted.

6 changes: 3 additions & 3 deletions custom_handler/main.go
Expand Up @@ -17,9 +17,9 @@ type MyHandler struct {
func (m *MyHandler) Serve(ctx *iris.Context) {
data := &m.data
data.UserAgent = ctx.RequestHeader("User-Agent")
ctx.Write("Path: %s", ctx.PathString())
ctx.Write("\nUser agent: %s", data.UserAgent)
ctx.Write("\nData always same: data.Sysname: %s and data.Version: %d", data.Sysname, data.Version)
ctx.Writef("Path: %s", ctx.Path())
ctx.Writef("\nUser agent: %s", data.UserAgent)
ctx.Writef("\nData always same: data.Sysname: %s and data.Version: %d", data.Sysname, data.Version)
}

func main() {
Expand Down
4 changes: 2 additions & 2 deletions custom_net_listener/main.go
Expand Up @@ -8,11 +8,11 @@ import (

func main() {
iris.Get("/", func(ctx *iris.Context) {
ctx.Write("Hello from the server")
ctx.Writef("Hello from the server")
})

iris.Get("/mypath", func(ctx *iris.Context) {
ctx.Write("Hello from the server on path /mypath")
ctx.Writef("Hello from %s", ctx.Path())
})

// create our custom listener
Expand Down
27 changes: 27 additions & 0 deletions custom_nethttp_router/main.go
@@ -0,0 +1,27 @@
package main

import (
"github.com/kataras/iris"
"net/http"
)

func main() {

api := iris.New()

api.Router = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// optional: get the context to get its helper functions
ctx := api.AcquireCtx(w, r)
if ctx.Path() == "/hello" {
ctx.WriteString("hello world")
} else {
ctx.EmitError(http.StatusNotFound)
}

// release the ctx
api.ReleaseCtx(ctx)
})

// Go to http://localhost:8080/hello
api.Listen(":8080")
}
10 changes: 5 additions & 5 deletions custom_fasthttp_server/main.go → custom_nethttp_server/main.go
Expand Up @@ -2,17 +2,17 @@ package main

import (
"github.com/kataras/iris"
"github.com/valyala/fasthttp"
"net/http"
)

func main() {
api := iris.New()
api.Get("/", func(ctx *iris.Context) {
ctx.Write("Hello from the server")
ctx.Writef("Hello from the server")
})

api.Get("/mypath", func(ctx *iris.Context) {
ctx.Write("Hello from the server on path /mypath")
ctx.Writef("Hello from %s", ctx.Path())
})
// to use a custom server you have to call .Build after route, sessions, templates, websockets,ssh... before server's listen
api.Build()
Expand All @@ -21,8 +21,8 @@ func main() {
// and api.Plugins.DoPostListen(api) after he ListenAndServe

// create our custom fasthttp server and assign the Handler/Router
fsrv := &fasthttp.Server{Handler: api.Router}
fsrv.ListenAndServe(":8080")
fsrv := &http.Server{Handler: api.Router, Addr: ":8080"}
fsrv.ListenAndServe()

// now if you navigate to http://127.0.0.1:8080/mypath
}
2 changes: 2 additions & 0 deletions favicon/main.go
Expand Up @@ -2,6 +2,8 @@ package main

import "github.com/kataras/iris"

// if your ide cannot find the ./static folder try to build that program and after execute it
// or try to download & run this example via LiteIDE.
func main() {
iris.Favicon("./static/favicons/iris_favicon_32_32.ico")
// This will serve the ./static/favicons/iris_favicon_32_32.ico to: localhost:8080/favicon.ico
Expand Down
8 changes: 2 additions & 6 deletions file_upload_simple/main.go
Expand Up @@ -28,8 +28,8 @@ func main() {
iris.Post("/upload", func(ctx *iris.Context) {

// Get the file from the request
info, err := ctx.FormFile("uploadfile")
file, err := info.Open()
file, info, err := ctx.FormFile("uploadfile")

defer file.Close()
fname := info.Filename

Expand All @@ -45,10 +45,6 @@ func main() {
io.Copy(out, file)

})
// set the max request body size of the server, used to be able to send big files to the server
// 32MB max upload filesize)
// By default request body size is 4MB.
iris.Set(iris.OptionMaxRequestBodySize(32 << 20))

iris.Listen(":8080")

Expand Down
23 changes: 11 additions & 12 deletions flash_messages/main.go
Expand Up @@ -7,29 +7,28 @@ import (
func main() {

iris.Get("/set", func(c *iris.Context) {
c.SetFlash("name", "iris")
c.Write("Message setted, is available for the next request")
c.Session().SetFlash("name", "iris")
c.Writef("Message setted, is available for the next request")
})

iris.Get("/get", func(c *iris.Context) {
name, err := c.GetFlash("name")
if err != nil {
c.Write(err.Error())
name := c.Session().GetFlashString("name")
if name != "" {
c.Writef("Empty name!!")
return
}
c.Write("Hello %s", name)
c.Writef("Hello %s", name)
})

iris.Get("/test", func(c *iris.Context) {

name, err := c.GetFlash("name")
if err != nil {
c.Write(err.Error())
name := c.Session().GetFlashString("name")
if name != "" {
c.Writef("Empty name!!")
return
}

c.Write("Ok you are comming from /set ,the value of the name is %s", name)
c.Write(", and again from the same context: %s", name)
c.Writef("Ok you are comming from /set ,the value of the name is %s", name)
c.Writef(", and again from the same context: %s", name)

})

Expand Down
10 changes: 5 additions & 5 deletions httperrors/main.go
Expand Up @@ -7,15 +7,15 @@ import (
func main() {

iris.OnError(iris.StatusInternalServerError, func(ctx *iris.Context) {
ctx.Write(iris.StatusText(iris.StatusInternalServerError)) // Outputs: Internal Server Error
ctx.SetStatusCode(iris.StatusInternalServerError) // 500
ctx.Writef(iris.StatusText(iris.StatusInternalServerError)) // Outputs: Internal Server Error
ctx.SetStatusCode(iris.StatusInternalServerError) // 500

ctx.Log("http status: 500 happened!\n")
})

iris.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
ctx.Write(iris.StatusText(iris.StatusNotFound)) // Outputs: Not Found
ctx.SetStatusCode(iris.StatusNotFound) // 404
ctx.Writef(iris.StatusText(iris.StatusNotFound)) // Outputs: Not Found
ctx.SetStatusCode(iris.StatusNotFound) // 404

ctx.Log("http status: 404 happened!\n")
})
Expand Down Expand Up @@ -46,7 +46,7 @@ func main() {
})

users.Get("/profile/:id", func(ctx *iris.Context) {
ctx.Write("Hello from Profile with ID: %s", ctx.Param("id"))
ctx.Writef("Hello from Profile with ID: %s", ctx.Param("id"))
})
}

Expand Down
4 changes: 2 additions & 2 deletions letsencrypt/main.go
Expand Up @@ -5,11 +5,11 @@ import "github.com/kataras/iris"

func main() {
iris.Get("/", func(ctx *iris.Context) {
ctx.Write("Hello from SECURE SERVER!")
ctx.Writef("Hello from SECURE SERVER!")
})

iris.Get("/test2", func(ctx *iris.Context) {
ctx.Write("Welcome to secure server from /test2!")
ctx.Writef("Welcome to secure server from /test2!")
})

iris.Get("/redirect", func(ctx *iris.Context) {
Expand Down
4 changes: 2 additions & 2 deletions listentls/main.go
Expand Up @@ -7,11 +7,11 @@ import (
func main() {
host := "127.0.0.1:443"
iris.Get("/", func(ctx *iris.Context) {
ctx.Write("Hello from the SECURE server")
ctx.Writef("Hello from the SECURE server")
})

iris.Get("/mypath", func(ctx *iris.Context) {
ctx.Write("Hello from the SECURE server on path /mypath")
ctx.Writef("Hello from the SECURE server on path /mypath")
})

// start a secondary server (HTTP) on port 80, this is a non-blocking func
Expand Down
2 changes: 1 addition & 1 deletion log_response_custom_middleware/main.go
Expand Up @@ -26,5 +26,5 @@ func responseLogger(c *iris.Context) {
c.Next() // process the request first, we don't want to have delays

date := time.Now().Format("01/02 - 15:04:05")
fmt.Printf("%s\n%s", date, c.Response.String())
fmt.Printf("%s\n%s", date, c.ResponseWriter.Body())
}
4 changes: 2 additions & 2 deletions middleware/main.go
Expand Up @@ -10,7 +10,7 @@ func main() {

// register global middleware, you can pass more than one handler comma separated
iris.UseFunc(func(c *iris.Context) {
fmt.Printf("(1)Global logger: %s\n", c.PathString())
fmt.Printf("(1)Global logger: %s\n", c.Path())
c.Next()
})

Expand All @@ -26,7 +26,7 @@ func main() {
fmt.Println("(2)HOME logger for /home")
c.Next()
}, func(c *iris.Context) {
c.Write("Hello from /home")
c.Writef("Hello from /home")
})

iris.Listen(":8080")
Expand Down

0 comments on commit 53172a9

Please sign in to comment.