pithy

package module
v0.0.0-...-f626ee3 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 12 Imported by: 0

README

pithy

Pithy is a very very simple and pithy web framework.It is useful for me.

Documentation

Index

Constants

View Source
const (
	LogLevelDebug = iota
	LogLevelInfo
	LogLevelWarn
	LogLevelError
	LogLevelFatal
)

Variables

View Source
var (
	ErrInvalidServerRouter  = errors.New("Invalid server router")
	ErrInvalidListenAddress = errors.New("Invalid listen address")
)

constant errors

Functions

func Debugf

func Debugf(fmt string, v ...interface{})

func Debugln

func Debugln(v ...interface{})

func Errorf

func Errorf(fmt string, v ...interface{})

func Errorln

func Errorln(v ...interface{})

func Fatalf

func Fatalf(fmt string, v ...interface{})

func Fatalln

func Fatalln(v ...interface{})

func Flush

func Flush()

func Infof

func Infof(fmt string, v ...interface{})

func Infoln

func Infoln(v ...interface{})

func RegisterLogger

func RegisterLogger(logger Logger)

func Warnf

func Warnf(fmt string, v ...interface{})

func Warnln

func Warnln(v ...interface{})

Types

type APIFunc

type APIFunc func(*HTTPContext) *APIResult

type APIResult

type APIResult struct {
	Message    string      `json:"message"`
	ResultCode int         `json:"code"`
	Data       interface{} `json:"data"`
	// For internal use
	StatusCode int    `json:"-"`
	RawBytes   []byte `json:"-"` // If raw is not nil, directly send raw bytes rather than sending json bytes of the result
	// Original error
	Err error `json:"-"`
}

APIError holds the error code and error message for responding to request peer

func NewAPIResult

func NewAPIResult(result, status int, msg string) *APIResult

func NewAPIResultFromResultCode

func NewAPIResultFromResultCode(result int, msg string) *APIResult

func (*APIResult) String

func (r *APIResult) String() string

type APIRouter

type APIRouter interface {
	Path() string
	Method() string
	Handler() APIFunc
	Version() bool
}

APIRouter defines an interface to determine which handler is called by path and method

type HTTPContext

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

func (*HTTPContext) GetFormValueInt64

func (c *HTTPContext) GetFormValueInt64(key string, def int64) int64

func (*HTTPContext) GetFormValueString

func (c *HTTPContext) GetFormValueString(key string) string

func (*HTTPContext) GetFormValueStringTrimBlank

func (c *HTTPContext) GetFormValueStringTrimBlank(key string) string

func (*HTTPContext) GetPathVarInt64

func (c *HTTPContext) GetPathVarInt64(key string, def int64) int64

func (*HTTPContext) GetPathVarString

func (c *HTTPContext) GetPathVarString(key string) string

func (*HTTPContext) GetPathVersion

func (c *HTTPContext) GetPathVersion() string

func (*HTTPContext) GetRequest

func (c *HTTPContext) GetRequest() *http.Request

func (*HTTPContext) GetResponseWriter

func (c *HTTPContext) GetResponseWriter() http.ResponseWriter

func (*HTTPContext) GetUserData

func (c *HTTPContext) GetUserData(key string) interface{}

func (*HTTPContext) ReadBody

func (c *HTTPContext) ReadBody() ([]byte, error)

func (*HTTPContext) SetRequest

func (c *HTTPContext) SetRequest(r *http.Request)

func (*HTTPContext) SetUserData

func (c *HTTPContext) SetUserData(key string, value interface{})

func (*HTTPContext) UnmarshalJSONFromBody

func (c *HTTPContext) UnmarshalJSONFromBody(i interface{}) error

type HTTPServer

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

func NewHTTPServer

func NewHTTPServer(address string) *HTTPServer

func (*HTTPServer) AppendRouters

func (s *HTTPServer) AppendRouters(rts ...APIRouter)

AppendRouters create mux and update handler of server

func (*HTTPServer) EnablePprof

func (s *HTTPServer) EnablePprof()

func (*HTTPServer) GetListener

func (s *HTTPServer) GetListener() net.Listener

GetListener get the listener of the server

func (*HTTPServer) Serve

func (s *HTTPServer) Serve() error

Serve with the speicified address

func (*HTTPServer) SetNotFoundHandler

func (s *HTTPServer) SetNotFoundHandler(h APIFunc)

SetNotFoundHandler -

func (*HTTPServer) Stop

func (s *HTTPServer) Stop()

Stop stops the server

type Logger

type Logger interface {
	Outputln(lv int, v ...interface{})
	Outputf(lv int, fmt string, v ...interface{})
	Flush()
}

type SwappableHandler

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

SwappableHandler is a http.Handler thats allows you to swap mux during working

func (*SwappableHandler) ServeHTTP

func (h *SwappableHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implement the http.Handler interface

func (*SwappableHandler) Swap

func (h *SwappableHandler) Swap(r *mux.Router)

Swap swaps the router of the handler

Jump to

Keyboard shortcuts

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