util

package
v0.0.0-...-80b44f5 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckExist

func CheckExist(filepath string) bool

CheckExist check if a file or dir is Exist.

func CheckMethod

func CheckMethod(w http.ResponseWriter, r *http.Request, excepted string) bool

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

func GetClientIP(r *http.Request) string

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

func GetEnvWithCheck(name, key string, log *logger.Logger) string

GetEnvWithCheck will get a env var, print it, and return it. If the var is empty, will raise a Fatal.

func GetTaskIDByRequest

func GetTaskIDByRequest(r *http.Request) string

GetTaskIDByRequest gets last part of url path as a taskID and return it.

func IsDir

func IsDir(path string) bool

IsDir check if the path is a file, false when not exist or is a dir.

func IsFile

func IsFile(path string) bool

IsFile check if a path a file, false when not exist or is a file.

func MaskString

func MaskString(str string, showNum int) string

MaskString keep first `showNum` count char of a string `str` and change all remaining chars to "*"

func MustBeOr404

func MustBeOr404(w http.ResponseWriter, r *http.Request, path string) bool

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

func MustExistOr404(w http.ResponseWriter, r *http.Request, filepath string) bool

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

type ContextCreator func(r *http.Request) interface{}

ContextCreator accept a request and return a context, used in TemplateRenderHandler.

Jump to

Keyboard shortcuts

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