toolkit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 14 Imported by: 0

README

GO Toolkit

A simple example of how to create a reusable Go module with commonly used tools.

The included tools are:

  • Read JSON
  • Write JSON
  • Produce a JSON encoded error response
  • Upload a file or multiple files to a specified directory
  • Download a static file
  • Get a random string of length n
  • Post JSON to a remote service
  • Create a directory, including all parent directories, if it does not already exist
  • Create a URL safe slug from a string

Installation

go get -u github.com/StratoNET/GO-Toolkit

Documentation

Index

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    interface{} `json:"data,omitempty"`
}

JSONResponse is used hold and transport JSON

type Tools

type Tools struct {
	AllowedFileTypes   []string
	MaxFileSize        int
	MaxJSONPayloadSize int
	AllowUnknownFields bool
}

Tools is used to instantiate this module. Any variable of this type will have access to all methods with the receiver *Tools

func (*Tools) CreateDirIfNotExist

func (t *Tools) CreateDirIfNotExist(path string) error

CreateDirIfNotExist creates a directory and all necessary parents, if it does not exist

func (*Tools) DownloadStaticFile

func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, pathName, fileName, displayName string)

DownloadStaticFile downloads a file and forces the browser not to open/display it by setting content disposition; (specification of the file display name is also available)

func (*Tools) ErrorJSON

func (t *Tools) ErrorJSON(w http.ResponseWriter, err error, status ...int) error

ErrorJSON takes an error and an optional status code, then generates and sends a JSON error message

func (*Tools) PushJSONToRemoteService

func (t *Tools) PushJSONToRemoteService(uri string, data interface{}, client ...*http.Client) (*http.Response, int, error)

PushJSONToRemoteService posts JSON data to remote service, returning a response, status code and any error

func (*Tools) RandomString

func (t *Tools) RandomString(n int) string

RandomString returns string of random characters of length n, generated from randomStringSource

func (*Tools) ReadJSON

func (t *Tools) ReadJSON(w http.ResponseWriter, r *http.Request, data interface{}) error

ReadJSON attempts to read request body and converts from JSON into a data variable

func (*Tools) Slugify

func (t *Tools) Slugify(s string) (string, error)

Slugify creates a slug from a string

func (*Tools) UploadFiles

func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)

UploadFiles allows uploading multiple files in one action to a specified directory with, if required, random naming. A slice containing newly named files, original file names, file size is returned and potentially an error. If the optional last parameter is set to true then files are not renamed but retain their original file names.

func (*Tools) UploadOneFile

func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)

UploadOneFile convenience method which restricts to uploading only one file

func (*Tools) WriteJSON

func (t *Tools) WriteJSON(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error

WriteJSON takes a response status code & any data then writes JSON to the client

type UploadedFile

type UploadedFile struct {
	NewFileName      string
	OriginalFileName string
	FileSize         int64
}

UploadedFile is used to hold information about an uploaded file

Jump to

Keyboard shortcuts

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