dessert

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2021 License: MIT Imports: 9 Imported by: 0

README

A dessert for GIN with fx.

Installation

go get -u github.com/snownd/dessert

Quick start

  1. create a database connection:
type DBConn struct {
  db *sql.DB
}
func NewDBConn(lc fx.Lifecycle) (*DBConn, error){
  // Connect to database
  // Don't forget to close the connection.
  // For example:
  //  lc.Append(fx.Hook{
  // 		OnStop: func(c context.Context) error {
  // 			return db.Close()
  // 		},
  // 	})
}

var DBModule = fx.Provide(NewDBConn)
  1. create service:
type Foo struct {
  // fieds...
}
type FooService struct {
  dbConn *DBConn
}
// NewFooService dbConn is injected by fx
func NewFooService(dbConn *DBConn) *FooService {
  return &FooService{dbConn}
}

func (s FooService) FindFoo(ctx context.Context, id string) (Foo, error) {
  // find implementation
}

var	ServiceModule := fx.Options(
	fx.Provide(NewPrintService),
  // You can register other services here.
)
  1. Controller:

type pathParam struct {
	dessert.IPathParam
	ID string `uri:"id" binding:"required,uuid"`
}

type res struct {
	*dessert.JsonBaseResponse
	Foo Foo `json:"foo"`
}

func NewFooController(s *FooService) dessert.Controller {
  ctr := dessert.NewBaseController("/foo")

	ctr.Get("/:id", func(ctx context.Context, pathID *pathParam) (int, dessert.IResponse, error) {
    foo,err := s.FindFoo(ctx, pathID.ID)
		return 200, &res{dessert.Res, foo}, err
	})
}

var ControllerModule = fx.Options(
  // must wrap controller constructor with NewControllerContainer function.
  dessert.NewControllerContainer(NewFooController),
  // Add other controllers here.
)

  1. App:
func main() {
  app := fx.New(
    DBModule,
    ServiceModule,
    ControllerModule,
    dessert.DefaultServerModule,
  )
  app.Run()
}

For runnable code, see example.

Documentation

Index

Constants

View Source
const (
	GroupController       = "controller"
	GroupGlobalMiddleware = "serverMiddleware"
)

Variables

View Source
var DefaultServerModule = fx.Options(
	DefaultServerOptions,
	fx.Provide(NewServer),
	fx.Invoke(func(s *Server) {}),
)
View Source
var ErrBind = errors.New("Dessert:BindError")

Functions

func NewControllerContainer

func NewControllerContainer(constructor interface{}) fx.Annotated

func NewGlobalMiddlewareContainer

func NewGlobalMiddlewareContainer(constructor interface{}) fx.Annotated

Types

type Context

type Context = gin.Context

type Controller

type Controller struct {
	Path        string
	Middlewares []Middleware
	Handlers    []RequestHandler
}

func NewBaseController

func NewBaseController(path string) Controller

func NewController

func NewController(path string, middlewares []Middleware) Controller

func (*Controller) Delete

func (c *Controller) Delete(path string, fn interface{}) *Controller

func (*Controller) DeleteWithOpt

func (c *Controller) DeleteWithOpt(path string, fn interface{}, opts *RequestHandlerOptions) *Controller

func (*Controller) Get

func (c *Controller) Get(path string, fn interface{}) *Controller

func (*Controller) GetWithOpt

func (c *Controller) GetWithOpt(path string, fn interface{}, opts *RequestHandlerOptions) *Controller

func (*Controller) Head

func (c *Controller) Head(path string, fn interface{}, opts *RequestHandlerOptions)

func (*Controller) Options

func (c *Controller) Options(path string, fn interface{}, opts *RequestHandlerOptions)

func (*Controller) Patch

func (c *Controller) Patch(path string, fn interface{}, opts *RequestHandlerOptions)

func (*Controller) Post

func (c *Controller) Post(path string, fn interface{}) *Controller

func (*Controller) PostWithOpt

func (c *Controller) PostWithOpt(path string, fn interface{}, opts *RequestHandlerOptions) *Controller

func (*Controller) Put

func (c *Controller) Put(path string, fn interface{}) *Controller

func (*Controller) PutWithOpt

func (c *Controller) PutWithOpt(path string, fn interface{}, opts *RequestHandlerOptions) *Controller

func (*Controller) Trace

func (c *Controller) Trace(path string, fn interface{}, opts *RequestHandlerOptions)

type GlobalErrorHandler

type GlobalErrorHandler = func(ctx *Context, err error)

func NewDefaultErrorHandler

func NewDefaultErrorHandler() GlobalErrorHandler

type GlobalMiddleware

type GlobalMiddleware Middleware

func NewRecoveryMiddleware

func NewRecoveryMiddleware() GlobalMiddleware

type GlobalResponseHandler

type GlobalResponseHandler = func(ctx *Context, status int, res IResponse)

func NewDefaultResponseHandler

func NewDefaultResponseHandler() GlobalResponseHandler

type HandlerInterceptor

type HandlerInterceptor = Middleware

type IDTO

type IDTO interface {
	// contains filtered or unexported methods
}

IDTO http query or body

type IHeader

type IHeader interface {
	// contains filtered or unexported methods
}

IHeader http request header

type IPathParam

type IPathParam interface {
	// contains filtered or unexported methods
}

IPathParam http path param tag: name:"id"

type IResponse

type IResponse interface {
	ContentType() string
	Reader() io.ReadCloser
	ContentLength() int64
}

IResponse http response

type JsonBaseResponse

type JsonBaseResponse struct {
}

func (JsonBaseResponse) ContentLength

func (r JsonBaseResponse) ContentLength() int64

func (JsonBaseResponse) ContentType

func (r JsonBaseResponse) ContentType() string

func (JsonBaseResponse) Reader

func (r JsonBaseResponse) Reader() io.ReadCloser

type Middleware

type Middleware = gin.HandlerFunc

type RequestHandler

type RequestHandler struct {
	Fn      RequestHandlerFunc
	Path    string
	Method  RequestMethod
	Options RequestHandlerOptions
}

type RequestHandlerFunc

type RequestHandlerFunc func(*Context)

type RequestHandlerOptions

type RequestHandlerOptions struct {
	HandlerInterceptor []HandlerInterceptor
}

type RequestMethod

type RequestMethod string

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(lc fx.Lifecycle, opts ServerOptions) (*Server, error)

type ServerNetOptions

type ServerNetOptions struct {
	Timeout        time.Duration
	Addr           string
	MaxHeaderBytes int
}

func NewServerNetOptions

func NewServerNetOptions() ServerNetOptions

type ServerOptions

type ServerOptions struct {
	fx.In
	Controllers     []Controller       `group:"controller"`
	Middlewares     []GlobalMiddleware `group:"serverMiddleware"`
	ErrorHandler    GlobalErrorHandler
	ResponseHandler GlobalResponseHandler
	NetOptions      ServerNetOptions
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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