toolkit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: MIT Imports: 12 Imported by: 0

README

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 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/NhanNT-SE/go-module

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"`
}

JSONResponse is a type used for sending Json around

type Tools

type Tools struct {
	MaxFileSize        int64
	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 receives Tools

func (*Tools) CreateDirIfNotExists

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

CreateDirIfNotExists creates a directory, and all necessary parents, if it does not exists

func (*Tools) DownloadStaticFile

func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, p, file, 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 specifications of the display name

func (*Tools) ErrorJSON

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

ErrorJSON take an error, optionally a status code, and generates and sends a Json error

func (*Tools) PushJsonToRemote

func (t *Tools) PushJsonToRemote(uri string, data any, client ...*http.Client) (*http.Response, int, error)

PushJsonToRemote posts aribtrary data to some URL and 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 characters of length 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

ReadJSON tries to reade the body of a request and converts from json into a go data variable

func (*Tools) Slugify

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

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

func (*Tools) UploadFiles

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

func (*Tools) UploadOneFile

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

func (*Tools) WriteJSON

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

WriteJSON takes a response status code and arbitrary data and writes json to the client

type UploadedFile

type UploadedFile struct {
	NewFileName     string
	OriginalFileNam string
	FileSize        int64
}

UploadedFile is a struct to sve information about an uploaded file

Jump to

Keyboard shortcuts

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