Skip to content

Latest commit

 

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Toolbox

Toolbox is a convenience package created for the foundational layer of Go projects.

Purpose

This package was created to limit rewriting functions or repeated code for projects. It is a personal library of reusable functions commonly used in development.

Features

  • Random String Generator: Generates a random string of specified length.
  • File Upload: Uploads files to a specified directory with support for MIME type and file size validation.
  • Directory Creator: Creates directories for non-existent paths.
  • Directory Cleaner: Removes all files in a specified directory while preserving the directory itself.
  • Slug Generator: Generates URL-safe slugs from strings.
  • File Downloader: Downloads files with a specified name, forcing the browser to avoid displaying them in the browser window.
  • JSON Reader: Reads JSON data from an HTTP request and decodes it into a specified struct.
  • JSON Writer: Encodes data to JSON and writes it to an HTTP response.
  • Post JSON with Client: Sends a JSON-encoded HTTP POST request to a remote service.

Installation

To install the Toolbox package, run:

go get -u github.com/wtran29/toolbox

Usage Examples

Random String Generator

tools := toolbox.Tools{}
randomString := tools.RandomString(10)
fmt.Println(randomString)

File Upload

http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
    tools := toolbox.Tools{}
    files, err := tools.UploadFiles(r, "./uploads", true)
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    fmt.Fprintf(w, "Files uploaded: %v", files)
})

Directory Creator

tools := toolbox.Tools{}
err := tools.CreateDirIfNotExist("./new_directory")
if err != nil {
    log.Fatal(err)
}

Directory Cleaner

tools := toolbox.Tools{}
err := tools.CleanDirectory("./uploads")
if err != nil {
    log.Fatal(err)
}

Slug Generator

tools := toolbox.Tools{}
slug, err := tools.Slugify("Example String!")
if err != nil {
    log.Fatal(err)
}
fmt.Println(slug) // Output: example-string

File Downloader

http.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) {
    tools := toolbox.Tools{}
    tools.DownloadStaticFile(w, r, "./files", "example.txt", "example_download.txt")
})

JSON Reader

http.HandleFunc("/readjson", func(w http.ResponseWriter, r *http.Request) {
    tools := toolbox.Tools{}
    var data struct {
        Name string `json:"name"`
    }
    err := tools.ReadJSON(w, r, &data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    fmt.Fprintf(w, "Received: %v", data)
})

JSON Writer

http.HandleFunc("/writejson", func(w http.ResponseWriter, r *http.Request) {
    tools := toolbox.Tools{}
    payload := toolbox.JSONResponse{
        Error:   false,
        Message: "Success",
    }
    err := tools.WriteJSON(w, http.StatusOK, payload)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
})

Post JSON with Client

tools := toolbox.Tools{}
data := map[string]string{"foo": "bar"}
res, statusCode, err := tools.PostJSONWithClient("http://example.com/api", data)
if err != nil {
    log.Fatal(err)
}
defer res.Body.Close()
fmt.Printf("Status Code: %d\n", statusCode)

Contributing

Feel free to open issues or submit pull requests if you have suggestions for improvements or new features.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Toolbox is a convenience package for Go projects

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages