2017-08-28 11:26:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
2017-08-28 11:26:45 +02:00
|
|
|
)
|
|
|
|
|
2017-12-20 07:33:53 +01:00
|
|
|
type postValue func(string) string
|
2017-08-28 11:26:45 +02:00
|
|
|
|
2017-12-20 07:33:53 +01:00
|
|
|
func main() {
|
2017-08-28 11:26:45 +02:00
|
|
|
app := iris.New()
|
|
|
|
|
2017-12-27 03:15:41 +01:00
|
|
|
mvc.New(app.Party("/user")).Register(
|
2017-12-20 07:33:53 +01:00
|
|
|
func(ctx iris.Context) postValue {
|
|
|
|
return ctx.PostValue
|
2017-12-27 03:15:41 +01:00
|
|
|
}).Handle(new(UserController))
|
2017-08-28 11:26:45 +02:00
|
|
|
|
|
|
|
// 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
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":9092")
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserController is our user example controller.
|
2017-12-20 07:33:53 +01:00
|
|
|
type UserController struct{}
|
2017-08-28 11:26:45 +02:00
|
|
|
|
|
|
|
// Get handles GET /user
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *UserController) Get() string {
|
|
|
|
return "Select all users"
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 07:33:53 +01:00
|
|
|
// User is our test User model, nothing tremendous here.
|
|
|
|
type User struct{ ID int64 }
|
|
|
|
|
2020-06-07 14:26:06 +02:00
|
|
|
// GetBy handles GET /user/42, equal to .Get("/user/{id:int64}")
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *UserController) GetBy(id int64) User {
|
|
|
|
// Select User by ID == $id.
|
|
|
|
return User{id}
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post handles POST /user
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *UserController) Post(post postValue) string {
|
|
|
|
username := post("username")
|
|
|
|
return "Create by user with username: " + username
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBy handles PUT /user/42
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *UserController) PutBy(id int) string {
|
|
|
|
// Update user by ID == $id
|
|
|
|
return "User updated"
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBy handles DELETE /user/42
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *UserController) DeleteBy(id int) bool {
|
|
|
|
// Delete user by ID == %id
|
|
|
|
//
|
|
|
|
// when boolean then true = iris.StatusOK, false = iris.StatusNotFound
|
|
|
|
return true
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFollowersBy handles GET /user/followers/42
|
2017-12-20 07:33:53 +01:00
|
|
|
func (c *UserController) GetFollowersBy(id int) []User {
|
|
|
|
// Select all followers by user ID == $id
|
|
|
|
return []User{ /* ... */ }
|
2017-08-28 11:26:45 +02:00
|
|
|
}
|