rock

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: MIT Imports: 15 Imported by: 1

README

My custom go web tool

This package is inspired by the following

Documentation

Overview

Codes from https://github.com/gin-gonic/gin/blob/master/debug.go

Index

Constants

This section is empty.

Variables

View Source
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)

DebugPrintRouteFunc indicates debug log output format.

View Source
var DefaultWriter io.Writer = os.Stdout

Functions

func EnsureTemplateName added in v0.2.0

func EnsureTemplateName(s string, v ViewEngine) string

Get filename by viewEngine

func IsDebugging

func IsDebugging() bool

Types

type App

type App struct {
	*RouterGroup
	// contains filtered or unexported fields
}

func New

func New() *App

func (*App) ConfigurationReadOnly added in v0.2.0

func (app *App) ConfigurationReadOnly() *Configuration

ConfigurationReadOnly returns an object which doesn't allow field writing.

func (*App) GetView added in v0.2.0

func (app *App) GetView() View

func (*App) RegisterView added in v0.2.0

func (app *App) RegisterView(viewEngine ViewEngine)

func (*App) Run

func (app *App) Run(args ...string) (err error)

Run defines the method to start a http server

func (*App) ServeHTTP

func (app *App) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*App) View added in v0.2.0

func (app *App) View(writer io.Writer, filename string, bindingData interface{}) error

type BlockEngine added in v0.2.0

type BlockEngine struct{}

type Configuration added in v0.2.0

type Configuration struct {
	// Defaults to "rock.view.engine".
	ViewEngineContextKey string `ini:"view_engine_context_key" json:"viewEngineContextKey,omitempty" yaml:"ViewEngineContextKey" toml:"ViewEngineContextKey"`
	// ViewLayoutContextKey is the context's values key
	// responsible to store and retrieve(string) the current view layout.
	// A middleware can modify its associated value to change
	// the layout that `ctx.View` will use to render a template.
	//
	// Defaults to "rock.view.layout".
	ViewLayoutContextKey string `ini:"view_layout_context_key" json:"viewLayoutContextKey,omitempty" yaml:"ViewLayoutContextKey" toml:"ViewLayoutContextKey"`
	// ViewDataContextKey is the context's values key
	// responsible to store and retrieve(interface{}) the current view binding data.
	// A middleware can modify its associated value to change
	// the template's data on-fly.
	//
	// Defaults to "rock.view.data".
	ViewDataContextKey string `ini:"view_data_context_key" json:"viewDataContextKey,omitempty" yaml:"ViewDataContextKey" toml:"ViewDataContextKey"`
	// FallbackViewContextKey is the context's values key
	// responsible to store the view fallback information.
	//
	// Defaults to "rock.view.fallback".
	FallbackViewContextKey string `` /* 131-byte string literal not displayed */
}

func DefaultConfiguration added in v0.2.0

func DefaultConfiguration() Configuration

func (*Configuration) GetViewDataContextKey added in v0.2.0

func (c *Configuration) GetViewDataContextKey() string

GetViewDataContextKey returns the ViewDataContextKey field.

func (*Configuration) GetViewEngineContextKey added in v0.2.0

func (c *Configuration) GetViewEngineContextKey() string

GetViewDataContextKey returns the ViewDataContextKey field.

type Context

type Context interface {
	Request() *http.Request
	Writer() http.ResponseWriter
	Next()
	// writer
	Write(rawBody []byte) (int, error)
	// response method
	StatusCode() int
	Status(code int)
	SetHeader(key string, value string)
	Fail(code int, err string)
	String(code int, format string, values ...interface{})
	JSON(code int, obj interface{})
	// request method
	Param(key string) interface{}
	Query(key string) string
	QueryInt(key string) int
	// render
	// HTML(code int, name string, data interface{})
	ViewEngine(engine ViewEngine)
	HTML(name string, viewData ...interface{})
	Data() M
	SetData(M)
	Set(key string, value interface{})
	Get(key string) (value interface{}, exists bool)

	GetView() View
}

type Ctx

type Ctx struct {

	// request info
	Path   string
	Method string
	// contains filtered or unexported fields
}

func (*Ctx) Data

func (c *Ctx) Data() M

func (*Ctx) Fail added in v0.2.0

func (c *Ctx) Fail(code int, err string)

func (*Ctx) Get

func (c *Ctx) Get(key string) (value interface{}, exists bool)

func (*Ctx) GetView added in v0.2.0

func (c *Ctx) GetView() View

func (*Ctx) HTML

func (c *Ctx) HTML(name string, viewData ...interface{})

func (*Ctx) JSON

func (c *Ctx) JSON(code int, obj interface{})

func (*Ctx) Next

func (c *Ctx) Next()

func (*Ctx) Param

func (c *Ctx) Param(key string) interface{}

func (*Ctx) Query added in v0.2.0

func (c *Ctx) Query(key string) string

func (*Ctx) QueryInt added in v0.2.0

func (c *Ctx) QueryInt(key string) int

func (*Ctx) Request

func (c *Ctx) Request() *http.Request

func (*Ctx) Set

func (c *Ctx) Set(key string, value interface{})

func (*Ctx) SetData

func (c *Ctx) SetData(data M)

set all data

func (*Ctx) SetHeader added in v0.2.0

func (c *Ctx) SetHeader(key string, value string)

func (*Ctx) Status

func (c *Ctx) Status(code int)

func (*Ctx) StatusCode added in v0.2.0

func (c *Ctx) StatusCode() int

func (*Ctx) String

func (c *Ctx) String(code int, format string, values ...interface{})

func (*Ctx) ViewEngine added in v0.2.0

