Documentation ¶
Index ¶
- func CheckExist(filepath string) bool
- func CheckMethod(w http.ResponseWriter, r *http.Request, excepted string) bool
- func DisableListDir(log *logger.Logger, h http.HandlerFunc) http.HandlerFunc
- func ErrHandle(w http.ResponseWriter, err error) bool
- func ErrHandleWithCode(w http.ResponseWriter, err error, code int) bool
- func GetClientIP(r *http.Request) string
- func GetEnvWithCheck(name, key string, log *logger.Logger) string
- func GetTaskIDByRequest(r *http.Request) string
- func IsDir(path string) bool
- func IsFile(path string) bool
- func MaskString(str string, showNum int) string
- func MustBeOr404(w http.ResponseWriter, r *http.Request, path string) bool
- func MustExistOr404(w http.ResponseWriter, r *http.Request, filepath string) bool
- func RenderJson(w http.ResponseWriter, data []byte, code int) (err error)
- func RenderTemplate(templatePath string, w http.ResponseWriter, data interface{}) error
- func RenderTemplateString(templateString string, w http.ResponseWriter, data interface{}) error
- func RequestFilter(pathMustBe string, methodMustBe string, log *logger.Logger, ...) http.HandlerFunc
- func TemplateRenderHandler(templatePath string, contextCreator ContextCreator, log *logger.Logger) http.HandlerFunc
- func TemplateStringRenderHandler(templateName string, templateString string, contextCreator ContextCreator, ...) http.HandlerFunc
- type ContextCreator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckMethod ¶
CheckMethod check if request method is as excepted. If not, write the status "MethodNotAllow" to header, "Method Not Allowed." to response and return false. Else don't do anything and return true.
func DisableListDir ¶
func DisableListDir(log *logger.Logger, h http.HandlerFunc) http.HandlerFunc
DisableListDir accept a handle func and return a handle that not allow list dir.
func ErrHandle ¶
func ErrHandle(w http.ResponseWriter, err error) bool
ErrHandle is a simple error handle function. See ErrHandleWithCode, this func use `http.StatusInternalServerError` as code.
func ErrHandleWithCode ¶
func ErrHandleWithCode(w http.ResponseWriter, err error, code int) bool
ErrHandleWithCode is a simple error handle function. If err is an error, write code to header and write error message to response and return true. Else (err is nil), don't do anything and return false.
func GetClientIP ¶
GetClientIP get client ip address from a http request. Try to get ip from x-forwarded-for header first, If key not exist in header, try to get ip:host from r.RemoteAddr split it to ip:port and return ip, If error happened, return 0.0.0.0
func GetEnvWithCheck ¶
GetEnvWithCheck will get a env var, print it, and return it. If the var is empty, will raise a Fatal.
func GetTaskIDByRequest ¶
GetTaskIDByRequest gets last part of url path as a taskID and return it.
func MaskString ¶
MaskString keep first `showNum` count char of a string `str` and change all remaining chars to "*"
func MustBeOr404 ¶
MustBeOr404 check if URL path is as excepted. If not equal, write 404 to header, "404 not fount" to response, and return false. Else don't do anything and return true.
func MustExistOr404 ¶
MustExistOr404 check if a file is exist. If not, write 404 to header, "404 not fount" to response, and return false. Else don't do anything and return true. noinspection GoUnusedExportedFunction
func RenderJson ¶
func RenderJson(w http.ResponseWriter, data []byte, code int) (err error)
RenderJson is a shortcut function to write JSON data to response, and set the header Content-Type.
func RenderTemplate ¶
func RenderTemplate(templatePath string, w http.ResponseWriter, data interface{}) error
RenderTemplate is a shortcut function to render template to response.
func RenderTemplateString ¶
func RenderTemplateString(templateString string, w http.ResponseWriter, data interface{}) error
func RequestFilter ¶
func RequestFilter(pathMustBe string, methodMustBe string, log *logger.Logger, handlerFunc http.HandlerFunc) http.HandlerFunc
RequestFilter accept a http.HandlerFunc and return a new one which only accept path is pathMustBe and method is methodMustBe. Error message in new handler will be print with logger log, if log is nil, will use default logger. If pathMustBe or methodMustBe is empty string, no check will be performed.
func TemplateRenderHandler ¶
func TemplateRenderHandler(templatePath string, contextCreator ContextCreator, log *logger.Logger) http.HandlerFunc
TemplateRenderHandler is a shortcut function that generate a http.HandlerFunc. The generated func use contextCreator to create context and render the templatePath template file. If contextCreator is nil, nil will be used as context.
func TemplateStringRenderHandler ¶
func TemplateStringRenderHandler(templateName string, templateString string, contextCreator ContextCreator, log *logger.Logger) http.HandlerFunc
TemplateStringRenderHandler is a shortcut function that generate a http.HandlerFunc. The generated func use contextCreator to create context and render the templateString as template. If contextCreator is nil, nil will be used as context.
Types ¶
type ContextCreator ¶
ContextCreator accept a request and return a context, used in TemplateRenderHandler.