cake

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

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

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 14 Imported by: 0

README ΒΆ

Cake

GitHub code size in bytes GitHub issues Go Report Card Version

Report a Bug | Request a Feature | Ask a Question


Cake is a tasty turn-key API toolkit for Go. Cake takes care of the boring stuff and lets you focus on your business logic. You can finally have your cake and eat it to.

🍰 Let's bake

Getting started with Cake is fast, and it gives your all the tools you need to create a sweet API:

package main

import (
	"github.com/arivictor/cake"
	"net/http"
)

func main() {
	app := cake.New()

	app.Route(http.MethodGet, "/:name", Handler)

	app.Run("localhost", 8080)
}

func Handler(w http.ResponseWriter, r *http.Request) {
	name := cake.URLParam(r, "name", "there")
	cake.Send(w, "Hi "+name+"!")
}

Middleware Support

Cake comes boxed with common middleware to get you started. You can load middleware per route or at the global scope.

package main

import (
	"github.com/arivictor/cake"
	"github.com/arivictor/cake/middleware"
	"net/http"
)

func main() {
	app := cake.New()

	// Global middleware
	app.Use(middleware.CORS)

	// Route scoped middleware
	app.Route(http.MethodGet, "/:name", app.WrapRoute(Handler, middleware.LogRequest))

	app.Run("localhost", 8080)
}

func Handler(w http.ResponseWriter, r *http.Request) {
	name := cake.URLParam(r, "name", "there")
	cake.Send(w, "Hi "+name+"!")
}

You can provide your own by using the below signature

package myapp

import "net/http"

func ExampleMiddleware(next http.HandlerFunc) http.HandlerFunc {
    return func (w http.ResponseWriter, r *http.Request) {
		// Do stuff here
		
        next.ServeHTTP(w, r)
    }
}

^ No preservatives, no added colours, just delicious routing...

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func BadRequestResponse ΒΆ

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

BadRequestResponse writes a bad request response

func ErrorResponse ΒΆ

func ErrorResponse(w http.ResponseWriter, status int, message interface{})

ErrorResponse writes an error message back to the requester

func FailedValidationResponse ΒΆ

func FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string)

FailedValidationResponse writes a failed validation response

func JSON ΒΆ

func JSON(w http.ResponseWriter, data map[string]interface{}) error

JSON to http response writer

func MethodNotAllowedResponse ΒΆ

func MethodNotAllowedResponse(w http.ResponseWriter, r *http.Request)

MethodNotAllowedResponse writes a method not allowed response

func NotFoundResponse ΒΆ

func NotFoundResponse(w http.ResponseWriter, r *http.Request)

NotFoundResponse writes a resource not found response

func Send ΒΆ

func Send(w http.ResponseWriter, data string)

func ServerErrorResponse ΒΆ

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

ServerErrorResponse writes an internal server error response

func URLParam ΒΆ

func URLParam(req *http.Request, key string, fallback string) string

Types ΒΆ

type IApp ΒΆ

type IApp interface {
	Route(method string, path string, handler http.HandlerFunc)
	Run(host string, port int)
	Use(middleware ...middleware.AppMiddleware)
	WrapRoute(handler http.HandlerFunc, middleware ...middleware.AppMiddleware) http.HandlerFunc
}

func New ΒΆ

func New() IApp

type Level ΒΆ

type Level int8

Level represents a log level

const (
	Debug Level = iota
	Info
	Warn
	Error
	Fatal
)

func (Level) String ΒΆ

func (l Level) String() string

type Logger ΒΆ

type Logger struct {
	Out      io.Writer
	MinLevel Level
	// contains filtered or unexported fields
}

Logger model

func (*Logger) Debug ΒΆ

func (l *Logger) Debug(message string, properties map[string]string)

Debug labelled log

func (*Logger) Error ΒΆ

func (l *Logger) Error(err error, properties map[string]string)

Error labelled log

func (*Logger) Fatal ΒΆ

func (l *Logger) Fatal(err string, properties map[string]string)

Fatal labelled log and exits process

func (*Logger) Info ΒΆ

func (l *Logger) Info(message string, properties map[string]string)

Info labelled log

func (*Logger) Warn ΒΆ

func (l *Logger) Warn(message string, properties map[string]string)

Warn labelled log

func (*Logger) Write ΒΆ

func (l *Logger) Write(message []byte) (n int, err error)

Write outputs a raw message

type Route ΒΆ

type Route struct {
	Path        *regexp.Regexp
	Method      string
	HandlerFunc http.HandlerFunc
}

func NewRoute ΒΆ

func NewRoute(path *regexp.Regexp, httpMethod string, handlerFunc http.HandlerFunc) Route

func (*Route) Match ΒΆ

func (route *Route) Match(request *http.Request) map[string]string

type Router ΒΆ

type Router struct {
	Routes           Routes
	GlobalMiddleware []middleware.AppMiddleware
	NotFound         http.HandlerFunc
	MethodNotAllowed http.HandlerFunc
}

func (*Router) ServeHTTP ΒΆ

func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

type Routes ΒΆ

type Routes []Route

type Server ΒΆ

type Server struct {
	Host    string
	Port    int
	Handler *Router
	Logger  *Logger
}

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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