2017-08-18 16:09:18 +02:00
|
|
|
package mvc
|
|
|
|
|
|
|
|
import (
|
2017-08-19 20:54:33 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
"github.com/kataras/iris/context"
|
|
|
|
"github.com/kataras/iris/core/memstore"
|
|
|
|
"github.com/kataras/iris/mvc/activator"
|
|
|
|
)
|
|
|
|
|
2017-10-09 14:26:46 +02:00
|
|
|
// C is the lightweight BaseController type as an alternative of the `Controller` struct type.
|
|
|
|
// It contains only the Name of the controller and the Context, it's the best option
|
|
|
|
// to balance the performance cost reflection uses
|
|
|
|
// if your controller uses the new func output values dispatcher feature;
|
|
|
|
// func(c *ExampleController) Get() string |
|
|
|
|
// (string, string) |
|
|
|
|
// (string, int) |
|
|
|
|
// int |
|
|
|
|
// (int, string |
|
|
|
|
// (string, error) |
|
2017-10-12 02:51:06 +02:00
|
|
|
// bool |
|
|
|
|
// (any, bool) |
|
2017-10-09 14:26:46 +02:00
|
|
|
// error |
|
|
|
|
// (int, error) |
|
|
|
|
// (customStruct, error) |
|
|
|
|
// customStruct |
|
|
|
|
// (customStruct, int) |
|
|
|
|
// (customStruct, string) |
|
|
|
|
// Result or (Result, error)
|
|
|
|
// where Get is an HTTP Method func.
|
|
|
|
//
|
|
|
|
// Look `core/router#APIBuilder#Controller` method too.
|
|
|
|
//
|
|
|
|
// It completes the `activator.BaseController` interface.
|
|
|
|
//
|
2017-10-12 02:51:06 +02:00
|
|
|
// Example at: https://github.com/kataras/iris/tree/master/_examples/mvc/overview/web/controllers.
|
2017-10-09 14:26:46 +02:00
|
|
|
// Example usage at: https://github.com/kataras/iris/blob/master/mvc/method_result_test.go#L17.
|
|
|
|
type C struct {
|
|
|
|
// The Name of the `C` controller.
|
|
|
|
Name string
|
|
|
|
// The current context.Context.
|
|
|
|
//
|
|
|
|
// we have to name it for two reasons:
|
|
|
|
// 1: can't ignore these via reflection, it doesn't give an option to
|
|
|
|
// see if the functions is derived from another type.
|
|
|
|
// 2: end-developer may want to use some method functions
|
|
|
|
// or any fields that could be conflict with the context's.
|
|
|
|
Ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ activator.BaseController = &C{}
|
|
|
|
|
|
|
|
// SetName sets the controller's full name.
|
|
|
|
// It's called internally.
|
|
|
|
func (c *C) SetName(name string) { c.Name = name }
|
|
|
|
|
|
|
|
// BeginRequest starts the request by initializing the `Context` field.
|
|
|
|
func (c *C) BeginRequest(ctx context.Context) { c.Ctx = ctx }
|
|
|
|
|
|
|
|
// EndRequest does nothing, is here to complete the `BaseController` interface.
|
|
|
|
func (c *C) EndRequest(ctx context.Context) {}
|
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// Controller is the base controller for the high level controllers instances.
|
|
|
|
//
|
|
|
|
// This base controller is used as an alternative way of building
|
|
|
|
// APIs, the controller can register all type of http methods.
|
|
|
|
//
|
|
|
|
// Keep note that controllers are bit slow
|
|
|
|
// because of the reflection use however it's as fast as possible because
|
|
|
|
// it does preparation before the serve-time handler but still
|
|
|
|
// remains slower than the low-level handlers
|
|
|
|
// such as `Handle, Get, Post, Put, Delete, Connect, Head, Trace, Patch`.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// All fields that are tagged with iris:"persistence"` or binded
|
|
|
|
// are being persistence and kept the same between the different requests.
|
|
|
|
//
|
|
|
|
// An Example Controller can be:
|
|
|
|
//
|
|
|
|
// type IndexController struct {
|
|
|
|
// Controller
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func (c *IndexController) Get() {
|
|
|
|
// c.Tmpl = "index.html"
|
|
|
|
// c.Data["title"] = "Index page"
|
|
|
|
// c.Data["message"] = "Hello world!"
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Usage: app.Controller("/", new(IndexController))
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Another example with bind:
|
|
|
|
//
|
|
|
|
// type UserController struct {
|
|
|
|
// mvc.Controller
|
|
|
|
//
|
|
|
|
// DB *DB
|
|
|
|
// CreatedAt time.Time
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Get serves using the User controller when HTTP Method is "GET".
|
|
|
|
// func (c *UserController) Get() {
|
|
|
|
// c.Tmpl = "user/index.html"
|
|
|
|
// c.Data["title"] = "User Page"
|
|
|
|
// c.Data["username"] = "kataras " + c.Params.Get("userid")
|
|
|
|
// c.Data["connstring"] = c.DB.Connstring
|
|
|
|
// c.Data["uptime"] = time.Now().Sub(c.CreatedAt).Seconds()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Usage: app.Controller("/user/{id:int}", new(UserController), db, time.Now())
|
2017-08-22 12:00:24 +02:00
|
|
|
// Note: Binded values of context.Handler type are being recognised as middlewares by the router.
|
2017-08-18 16:09:18 +02:00
|
|
|
//
|
|
|
|
// Look `core/router/APIBuilder#Controller` method too.
|
2017-10-09 14:26:46 +02:00
|
|
|
//
|
|
|
|
// It completes the `activator.BaseController` interface.
|
2017-08-18 16:09:18 +02:00
|
|
|
type Controller struct {
|
2017-08-19 20:54:33 +02:00
|
|
|
// Name contains the current controller's full name.
|
2017-09-02 13:32:14 +02:00
|
|
|
//
|
|
|
|
// doesn't change on different paths.
|
2017-08-19 20:54:33 +02:00
|
|
|
Name string
|
2017-08-23 15:46:55 +02:00
|
|
|
|
2017-08-19 20:54:33 +02:00
|
|
|
// contains the `Name` as different words, all lowercase,
|
|
|
|
// without the "Controller" suffix if exists.
|
|
|
|
// we need this as field because the activator
|
|
|
|
// we will not try to parse these if not needed
|
|
|
|
// it's up to the end-developer to call `RelPath()` or `RelTmpl()`
|
|
|
|
// which will result to fill them.
|
2017-09-02 13:32:14 +02:00
|
|
|
//
|
|
|
|
// doesn't change on different paths.
|
2017-08-19 20:54:33 +02:00
|
|
|
nameAsWords []string
|
|
|
|
|
|
|
|
// relPath the "as assume" relative request path.
|
|
|
|
//
|
|
|
|
// If UserController and request path is "/user/messages" then it's "/messages"
|
|
|
|
// if UserPostController and request path is "/user/post" then it's "/"
|
|
|
|
// if UserProfile and request path is "/user/profile/likes" then it's "/likes"
|
2017-09-02 13:32:14 +02:00
|
|
|
//
|
|
|
|
// doesn't change on different paths.
|
2017-08-19 20:54:33 +02:00
|
|
|
relPath string
|
|
|
|
|
|
|
|
// request path and its parameters, read-write.
|
2017-09-02 13:32:14 +02:00
|
|
|
// Path is the current request path, if changed then it redirects.
|
2017-08-19 20:54:33 +02:00
|
|
|
Path string
|
|
|
|
// Params are the request path's parameters, i.e
|
|
|
|
// for route like "/user/{id}" and request to "/user/42"
|
|
|
|
// it contains the "id" = 42.
|
2017-08-18 16:09:18 +02:00
|
|
|
Params *context.RequestParams
|
|
|
|
|
|
|
|
// some info read and write,
|
|
|
|
// can be already set-ed by previous handlers as well.
|
|
|
|
Status int
|
|
|
|
Values *memstore.Store
|
|
|
|
|
2017-08-19 20:54:33 +02:00
|
|
|
// relTmpl the "as assume" relative path to the view root folder.
|
|
|
|
//
|
|
|
|
// If UserController then it's "user/"
|
|
|
|
// if UserPostController then it's "user/post/"
|
|
|
|
// if UserProfile then it's "user/profile/".
|
2017-09-02 13:32:14 +02:00
|
|
|
//
|
|
|
|
// doesn't change on different paths.
|
2017-08-19 20:54:33 +02:00
|
|
|
relTmpl string
|
2017-09-02 13:32:14 +02:00
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// view read and write,
|
|
|
|
// can be already set-ed by previous handlers as well.
|
|
|
|
Layout string
|
|
|
|
Tmpl string
|
|
|
|
Data map[string]interface{}
|
|
|
|
|
2017-09-02 13:32:14 +02:00
|
|
|
ContentType string
|
2017-09-02 16:44:35 +02:00
|
|
|
Text string // response as string
|
2017-09-02 13:32:14 +02:00
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// give access to the request context itself.
|
|
|
|
Ctx context.Context
|
|
|
|
}
|
|
|
|
|
2017-10-09 14:26:46 +02:00
|
|
|
var _ activator.BaseController = &Controller{}
|
|
|
|
|
|
|
|
var ctrlSuffix = reflect.TypeOf(Controller{}).Name()
|
|
|
|
|
2017-08-19 20:54:33 +02:00
|
|
|
// SetName sets the controller's full name.
|
|
|
|
// It's called internally.
|
|
|
|
func (c *Controller) SetName(name string) {
|
|
|
|
c.Name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) getNameWords() []string {
|
|
|
|
if len(c.nameAsWords) == 0 {
|
|
|
|
c.nameAsWords = findCtrlWords(c.Name)
|
|
|
|
}
|
|
|
|
return c.nameAsWords
|
|
|
|
}
|
|
|
|
|
2017-08-24 14:40:06 +02:00
|
|
|
// Route returns the current request controller's context read-only access route.
|
|
|
|
func (c *Controller) Route() context.RouteReadOnly {
|
2017-09-02 13:32:14 +02:00
|
|
|
return c.Ctx.GetCurrentRoute()
|
2017-08-24 14:40:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 20:54:33 +02:00
|
|
|
const slashStr = "/"
|
|
|
|
|
|
|
|
// RelPath tries to return the controller's name
|
|
|
|
// without the "Controller" prefix, all lowercase
|
|
|
|
// prefixed with slash and splited by slash appended
|
|
|
|
// with the rest of the request path.
|
|
|
|
// For example:
|
|
|
|
// If UserController and request path is "/user/messages" then it's "/messages"
|
|
|
|
// if UserPostController and request path is "/user/post" then it's "/"
|
|
|
|
// if UserProfile and request path is "/user/profile/likes" then it's "/likes"
|
|
|
|
//
|
|
|
|
// It's useful for things like path checking and redirect.
|
|
|
|
func (c *Controller) RelPath() string {
|
|
|
|
if c.relPath == "" {
|
|
|
|
w := c.getNameWords()
|
|
|
|
rel := strings.Join(w, slashStr)
|
|
|
|
|
|
|
|
reqPath := c.Ctx.Path()
|
|
|
|
if len(reqPath) == 0 {
|
|
|
|
// it never come here
|
2017-08-22 12:00:24 +02:00
|
|
|
// but to protect ourselves just return an empty slash.
|
2017-08-19 20:54:33 +02:00
|
|
|
return slashStr
|
|
|
|
}
|
|
|
|
// [1:]to ellimuate the prefixes like "//"
|
|
|
|
// request path has always "/"
|
2017-08-24 14:40:06 +02:00
|
|
|
rel = strings.Replace(reqPath[1:], rel, "", 1)
|
2017-08-19 20:54:33 +02:00
|
|
|
if rel == "" {
|
|
|
|
rel = slashStr
|
|
|
|
}
|
|
|
|
c.relPath = rel
|
2017-08-24 14:40:06 +02:00
|
|
|
// this will return any dynamic path after the static one
|
|
|
|
// or a a slash "/":
|
|
|
|
//
|
|
|
|
// reqPath := c.Ctx.Path()
|
|
|
|
// if len(reqPath) == 0 {
|
|
|
|
// // it never come here
|
|
|
|
// // but to protect ourselves just return an empty slash.
|
|
|
|
// return slashStr
|
|
|
|
// }
|
|
|
|
// var routeVParams []string
|
|
|
|
// c.Params.Visit(func(key string, value string) {
|
|
|
|
// routeVParams = append(routeVParams, value)
|
|
|
|
// })
|
|
|
|
|
|
|
|
// rel := c.Route().StaticPath()
|
|
|
|
// println(rel)
|
|
|
|
// // [1:]to ellimuate the prefixes like "//"
|
|
|
|
// // request path has always "/"
|
|
|
|
// rel = strings.Replace(reqPath, rel[1:], "", 1)
|
|
|
|
// println(rel)
|
|
|
|
// if rel == "" {
|
|
|
|
// rel = slashStr
|
|
|
|
// }
|
|
|
|
// c.relPath = rel
|
2017-08-19 20:54:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.relPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelTmpl tries to return the controller's name
|
|
|
|
// without the "Controller" prefix, all lowercase
|
|
|
|
// splited by slash and suffixed by slash.
|
|
|
|
// For example:
|
|
|
|
// If UserController then it's "user/"
|
|
|
|
// if UserPostController then it's "user/post/"
|
|
|
|
// if UserProfile then it's "user/profile/".
|
|
|
|
//
|
|
|
|
// It's useful to locate templates if the controller and views path have aligned names.
|
|
|
|
func (c *Controller) RelTmpl() string {
|
|
|
|
if c.relTmpl == "" {
|
|
|
|
c.relTmpl = strings.Join(c.getNameWords(), slashStr) + slashStr
|
|
|
|
}
|
|
|
|
return c.relTmpl
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:32:14 +02:00
|
|
|
// Write writes to the client via the context's ResponseWriter.
|
|
|
|
// Controller completes the `io.Writer` interface for the shake of ease.
|
|
|
|
func (c *Controller) Write(contents []byte) (int, error) {
|
|
|
|
c.tryWriteHeaders()
|
|
|
|
return c.Ctx.ResponseWriter().Write(contents)
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:23:03 +02:00
|
|
|
// Writef formats according to a format specifier and writes to the response.
|
|
|
|
func (c *Controller) Writef(format string, a ...interface{}) (int, error) {
|
|
|
|
c.tryWriteHeaders()
|
|
|
|
return c.Ctx.ResponseWriter().Writef(format, a...)
|
|
|
|
}
|
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// BeginRequest starts the main controller
|
|
|
|
// it initialize the Ctx and other fields.
|
|
|
|
//
|
2017-08-19 20:54:33 +02:00
|
|
|
// It's called internally.
|
2017-08-18 16:09:18 +02:00
|
|
|
// End-Developer can ovverride it but it still MUST be called.
|
|
|
|
func (c *Controller) BeginRequest(ctx context.Context) {
|
|
|
|
// path and path params
|
|
|
|
c.Path = ctx.Path()
|
|
|
|
c.Params = ctx.Params()
|
|
|
|
// response status code
|
|
|
|
c.Status = ctx.GetStatusCode()
|
2017-10-09 14:26:46 +02:00
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// share values
|
|
|
|
c.Values = ctx.Values()
|
2017-09-02 13:32:14 +02:00
|
|
|
// view data for templates, remember
|
|
|
|
// each controller is a new instance, so
|
|
|
|
// checking for nil and then init those type of fields
|
|
|
|
// have no meaning.
|
2017-08-18 16:09:18 +02:00
|
|
|
c.Data = make(map[string]interface{}, 0)
|
2017-09-02 13:32:14 +02:00
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// context itself
|
|
|
|
c.Ctx = ctx
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:32:14 +02:00
|
|
|
func (c *Controller) tryWriteHeaders() {
|
2017-10-09 14:26:46 +02:00
|
|
|
if c.Status > 0 && c.Status != c.Ctx.GetStatusCode() {
|
|
|
|
c.Ctx.StatusCode(c.Status)
|
2017-09-02 13:32:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 14:26:46 +02:00
|
|
|
if c.ContentType != "" {
|
|
|
|
c.Ctx.ContentType(c.ContentType)
|
2017-09-02 13:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
// EndRequest is the final method which will be executed
|
|
|
|
// before response sent.
|
|
|
|
//
|
|
|
|
// It checks for the fields and calls the necessary context's
|
|
|
|
// methods to modify the response to the client.
|
|
|
|
//
|
2017-08-19 20:54:33 +02:00
|
|
|
// It's called internally.
|
2017-08-18 16:09:18 +02:00
|
|
|
// End-Developer can ovveride it but still should be called at the end.
|
|
|
|
func (c *Controller) EndRequest(ctx context.Context) {
|
2017-10-09 14:26:46 +02:00
|
|
|
if ctx.ResponseWriter().Written() >= 0 { // status code only (0) or actual body written(>0)
|
2017-09-02 13:32:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
if path := c.Path; path != "" && path != ctx.Path() {
|
2017-09-02 13:32:14 +02:00
|
|
|
// then redirect and exit.
|
|
|
|
ctx.Redirect(path, c.Status)
|
2017-08-18 16:09:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-02 13:32:14 +02:00
|
|
|
c.tryWriteHeaders()
|
|
|
|
if response := c.Text; response != "" {
|
|
|
|
ctx.WriteString(response)
|
|
|
|
return // exit here
|
2017-08-18 16:09:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if view := c.Tmpl; view != "" {
|
|
|
|
if layout := c.Layout; layout != "" {
|
|
|
|
ctx.ViewLayout(layout)
|
|
|
|
}
|
2017-10-09 14:26:46 +02:00
|
|
|
if len(c.Data) > 0 {
|
2017-10-10 15:58:14 +02:00
|
|
|
dataKey := ctx.Application().ConfigurationReadOnly().GetViewDataContextKey()
|
|
|
|
// In order to respect any c.Ctx.ViewData that may called manually before;
|
|
|
|
if ctx.Values().Get(dataKey) == nil {
|
|
|
|
// if no c.Ctx.ViewData then it's empty do a
|
|
|
|
// pure set, it's faster.
|
|
|
|
ctx.Values().Set(dataKey, c.Data)
|
|
|
|
} else {
|
|
|
|
// else do a range loop and set the data one by one.
|
|
|
|
for k, v := range c.Data {
|
|
|
|
ctx.ViewData(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
}
|
2017-09-02 13:32:14 +02:00
|
|
|
|
2017-08-18 16:09:18 +02:00
|
|
|
ctx.View(view)
|
|
|
|
}
|
|
|
|
}
|