func (ctx *Ctx) ViewEngine(engine ViewEngine)

func (*Ctx) Write added in v0.2.0

func (ctx *Ctx) Write(rawBody []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply.

If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.

Depending on the HTTP protocol version and the client, calling Write or WriteHeader may prevent future reads on the Request.Body. For HTTP/1.x requests, handlers should read any needed request body data before writing the response. Once the headers have been flushed (due to either an explicit Flusher.Flush call or writing enough data to trigger a flush), the request body may be unavailable. For HTTP/2 requests, the Go HTTP server permits handlers to continue to read the request body while concurrently writing the response. However, such behavior may not be supported by all HTTP/2 clients. Handlers should read before writing if possible to maximize compatibility.

func (*Ctx) Writer added in v0.2.0

func (c *Ctx) Writer() http.ResponseWriter

type Engine added in v0.2.0

type Engine = ViewEngine

type Entry added in v0.2.0

type Entry struct {
	Key      string      `json:"key" msgpack:"key" yaml:"Key" toml:"Value"`
	ValueRaw interface{} `json:"value" msgpack:"value" yaml:"Value" toml:"Value"`
	// contains filtered or unexported fields
}

func (Entry) Value added in v0.2.0

func (e Entry) Value() interface{}

Value returns the value of the entry, respects the immutable.

type H added in v0.2.0

type H Map

type Handler

type Handler = HandlerFunc

type HandlerFunc

type HandlerFunc func(Context)

func Recovery added in v0.2.0

func Recovery() HandlerFunc

type HandlersChain

type HandlersChain []HandlerFunc

func (HandlersChain) Last added in v0.2.0

func (c HandlersChain) Last() HandlerFunc

Last returns the last handler in the chain. ie. the last handler is the main one.

type M

type M Map

type Map added in v0.2.0

type Map map[string]interface{}

type MiddlewareFunc added in v0.2.0

type MiddlewareFunc = HandlerFunc

type PreMiddlewareFunc added in v0.2.0

type PreMiddlewareFunc = HandlerFunc

type Router

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

Mux is a tire base HTTP request router which can be used to dispatch requests to different handler functions.

func NewRouter added in v0.2.0

func NewRouter(opts ...trie.Options) *Router

New returns a Mux instance.

func (*Router) Get added in v0.2.0

func (r *Router) Get(pattern string, handler HandlerFunc)

Get registers a new GET route for a path with matching handler in the Router.

func (*Router) Handle

func (r *Router) Handle(method, pattern string, handler HandlerFunc)

type RouterGroup added in v0.2.0

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

func (*RouterGroup) Get added in v0.2.0

func (group *RouterGroup) Get(pattern string, handler HandlerFunc)

func (*RouterGroup) Group added in v0.2.0

func (group *RouterGroup) Group(prefix string) *RouterGroup

func (*RouterGroup) NoMethod added in v0.2.0

func (group *RouterGroup) NoMethod(handler HandlerFunc)

func (*RouterGroup) NoRoute added in v0.2.0

func (group *RouterGroup) NoRoute(handler HandlerFunc)

func (*RouterGroup) Post added in v0.2.0

func (group *RouterGroup) Post(pattern string, handler HandlerFunc)

func (*RouterGroup) RegisterView added in v0.2.0

func (group *RouterGroup) RegisterView(viewEngine ViewEngine)

func (*RouterGroup) SetRender added in v0.2.0

func (group *RouterGroup) SetRender(render ViewEngine)

func (*RouterGroup) Static added in v0.2.0

func (group *RouterGroup) Static(relativePath string, root string)

serve static files

func (*RouterGroup) Use added in v0.2.0

func (group *RouterGroup) Use(middlewares ...HandlerFunc)

type Store added in v0.2.0

type Store []Entry

func (*Store) Get added in v0.2.0

func (r *Store) Get(key string) interface{}

Get returns the entry's value based on its key. If not found returns nil.

func (*Store) GetDefault added in v0.2.0

func (r *Store) GetDefault(key string, def interface{}) interface{}

GetDefault returns the entry's value based on its key. If not found returns "def". This function checks for immutability as well, the rest don't.

func (*Store) GetEntry added in v0.2.0

func (r *Store) GetEntry(key string) (Entry, bool)

GetEntry returns a pointer to the "Entry" found with the given "key" if nothing found then it returns an empty Entry and false.

func (*Store) Save added in v0.2.0

func (r *Store) Save(key string, value interface{}, immutable bool) (Entry, bool)

func (*Store) Set added in v0.2.0

func (r *Store) Set(key string, value interface{}) (Entry, bool)

type ValueSetter added in v0.2.0

type ValueSetter interface {
	Set(key string, newValue interface{}) (Entry, bool)
}

type View added in v0.2.0

type View struct {
	Engine ViewEngine
}

func (*View) ExecuteWriter added in v0.2.0

func (v *View) ExecuteWriter(w io.Writer, filename string, bindingData interface{}) error

ExecuteWriter calls the correct view Engine's ExecuteWriter func

func (*View) Register added in v0.2.0

func (v *View) Register(e Engine)

Register registers a view engine.

func (*View) Registered added in v0.2.0

func (v *View) Registered() bool

Registered reports whether an engine was registered.

type ViewEngine added in v0.2.0

type ViewEngine interface {
	Name() string
	Ext() string
	ExecuteWriter(writer io.Writer, filename string, bindingData interface{}) error
	SetViewDir(viewDir string)
	GetViewDir() string
}

Directories

Path Synopsis
Modify from https://github.com/teambition/trie-mux
Modify from https://github.com/teambition/trie-mux

Jump to

Keyboard shortcuts

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