chocokacang

package module
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 14 Imported by: 0

README

chocokacang

Lightweight web framework

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultErrorWriter io.Writer = os.Stderr
View Source
var Logger = &Log{}

Functions

This section is empty.

Types

type Context

type Context struct {
	Request  *http.Request
	Response Response

	Params Params
	// contains filtered or unexported fields
}

func (*Context) JSON

func (c *Context) JSON(code int, data any) Render

Render response as JSON

func (*Context) Next

func (c *Context) Next()

func (*Context) Status

func (c *Context) Status(code int)

Status sets the HTTP response code.

type D

type D map[string]any

type Engine

type Engine struct {
	// Router is a http.Handler which can be used to dispatch requests to different
	// handler functions via configurable routes
	Router Router
	Module Module
	// contains filtered or unexported fields
}

Engine is a framework's instance, this contains router, middleware and framework's settings Create and instance of Engine, by using New()

func New

func New() *Engine

Create new engine instance

func (*Engine) AddModule

func (e *Engine) AddModule(loader func(module *Module))

func (*Engine) GetHandler

func (e *Engine) GetHandler() http.Handler

func (*Engine) InitDB

func (e *Engine) InitDB(db func() *gorm.DB)

func (*Engine) Run

func (e *Engine) Run(addr ...string) (error error)

func (*Engine) SetLogLevel

func (e *Engine) SetLogLevel(level LogLevel)

type Handler

type Handler func(*Context) Render

type Log

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

func (*Log) Debug

func (l *Log) Debug(message string, values ...any)

func (*Log) Error

func (l *Log) Error(message string, values ...any)

func (*Log) Info

func (l *Log) Info(message string, values ...any)

func (*Log) Warning

func (l *Log) Warning(message string, values ...any)

type LogLevel

type LogLevel int
const (
	LogLevelInfo LogLevel = iota
	LogLevelWarning
	LogLevelError
	LogLevelDebug
)

type Middleware

type Middleware func(*Context)

type Middlewares

type Middlewares []Middleware

type Module

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

func (*Module) Model

func (m *Module) Model(init func(db *gorm.DB))

func (*Module) Router

func (m *Module) Router(init func(router *Router))

type Param

type Param struct {
	Key   string
	Value string
}

type Params

type Params []Param

func (Params) Get

func (ps Params) Get(name string) (string, bool)

type Render

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

Render interface is to be implemented by JSON, XML, HTML, YAML and so on.

type Response

type Response interface {
	http.ResponseWriter
	WriteContentType([]string)
}

type Router

type Router struct {
	BasePath    string
	Middlewares Middlewares
	// contains filtered or unexported fields
}

func (*Router) Get

func (r *Router) Get(path string, handler Handler)

func (*Router) Group

func (r *Router) Group(relativePath string, middlewares ...Middleware) RouterObject

Group is used to set the prefix of several routers and can also attach middleware to routers in the group.

func (*Router) New

func (r *Router) New(router func(*Router))

Add new router, this function will help to register routes using a function. This will helpful for manage routes for large application containing many routes

func (*Router) Post

func (r *Router) Post(path string, handler Handler)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*Router) Use

func (r *Router) Use(middlewares ...Middleware) RouterObject

Use attaches the middlewares to the router.

type RouterObject

type RouterObject interface {
	Get(path string, handler Handler)
}

Jump to

Keyboard shortcuts

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