web

package
v0.9.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2018 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

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.

Example

This example shows that jwtToken is injected through method Init, once you imported "github.com/hidevopsio/hiboot/pkg/starter/jwt", jwtToken jwt.Token will be injectable.

package main

import (
	"github.com/hidevopsio/hiboot/pkg/app"
	"github.com/hidevopsio/hiboot/pkg/app/web"
	"github.com/hidevopsio/hiboot/pkg/model"
	"github.com/hidevopsio/hiboot/pkg/starter/jwt"
	"time"
)

// This example shows that jwtToken is injected through method Init,
// once you imported "github.com/hidevopsio/hiboot/pkg/starter/jwt",
// jwtToken jwt.Token will be injectable.
func main() {
	// the web application entry
	web.NewApplication().Run()
}

// PATH: /login
type loginController struct {
	web.Controller

	token jwt.Token
}

type userRequest struct {
	// embedded field model.RequestBody mark that userRequest is request body
	model.RequestBody
	Username string `json:"username" validate:"required"`
	Password string `json:"password" validate:"required"`
}

func init() {
	// Register Rest Controller through constructor newLoginController
	app.Register(newLoginController)
}

// newLoginController inject jwtToken through the argument jwtToken jwt.Token on constructor
// the dependency jwtToken is auto configured in jwt starter, see https://github.com/hidevopsio/hiboot/tree/master/pkg/starter/jwt
func newLoginController(token jwt.Token) *loginController {
	return &loginController{
		token: token,
	}
}

// Post /
// The first word of method is the http method POST, the rest is the context mapping
func (c *loginController) Post(request *userRequest) (response model.Response, err error) {
	jwtToken, _ := c.token.Generate(jwt.Map{
		"username": request.Username,
		"password": request.Password,
	}, 30, time.Minute)

	response = new(model.BaseResponse)
	response.SetData(jwtToken)

	return
}
Output:

Index

Examples

Constants

This section is empty.

Variables

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 RestController = app.Register

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

Functions

func NewApplication

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

NewApplication create new web application instance and init it

Types

type Context

type Context struct {
	// Optional Part 1: embed (optional but required if you don't want to override all context's methods)
	// it's the context/context.go#context struct but you don't need to know it.
	context.Context
	ExtendedContext
}

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

func (*Context) Do

func (ctx *Context) Do(handlers context.Handlers)

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

func (*Context) HTML

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

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

func (*Context) Next

func (ctx *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) RequestBody

func (ctx *Context) RequestBody(data interface{}) error

RequestBody get RequestBody

func (*Context) RequestForm

func (ctx *Context) RequestForm(data interface{}) error

RequestForm get RequestFrom

func (*Context) RequestParams

func (ctx *Context) RequestParams(data interface{}) error

RequestParams get RequestParams

func (*Context) ResponseBody

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

ResponseBody set response

func (*Context) ResponseError

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

ResponseError response with error

func (*Context) ResponseString

func (ctx *Context) ResponseString(data string)

ResponseString set response

func (*Context) Translate

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

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

type Controller

type Controller struct {
	at.RestController
	ContextMapping string
	Ctx            *Context
}

Controller is the web base controller

type ExtendedContext

type ExtendedContext interface {
	RequestEx(data interface{}, cb func() error) error
	RequestBody(data interface{}) error
	RequestForm(data interface{}) error
	ResponseBody(message string, data interface{})
	ResponseError(message string, code int)
}

ExtendedContext extended context

type TestApplication

type TestApplication interface {
	app.Application
	RunTestServer(t *testing.T) (expect *httpexpect.Expect, err error)
	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 NewTestApplication

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

NewTestApplication returns the new test application

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL