shttp

package module
v0.0.0-...-332ed8c Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2015 License: MIT Imports: 5 Imported by: 0

README

shttp (simple-http)

So after a while while programming with Go, I discovered that golang's net/http is very nicely designed for writing web apps but I wanted to add helpers on top of it to cover my API use cases. The godoc is more useful than here but here is a quick example.

This is meant to not limit you in any way from what choosing your own router. I tend to lean towards HTTPRouter but choose your own flavor.

package main

import (
	"log"
	"net/http"

	"github.com/apriendeau/shttp"
)

func handler(w http.ResponseWriter, r *http.Request) {
	body := make(map[string]string)
	if err := shttp.Read(r, &body); err != nil {
		if err := shttp.Error(w, r, err, 422); err != nil {
			log.Panic(err)
		}
	}

	message := struct {
		Message  string            `json:"message"`
		Received map[string]string `json:"received"`
	}{"I received your information.", body}

	if err := shttp.Write(w, r, message, 200); err != nil {
		log.Panic(err)
	}
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/post", handler)
	if err := http.ListenAndServe(":8000", mux); err != nil {
		log.Panic(err)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(w http.ResponseWriter, r *http.Request, err error, status int) error

Error is used for when there is any kind of error and returns a consistant error format that the user can parse back.

func Read

func Read(r *http.Request, body interface{}) error

Read is a helper that responds parses the req.Body into the interface of your choice so that you can actually do something useful with the information that is sent to you.

Example
package main

import (
	"log"
	"net/http"

	"github.com/apriendeau/shttp"
)

func main() {
	// Simple mimic server
	handler := func(w http.ResponseWriter, r *http.Request) {
		// parsing for example sake
		body := make(map[string]interface{})
		if err := shttp.Read(r, &body); err != nil {
			log.Panic(err)
		}
		// send body back to mimic
		if err := shttp.Write(w, r, body, 200); err != nil {
			log.Panic(err)
		}
	}

	http.HandleFunc("/post", handler)
	if err := http.ListenAndServe(":8000", nil); err != nil {
		log.Panic(err)
	}
}
Output:

func Status

func Status(w http.ResponseWriter, r *http.Request, status int) error

Status only writes only status back to the req.

func Write

func Write(w http.ResponseWriter, r *http.Request, body interface{}, status int) (err error)

Write is what you use to to respond to the req when there is no error. It has content negotiation for xml and json so that people can choose their preferred data format.

Example
package main

import (
	"log"
	"net/http"

	"github.com/apriendeau/shttp"
)

func main() {
	handler := func(w http.ResponseWriter, r *http.Request) {
		message := struct {
			Message string `json:"message"`
		}{"I received your request."}

		if err := shttp.Write(w, r, message, 200); err != nil {
			log.Panic(err)
		}
	}

	http.HandleFunc("/post", handler)
	if err := http.ListenAndServe(":8000", nil); err != nil {
		log.Panic(err)
	}
}
Output:

Types

type Err

type Err struct {
	Title       string `json:"title" xml:"title"`
	Description string `json:"description,omitempty" xml:"description,omitempty"`
	Stack       string `json:"stack,omitempty" xml:"stack,omitempty"`
	Err         error  `json:"error" xml:"error"`
}

Err implements the error interface but also has a couple other fields including stack traces so you can see where exactly the error came from in the API when debugging.

func (Err) Error

func (e Err) Error() string

Error is for implementing the error interface

Jump to

Keyboard shortcuts

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