Documentation
¶
Index ¶
- type JSONResponse
- type Tools
- func (t *Tools) CreateDirIfNotExist(path string) error
- func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, p, file, displayName string)
- func (t *Tools) ErrorJSON(w http.ResponseWriter, err error, status ...int) error
- func (t *Tools) ErrorXML(w http.ResponseWriter, err error, status ...int) error
- func (t *Tools) PushJSONToRemote(uri string, data interface{}, 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 interface{}) error
- func (t *Tools) ReadXML(w http.ResponseWriter, r *http.Request, data interface{}) 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 interface{}, headers ...http.Header) error
- func (t *Tools) WriteXML(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error
- type UploadedFile
- type XMLResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONResponse ¶ added in v1.0.3
type JSONResponse struct { Error bool `json:"error"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` }
JSONResponse is the type used for sending JSON around.
type Tools ¶
type Tools struct { MaxJSONSize int // maximum size of JSON file we'll process MaxXMLSize int // maximum size of XML file we'll process MaxFileSize int // maximum size of uploaded files in bytes AllowedFileTypes []string // allowed file types for upload (e.g. image/jpeg) AllowUnknownFields bool // if set to true, allow unknown fields in JSON ErrorLog *log.Logger // the info log. InfoLog *log.Logger // the error log. }
Tools is the type for this package. Create a variable of this type, and you have access to all the exported methods with the receiver type *Tools.
func (*Tools) CreateDirIfNotExist ¶ added in v1.0.6
CreateDirIfNotExist creates a directory, and all necessary parent directories, if it does not exist.
func (*Tools) DownloadStaticFile ¶ added in v1.0.3
func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, p, file, displayName string)
DownloadStaticFile downloads a file to the remote user, and tries to force the browser to avoid displaying it in the browser window by setting content-disposition. It also allows specification of the display name.
func (*Tools) ErrorJSON ¶
ErrorJSON takes an error, and optionally a response status code, and generates and sends a JSON error response.
func (*Tools) ErrorXML ¶ added in v1.2.0
ErrorXML takes an error, and optionally a response status code, and generates and sends an XML error response.
func (*Tools) PushJSONToRemote ¶
func (t *Tools) PushJSONToRemote(uri string, data interface{}, client ...*http.Client) (*http.Response, int, error)
PushJSONToRemote posts arbitrary json to some url, and returns the response, the response status code, and error, if any. The final parameter, client, is optional, and will default to the standard http.Client. It exists to make testing possible without an active remote url.
func (*Tools) RandomString ¶
RandomString returns a random string of letters of length n, using characters specified in randomStringSource.
func (*Tools) ReadJSON ¶
ReadJSON tries to read the body of a request and converts it from JSON to a variable. The third parameter, data, is expected to be a pointer, so that we can read data into it.
func (*Tools) ReadXML ¶ added in v1.1.10
ReadXML tries to read the body of an XML request into a variable. The third parameter, data, is expected to be a pointer, so that we can read data into it.
func (*Tools) Slugify ¶ added in v1.1.6
Slugify is a (very) simple means of creating a slug from a provided string.
func (*Tools) UploadFiles ¶ added in v1.1.0
func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)
UploadFiles uploads one or more file to a specified directory, and gives the files a random name. It returns a slice containing the newly named files, the original file names, the size of the files, and potentially an error. If the optional last parameter is set to true, then we will not rename the files, but will use the original file names.
func (*Tools) UploadOneFile ¶ added in v1.0.4
func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)
UploadOneFile is just a convenience method that calls UploadFiles, but expects only one file to be in the upload.
type UploadedFile ¶ added in v1.0.8
UploadedFile is the type used for the uploaded file.
type XMLResponse ¶ added in v1.1.9
type XMLResponse struct { Error bool `xml:"error"` Message string `xml:"message"` Data interface{} `xml:"data,omitempty"` }
XMLResponse is the type used for sending XML around.