gotoolkit

package module
v3.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 12 Imported by: 0

README

GoToolkit

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 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/clicworth-com/gotoolkit

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

type Tools

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

Tools is the type to instantitate this module. Any varialee of this type will havee access to all the 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, displayName string)

DownloadStaticFile downloads a file, 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

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

func (*Tools) GetIP added in v3.3.1

func (t *Tools) GetIP(r *http.Request) (string, error)

func (*Tools) IsDigit added in v3.3.2

func (t *Tools) IsDigit(s string) bool

func (*Tools) PushJSONToRemote

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

PushJSONToRemote posts arbitrary data to some URL as JSON, and returns the response, status code, and error, if any. The final parameter, client, is optional. If none is specified, we use the standard http.Client.

func (*Tools) RandomString

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

RandomString returns a string of random character of lenght n, using randomStringSource as the source for the string

func (*Tools) ReadJSON

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

func (*Tools) Slugify

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

Slugify is a (very) simple means of creating a slug from a string

func (*Tools) UploadFiles

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

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.

func (*Tools) WriteJSON

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

type UploadedFile

type UploadedFile struct {
	NewFileName      string
	OriginalFileName string
	FileSize         int64
}

UploadedFile is a struct used to save information about an uploaded file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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