gomple

package module
v0.0.0-...-a30bf4c Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 5 Imported by: 0

README

gomple

Gomple is an experimental small HTTP Framework with integrates well with net/http (powered by go-chi/chi).

Example

see example_test.go

Documentation

Overview

Example
package main

import (
	"net/http"

	"github.com/johejo/gomple"
)

func handle(w http.ResponseWriter, r *http.Request) error {

	return nil
}

func main() {
	// with http.NewServeMux
	g := gomple.New()
	mux := http.NewServeMux()
	mux.HandleFunc("/", g.WrapFunc(handle))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorHandler

func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)

Types

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)

type Gomple

type Gomple struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Gomple

func (*Gomple) JSON

func (g *Gomple) JSON(w http.ResponseWriter, r *http.Request, j interface{})

func (*Gomple) WrapFunc

func (g *Gomple) WrapFunc(h HandlerFunc) http.HandlerFunc

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

type Mux

type Mux struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"encoding/json"
	"log"
	"net/http"

	"github.com/johejo/gomple"
)

func errHandler(w http.ResponseWriter, r *http.Request, err error) {
	if err != nil {

		log.Println(err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func main() {
	g := gomple.New(gomple.WithErrorHandler(errHandler))
	c := &controller{gomple: g}
	mux := gomple.NewMuxWithGomple(g)
	mux.Post("/users", c.createUser)
}

type controller struct {
	gomple *gomple.Gomple
}

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

func (c *controller) createUser(w http.ResponseWriter, r *http.Request) error {
	var u User
	if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
		return err
	}
	log.Println(u)
	w.WriteHeader(http.StatusCreated)
	return nil
}
Output:

func NewMux

func NewMux(opts ...Option) *Mux

func NewMuxWithGomple

func NewMuxWithGomple(g *Gomple) *Mux

func (*Mux) Connect

func (m *Mux) Connect(pattern string, h HandlerFunc)

func (*Mux) Delete

func (m *Mux) Delete(pattern string, h HandlerFunc)

func (*Mux) Get

func (m *Mux) Get(pattern string, h HandlerFunc)

func (*Mux) Gomple

func (m *Mux) Gomple() *Gomple

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(pattern string, h HandlerFunc)

func (*Mux) Head

func (m *Mux) Head(pattern string, h HandlerFunc)

func (*Mux) Options

func (m *Mux) Options(pattern string, h HandlerFunc)

func (*Mux) Post

func (m *Mux) Post(pattern string, h HandlerFunc)

func (*Mux) Put

func (m *Mux) Put(pattern string, h HandlerFunc)

func (*Mux) Raw

func (m *Mux) Raw() *chi.Mux

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Mux) Trace

func (m *Mux) Trace(pattern string, h HandlerFunc)

type Option

type Option func(cfg *config)

func WithErrorHandler

func WithErrorHandler(eh ErrorHandler) Option

Jump to

Keyboard shortcuts

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