Package web provides quick start framework for web application.

Main features of Hiboot web application

Web MVC (Model-View-Controller).

Auto Configuration, pre-created instance with properties configs for dependency injection.

Dependency injection with the struct tag `inject:""` or the constructor.

Dependency injection in Go

Dependency injection is a concept valid for any programming language. The general concept behind dependency injection is called Inversion of Control. According to this concept a struct should not configure its dependencies statically but should be configured from the outside.

Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable.

A Go struct has a dependency on another struct, if it uses an instance of this struct. We call this a struct dependency. For example, a struct which accesses a user controller has a dependency on user service struct.

Ideally Go struct should be as independent as possible from other Go struct. This increases the possibility of reusing these struct and to be able to test them independently from other struct.

The following example shows a struct which has no hard dependencies.


    This example shows that jwtToken is injected through method Init, once you imported "", jwtToken jwt.Token will be injectable.





    View Source
    const (
    	Any             = "ANY"
    	RequestMapping  = "RequestMapping"
    	ContextPathRoot = "/"
    	UrlSep          = "/"
    View Source
    const (
    	// ViewEnabled is the property for enabling web view
    	ViewEnabled = "web.view.enabled"
    	// ContextPath is the property for setting web view context path
    	ContextPath = "web.view.contextPath"
    	// DefaultPage is the property for setting default page
    	DefaultPage = "web.view.defaultPage"
    	// ResourcePath is the property for setting resource path
    	ResourcePath = "web.view.resourcePath"
    	// Extension is the property for setting extension
    	Extension = "web.view.extension"
    View Source
    const Profile = "web"


    View Source
    var (
    	// ErrControllersNotFound controller not found
    	ErrControllersNotFound = errors.New("[app] controllers not found")
    	// ErrInvalidController invalid controller
    	ErrInvalidController = errors.New("[app] invalid controller")
    View Source
    var (
    	ErrCanNotInterface = errors.New("response can not interface")
    View Source
    var NewTestApplication = RunTestApplication

      NewTestApplication is the alias of RunTestApplication Deprecated, you should use RunTestApplication instead

      View Source
      var RestController = app.Register

        RestController register rest controller to controllers container Deprecated: please use app.Register() instead


        func Handler

        func Handler(h func(context.Context)) iris.Handler

          Handler will convert our handler of func(*Context) to an iris Handler, in order to be compatible with the HTTP API.

          func NewApplication

          func NewApplication(controllers ...interface{}) app.Application

            NewApplication create new web application instance and init it

            func NewContext

            func NewContext(app ctx.Application) context.Context

              NewContext constructor of context.Context

              func RequestBody

              func RequestBody(c context.Context, data interface{}) error

                RequestBody get RequestBody

                func RequestForm

                func RequestForm(c context.Context, data interface{}) error

                  RequestForm get RequestFrom

                  func RequestParams

                  func RequestParams(c context.Context, data interface{}) error

                    RequestParams get RequestParams


                    type Annotations

                    type Annotations struct {
                    	Fields []*annotation.Field
                    	Object interface{}
                    	Value  reflect.Value

                    type Context

                    type Context struct {
                    	// contains filtered or unexported fields

                      Context Create your own custom Context, put any fields you wanna need.

                      func (*Context) Annotations

                      func (c *Context) Annotations() interface{}


                        func (*Context) HTML

                        func (c *Context) HTML(htmlContents string) (int, error)

                          HTML Override any context's method you want... [...]

                          func (*Context) Next

                          func (c *Context) Next()

                            Next The second one important if you will override the Context with an embedded context.Context inside it. Required in order to run the chain of handlers via this "*Context".

                            func (*Context) ResponseBody

                            func (c *Context) ResponseBody(message string, data interface{})

                              ResponseBody set response

                              func (*Context) ResponseError

                              func (c *Context) ResponseError(message string, code int)

                                ResponseError response with error

                                func (*Context) ResponseString

                                func (c *Context) ResponseString(data string)

                                  ResponseString set response

                                  func (*Context) SetAnnotations

                                  func (c *Context) SetAnnotations(ann interface{})


                                    func (*Context) SetURLParam

                                    func (c *Context) SetURLParam(name, value string)


                                      func (*Context) Translate

                                      func (c *Context) Translate(format string, args ...interface{}) string

                                        Translate override base context method Translate to return format if i18n is not enabled

                                        func (*Context) WrapHandler

                                        func (c *Context) WrapHandler(h http.Handler)

                                          WrapHandler is a helper function for wrapping http.Handler

                                          type Controller

                                          type Controller struct {

                                            Controller is the web base controller please use at.RestController instead

                                            type Dispatcher

                                            type Dispatcher struct {
                                            	SystemApp    *system.App
                                            	SystemServer *system.Server
                                            	ContextPathFormat string `value:"${server.context_path_format}" `
                                            	// contains filtered or unexported fields

                                            type HttpMethodSubscriber

                                            type HttpMethodSubscriber interface {
                                            	Subscribe(atController *annotation.Annotations, atMethod *annotation.Annotations)


                                              type TestApplication

                                              type TestApplication interface {
                                              	Initialize() error
                                              	SetProperty(name string, value ...interface{}) TestApplication
                                              	Run(t *testing.T) TestApplication
                                              	Request(method, path string, pathargs ...interface{}) *httpexpect.Request
                                              	Post(path string, pathargs ...interface{}) *httpexpect.Request
                                              	Get(path string, pathargs ...interface{}) *httpexpect.Request
                                              	Put(path string, pathargs ...interface{}) *httpexpect.Request
                                              	Delete(path string, pathargs ...interface{}) *httpexpect.Request
                                              	Patch(path string, pathargs ...interface{}) *httpexpect.Request
                                              	Options(path string, pathargs ...interface{}) *httpexpect.Request

                                                TestApplication the test web application interface for unit test only

                                                func NewTestApp

                                                func NewTestApp(controllers ...interface{}) TestApplication

                                                  NewTestApp returns the new test application

                                                  func RunTestApplication

                                                  func RunTestApplication(t *testing.T, controllers ...interface{}) TestApplication

                                                    RunTestApplication returns the new test application


                                                    Path Synopsis
                                                    Package statik contains static assets.
                                                    Package statik contains static assets.