Universal pkg folder for microservices

PkgJson:

Pkg for work with JSON.

Functions:

  • ReadJSON – tries to read the body of a request and converts it into json
  • WriteJSON – takes a response status code and arbitrary data and writes a json response to the client
  • ErrorJSON – takes an error, and optionally a response status code, and generates and sends a json error response
  • PushJSONToRemote – posts arbitrary json to some url, and returns error, if any, as well as the response status code

Example:

package main

import (
    PkgJson "github.com/dmRusakov/pkg/json"
)

var pkgJson PkgJson.Json

func main() {
    var jsonFromService PkgJson.JSONResponse
    _ = pkgJson.ReadJSON(w, r, jsonFromService)
}

Import:

go get -u github.com/dmRusakov/pkg/json

PkgFiles

Pkg for work with directory and files.

Functions:

  • DownloadStaticFile – downloads a file, and tries to force the browser to avoid displaying it inccthe browser window by setting content-disposition. It also allows specification of the display name.
  • UploadOneFile – uploads one file to a specified directory, and gives it a random name. It returns the newly named file, the original file name, and potentially an error.
  • RandomString – returns a random string of letters of length n

Example:

package main

import (
    PkgFiles "github.com/dmRusakov/pkg/files"
)

var pkgFiles PkgFiles.Files

func (app *Config) DownloadAFile(w http.ResponseWriter, r *http.Request) {
    pkgFiles.DownloadStaticFile(w, r, "./upload", "some-file.pdf", "some-file.pdf")
}

Import:

go get -u github.com/dmRusakov/pkg/files

GitHub

View Github