Skip to content

apriendeau/shttp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
	}
}

About

my http package for writing APIs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages