gores

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2021 License: Apache-2.0 Imports: 4 Imported by: 13

README

gores

Build Status GoDoc Go Report Card

http response utility library for Go

this package is very small and lightweight, useful for RESTful APIs.

installation

go get github.com/alioygur/gores

requirements

gores library requires Go version >=1.7

usage

package main

import (
	"log"
	"net/http"

	"github.com/alioygur/gores"
)

type User struct {
	Name  string
	Email string
	Age   int
}

func main() {
	// Plain text response
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		gores.String(w, http.StatusOK, "Hello World")
	})

	// HTML response
	http.HandleFunc("/html", func(w http.ResponseWriter, r *http.Request) {
		gores.HTML(w, http.StatusOK, "<h1>Hello World</h1>")
	})

	// JSON response
	http.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
		user := User{Name: "Ali", Email: "ali@example.com", Age: 28}
		gores.JSON(w, http.StatusOK, user)
	})

	// File response
	http.HandleFunc("/file", func(w http.ResponseWriter, r *http.Request) {
		err := gores.File(w, r, "./path/to/file.html")

		if err != nil {
			log.Println(err.Error())
		}
	})

	// Download file
	http.HandleFunc("/download-file", func(w http.ResponseWriter, r *http.Request) {
		err := gores.Download(w, r, "./path/to/file.pdf", "example.pdf")

		if err != nil {
			log.Println(err.Error())
		}
	})

	// No content
	http.HandleFunc("/no-content", func(w http.ResponseWriter, r *http.Request) {
		gores.NoContent(w)
	})

	// Error response
	http.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
		gores.Error(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
	})

	err := http.ListenAndServe(":8000", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

for more documentation godoc

Contribute

Use issues for everything

  • Report problems
  • Discuss before sending a pull request
  • Suggest new features/recipes
  • Improve/fix documentation

Thanks & Authors

I use code/got inspiration from these excellent libraries:

Documentation

Overview

Package gores http response utility library for GO

Index

Constants

View Source
const (
	CONNECT = "CONNECT"
	DELETE  = "DELETE"
	GET     = "GET"
	HEAD    = "HEAD"
	OPTIONS = "OPTIONS"
	PATCH   = "PATCH"
	POST    = "POST"
	PUT     = "PUT"
	TRACE   = "TRACE"
)

HTTP Methods

View Source
const (
	ApplicationJSON                  = "application/json"
	ApplicationJSONCharsetUTF8       = ApplicationJSON + "; " + CharsetUTF8
	ApplicationJavaScript            = "application/javascript"
	ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8
	ApplicationXML                   = "application/xml"
	ApplicationXMLCharsetUTF8        = ApplicationXML + "; " + CharsetUTF8
	ApplicationForm                  = "application/x-www-form-urlencoded"
	ApplicationProtobuf              = "application/protobuf"
	ApplicationMsgpack               = "application/msgpack"
	TextHTML                         = "text/html"
	TextHTMLCharsetUTF8              = TextHTML + "; " + CharsetUTF8
	TextPlain                        = "text/plain"
	TextPlainCharsetUTF8             = TextPlain + "; " + CharsetUTF8
	MultipartForm                    = "multipart/form-data"
)

Media Types

View Source
const (
	AcceptEncoding     = "Accept-Encoding"
	Authorization      = "Authorization"
	ContentDisposition = "Content-Disposition"
	ContentEncoding    = "Content-Encoding"
	ContentLength      = "Content-Length"
	ContentType        = "Content-Type"
	Location           = "Location"
	Upgrade            = "Upgrade"
	Vary               = "Vary"
	WWWAuthenticate    = "WWW-Authenticate"
	XForwardedFor      = "X-Forwarded-For"
	XRealIP            = "X-Real-IP"
)

Headers

View Source
const (
	// CharsetUTF8 utf8 character set
	CharsetUTF8 = "charset=utf-8"

	// WebSocket web socket protocol
	WebSocket = "websocket"
)

Variables

This section is empty.

Functions

func Download

func Download(w http.ResponseWriter, r *http.Request, path string, name string) error

Download the client is prompted to save the file with provided `name`, name can be empty, in that case name of the file is used.

func Error

func Error(w http.ResponseWriter, code int, message string)

Error sends a error response with a status code

func File

func File(w http.ResponseWriter, r *http.Request, path string) error

File sends a response with the content of the file

func HTML

func HTML(w http.ResponseWriter, code int, html string)

HTML sends an HTTP response with status code.

func JSON

func JSON(w http.ResponseWriter, code int, i interface{}) error

JSON sends a JSON response with status code.

func JSONIndent

func JSONIndent(w http.ResponseWriter, code int, i interface{}, prefix string, indent string) error

JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.

func JSONP

func JSONP(w http.ResponseWriter, code int, callback string, i interface{}) error

JSONP sends a JSONP response with status code. It uses `callback` to construct the JSONP payload.

func MustDownload added in v1.2.2

func MustDownload(w http.ResponseWriter, r *http.Request, path string, name string)

MustDownload calls Download and panics on error

func MustFile added in v1.2.2

func MustFile(w http.ResponseWriter, r *http.Request, path string)

MustFile calls File and panics on error

func MustJSON added in v1.2.2

func MustJSON(w http.ResponseWriter, code int, i interface{})

MustJSON calls JSON and panics on error

func MustJSONIndent added in v1.2.2

func MustJSONIndent(w http.ResponseWriter, code int, i interface{}, prefix string, indent string)

MustJSONIndent calls JSONIndent and panics on error

func MustJSONP added in v1.2.2

func MustJSONP(w http.ResponseWriter, code int, callback string, i interface{})

MustJSONP calls JSONP and panics on error

func MustXML added in v1.2.2

func MustXML(w http.ResponseWriter, code int, i interface{})

MustXML calls XML and panics on error

func MustXMLIndent added in v1.2.2

func MustXMLIndent(w http.ResponseWriter, code int, i interface{}, prefix string, indent string)

MustXMLIndent calls XMLIndent and panics on error

func NoContent

func NoContent(w http.ResponseWriter)

NoContent sends a response with no body and a status code.

func String

func String(w http.ResponseWriter, code int, s string)

String sends a string response with status code.

func XML

func XML(w http.ResponseWriter, code int, i interface{}) error

XML sends an XML response with status code.

func XMLIndent

func XMLIndent(w http.ResponseWriter, code int, i interface{}, prefix string, indent string) error

XMLIndent sends an XML response with status code, but it applies prefix and indent to format the output.

Types

This section is empty.

Jump to

Keyboard shortcuts

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