mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
README
This commit is contained in:
parent
1a163eb78d
commit
8dc4fbdbbf
154
README.md
154
README.md
|
@ -49,164 +49,14 @@ Learn what [others saying about Iris](https://iris-go.com/testimonials/) and **[
|
||||||
## 📖 Learning Iris
|
## 📖 Learning Iris
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# https://github.com/kataras/iris/wiki/Installation
|
|
||||||
$ go get github.com/kataras/iris/v12@master
|
$ go get github.com/kataras/iris/v12@master
|
||||||
$ go get github.com/iris-contrib/middleware@master
|
$ go get github.com/iris-contrib/middleware@master
|
||||||
$ go get github.com/iris-contrib/swagger@master
|
$ go get github.com/iris-contrib/swagger@master
|
||||||
# assume the following code in main.go file
|
|
||||||
$ cat main.go
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```go
|
Iris contains extensive and thorough **[documentation](https://iris-go.com/docs)** making it easy to get started with the framework.
|
||||||
package main
|
|
||||||
|
|
||||||
import "github.com/kataras/iris/v12"
|
<!-- Iris contains extensive and thorough **[wiki](https://github.com/kataras/iris/wiki)** making it easy to get started with the framework. -->
|
||||||
|
|
||||||
func main() {
|
|
||||||
app := iris.New()
|
|
||||||
|
|
||||||
booksAPI := app.Party("/books")
|
|
||||||
{
|
|
||||||
booksAPI.Use(iris.Compression)
|
|
||||||
|
|
||||||
// GET: http://localhost:8080/books
|
|
||||||
booksAPI.Get("/", list)
|
|
||||||
// POST: http://localhost:8080/books
|
|
||||||
booksAPI.Post("/", create)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Listen(":8080")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Book example.
|
|
||||||
type Book struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func list(ctx iris.Context) {
|
|
||||||
books := []Book{
|
|
||||||
{"Mastering Concurrency in Go"},
|
|
||||||
{"Go Design Patterns"},
|
|
||||||
{"Black Hat Go"},
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.JSON(books)
|
|
||||||
// TIP: negotiate the response between server's prioritizes
|
|
||||||
// and client's requirements, instead of ctx.JSON:
|
|
||||||
// ctx.Negotiation().JSON().MsgPack().Protobuf()
|
|
||||||
// ctx.Negotiate(books)
|
|
||||||
}
|
|
||||||
|
|
||||||
func create(ctx iris.Context) {
|
|
||||||
var b Book
|
|
||||||
err := ctx.ReadJSON(&b)
|
|
||||||
// TIP: use ctx.ReadBody(&b) to bind
|
|
||||||
// any type of incoming data instead.
|
|
||||||
if err != nil {
|
|
||||||
ctx.StopWithProblem(iris.StatusBadRequest, iris.NewProblem().
|
|
||||||
Title("Book creation failure").DetailErr(err))
|
|
||||||
// TIP: use ctx.StopWithError(code, err) when only
|
|
||||||
// plain text responses are expected on errors.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Received Book: " + b.Title)
|
|
||||||
|
|
||||||
ctx.StatusCode(iris.StatusCreated)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**MVC** equivalent:
|
|
||||||
|
|
||||||
```go
|
|
||||||
import "github.com/kataras/iris/v12/mvc"
|
|
||||||
```
|
|
||||||
|
|
||||||
```go
|
|
||||||
m := mvc.New(booksAPI)
|
|
||||||
m.Handle(new(BookController))
|
|
||||||
```
|
|
||||||
|
|
||||||
```go
|
|
||||||
type BookController struct {
|
|
||||||
/* dependencies */
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET: http://localhost:8080/books
|
|
||||||
func (c *BookController) Get() []Book {
|
|
||||||
return []Book{
|
|
||||||
{"Mastering Concurrency in Go"},
|
|
||||||
{"Go Design Patterns"},
|
|
||||||
{"Black Hat Go"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST: http://localhost:8080/books
|
|
||||||
func (c *BookController) Post(b Book) int {
|
|
||||||
println("Received Book: " + b.Title)
|
|
||||||
|
|
||||||
return iris.StatusCreated
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Run** your Iris web server:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ go run main.go
|
|
||||||
> Now listening on: http://localhost:8080
|
|
||||||
> Application started. Press CTRL+C to shut down.
|
|
||||||
```
|
|
||||||
|
|
||||||
**List** Books:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ curl --header 'Accept-Encoding:gzip' http://localhost:8080/books
|
|
||||||
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"title": "Mastering Concurrency in Go"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "Go Design Patterns"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "Black Hat Go"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
**Create** a new Book:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ curl -i -X POST \
|
|
||||||
--header 'Content-Encoding:gzip' \
|
|
||||||
--header 'Content-Type:application/json' \
|
|
||||||
--data "{\"title\":\"Writing An Interpreter In Go\"}" \
|
|
||||||
http://localhost:8080/books
|
|
||||||
|
|
||||||
> HTTP/1.1 201 Created
|
|
||||||
```
|
|
||||||
|
|
||||||
That's how an **error** response looks like:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ curl -X POST --data "{\"title\" \"not valid one\"}" \
|
|
||||||
http://localhost:8080/books
|
|
||||||
|
|
||||||
> HTTP/1.1 400 Bad Request
|
|
||||||
|
|
||||||
{
|
|
||||||
"status": 400,
|
|
||||||
"title": "Book creation failure"
|
|
||||||
"detail": "invalid character '\"' after object key",
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
[![run in the browser](https://img.shields.io/badge/Run-in%20the%20Browser-348798.svg?style=for-the-badge&logo=repl.it)](https://bit.ly/2YJeSZe)
|
|
||||||
|
|
||||||
Iris contains extensive and thorough **[wiki](https://github.com/kataras/iris/wiki)** making it easy to get started with the framework.
|
|
||||||
|
|
||||||
<!-- ![](https://media.giphy.com/media/Ur8iqy9FQfmPuyQpgy/giphy.gif) -->
|
<!-- ![](https://media.giphy.com/media/Ur8iqy9FQfmPuyQpgy/giphy.gif) -->
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -99,28 +99,20 @@ func main() {
|
||||||
// Handle the post request from the upload_form.html to the server
|
// Handle the post request from the upload_form.html to the server
|
||||||
app.Post("/upload", iris.LimitRequestBodySize(maxSize+1<<20), func(ctx iris.Context) {
|
app.Post("/upload", iris.LimitRequestBodySize(maxSize+1<<20), func(ctx iris.Context) {
|
||||||
// Get the file from the request.
|
// Get the file from the request.
|
||||||
file, info, err := ctx.FormFile("uploadfile")
|
f, fh, err := ctx.FormFile("uploadfile")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.StatusCode(iris.StatusInternalServerError)
|
ctx.StatusCode(iris.StatusInternalServerError)
|
||||||
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
defer file.Close()
|
_, err = ctx.SaveFormFile(fh, filepath.Join("./uploads", fh.Filename))
|
||||||
fname := info.Filename
|
|
||||||
|
|
||||||
// Create a file with the same name
|
|
||||||
// assuming that you have a folder named 'uploads'
|
|
||||||
out, err := os.OpenFile("./uploads/"+fname,
|
|
||||||
os.O_WRONLY|os.O_CREATE, 0666)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.StatusCode(iris.StatusInternalServerError)
|
ctx.StatusCode(iris.StatusInternalServerError)
|
||||||
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
ctx.HTML("Error while uploading: <b>" + err.Error() + "</b>")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer out.Close()
|
|
||||||
|
|
||||||
io.Copy(out, file)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// start the server at http://localhost:8080 with post limit at 5 MB.
|
// start the server at http://localhost:8080 with post limit at 5 MB.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user