Documentation
¶
Index ¶
- type JSONResponse
- type Tools
- func (t *Tools) CreateDirIfNotExist(path string) error
- func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, path, file, displayName string)
- func (t *Tools) ErrorJSON(w http.ResponseWriter, err error, status ...int) error
- func (t *Tools) PushJSONToRemote(uri string, data any, client ...*http.Client) (*http.Response, int, error)
- func (t *Tools) RandomString(n int) string
- func (t *Tools) ReadJSON(w http.ResponseWriter, r *http.Request, data any) error
- func (t *Tools) Slugify(s string) (string, error)
- func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)
- func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)
- func (t *Tools) WriteJSON(w http.ResponseWriter, status int, data any, headers ...http.Header) error
- type UploadedFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONResponse ¶
type JSONResponse struct {
Error bool `json:"error"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
JSONResponse is the type used to send JSON
type Tools ¶
type Tools struct {
MaxFileSize int
AllowedFileTypes []string
MaxJSONSize int
AllowUnknownFields bool
}
Tools is the type used to instantiate this module. Any variable of this type will have access to all the methods with the receiver *Tools.
func (*Tools) CreateDirIfNotExist ¶
CreateDirIfNotExist creates a directory, together with any necessary parents, if it does not exist.
func (*Tools) DownloadStaticFile ¶
func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, path, file, displayName string)
DownloadStaticFile downloads a file, and tries to force browser to avoid displaying it by setting content disposition. It also allows specification of the display name.
func (*Tools) ErrorJSON ¶
ErrorJSON takes an error and optionally a status code and generates and sends an error JSON response to client
func (*Tools) PushJSONToRemote ¶
func (t *Tools) PushJSONToRemote(uri string, data any, client ...*http.Client) (*http.Response, int, error)
PushJSONToRemote posts JSON data to the uri and returns the response and status code or an error The client parameter is optional. If none is specified, we use the standard http.Client.
func (*Tools) RandomString ¶
RandomString returns a string of random characters of length n, drawing characters from randomStringSource.
func (*Tools) ReadJSON ¶
ReadJSON tries to read the body of a request and converts the JSON into a Go data variable
func (*Tools) UploadFiles ¶
func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)
UploadFiles uploads one or more files to a specified directory, and gives the files a new randomized name. It returns a slice containing the newly named files, the original file, names, the size of the file, and potentially an error. If the optional rename parameter is set to false, the files will not be renamed.
func (*Tools) UploadOneFile ¶
func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)
UploadOneFile is a convenience method that calls UploadFiles, but expects only one file in the upload.
type UploadedFile ¶
UploadedFile is a struct used to save information about an uploaded file