requiem

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 13 Imported by: 0

README

GoDoc Build Status Go Report Card codecov

requiem

Controller-based REST API server container for Golang with Postgres support

Documentation here: https://godoc.org/github.com/mborders/requiem

Example Usage

Without DB
s := requiem.NewServer(... controllers)
s.Start()
With DB
s := requiem.NewServer(... controllers)
s.EnableDB = true
s.Start()
Change port or base path
s := requiem.NewServer(... controllers)
s.Port = 9090
s.BasePath = "/rest"
s.Start()

HttpController example

type MyController struct {
    DB *gorm.DB
}

type Response struct {
    Message string
}

type CreateRequest struct {
    SomeValue string
}

func (c MyController) getStuff(ctx requiem.HTTPContext) {
    m := Response{Message: "Hello, world!"}
    ctx.SendJSON(res)
}

func (c MyController) createStuff(ctx requiem.HTTPContext) {
    m := ctx.Body.(*CreateRequest)
    fmt.Println("Value: %s", m.SomeValue)
    ctx.SendStatus(Http.StatusNoContent)
}

func AuthInterceptor(ctx HTTPContext) bool {
    // Example:
    //   1) Check if user is authenticated
    //   2) Return false if not
    //   3) Use ctx.SetAttribute() to pass user claim
    
    return true
}

func (c MyController) Load(router *requiem.Router) {
    c.DB = router.DB
    r := router.NewRestRouter("/stuff")
    r.Get("/", c.getStuff)
    r.Post("/", c.createStuff)
    
    // Use AuthInterceptor
    r.Get("/interceptor", func(ctx HTTPContext) {
        ctx.SendStatus(http.StatusOK)
    }, AuthInterceptor)
}

DB Connection Environment Variables (if DB is enabled)

DB_HOST
DB_PORT
DB_NAME
DB_USERNAME
DB_PASSWORD

Documentation

Index

Constants

This section is empty.

Variables

Logger provides application-wide logging

Functions

func InitLogger

func InitLogger(exitOnFatal bool)

InitLogger initializes the application-wide logger

func ReadJSON

func ReadJSON(r io.Reader, v interface{}) interface{}

ReadJSON decodes the provided stream into the given interface.

func SendJSON

func SendJSON(w http.ResponseWriter, v interface{})

SendJSON converts the given interface into JSON and writes to the provided stream.

func SendJSONWithStatus added in v0.5.1

func SendJSONWithStatus(w http.ResponseWriter, v interface{}, s int)

SendJSONWithStatus writes a JSON response body to the provided stream, along with the specified status code.

func SendStatus

func SendStatus(w http.ResponseWriter, s int)

SendStatus writes the given HTTP status code into the provided response stream.

Types

type HTTPContext

type HTTPContext struct {
	Response http.ResponseWriter
	Request  *http.Request
	Body     interface{}
	// contains filtered or unexported fields
}

HTTPContext provides utility functions for HTTP requests/responses

func (*HTTPContext) GetAttribute

func (ctx *HTTPContext) GetAttribute(key string) interface{}

GetAttribute returns the context-scoped value for the given key

func (*HTTPContext) GetParam

func (ctx *HTTPContext) GetParam(p string) string

GetParam obtains the given parameter key from the request parameters.

func (*HTTPContext) SendJSON

func (ctx *HTTPContext) SendJSON(v interface{})

SendJSON converts the given interface into JSON and writes to the response.

func (*HTTPContext) SendJSONWithStatus added in v0.5.1

func (ctx *HTTPContext) SendJSONWithStatus(v interface{}, s int)

SendJSONWithStatus writes a JSON response body to the response, along with the specified status code.

func (*HTTPContext) SendStatus

func (ctx *HTTPContext) SendStatus(s int)

SendStatus writes the given HTTP status code into the response.

func (*HTTPContext) SetAttribute

func (ctx *HTTPContext) SetAttribute(key string, attr interface{})

SetAttribute sets the context-scoped value for the given key

type HTTPInterceptor

type HTTPInterceptor func(ctx HTTPContext) bool

HTTPInterceptor allows for pre-processing request handlers ex. an authentication interceptor could verify a user session

type HealthcheckController added in v0.6.0

type HealthcheckController struct {
}

func (HealthcheckController) Load added in v0.6.0

func (c HealthcheckController) Load(router *Router)

type IHttpController

type IHttpController interface {
	Load(router *Router)
}

IHttpController represents a REST API that can be loaded into a router

type RestRouter added in v0.4.0

type RestRouter struct {
	DB *gorm.DB
	// contains filtered or unexported fields
}

RestRouter represents a router for a specific REST API

func (*RestRouter) Delete added in v0.4.0

func (r *RestRouter) Delete(path string, handle func(HTTPContext), v interface{}, interceptors ...HTTPInterceptor)

Delete handles DELETE HTTP requests for the given path

func (*RestRouter) Get added in v0.4.0

func (r *RestRouter) Get(path string, handle func(HTTPContext), interceptors ...HTTPInterceptor)

Get handles GET HTTP requests for the given path

func (*RestRouter) Post added in v0.4.0

func (r *RestRouter) Post(path string, handle func(HTTPContext), v interface{}, interceptors ...HTTPInterceptor)

Post handles POST HTTP requests for the given path

func (*RestRouter) Put added in v0.4.0

func (r *RestRouter) Put(path string, handle func(HTTPContext), v interface{}, interceptors ...HTTPInterceptor)

Put handles PUT HTTP requests for the given path

type Router

type Router struct {
	MuxRouter *mux.Router
	DB        *gorm.DB
}

Router maintains routing paths for a single REST API

func (*Router) NewRestRouter added in v0.4.0

func (r *Router) NewRestRouter(path string) *RestRouter

NewRestRouter initializes a new REST router on at the given path

type Server

type Server struct {
	Port        int
	BasePath    string
	ExitOnFatal bool
	// contains filtered or unexported fields
}

Server represents a REST API server container Default port is 8080 Default path is /api

func NewServer

func NewServer(controllers ...IHttpController) *Server

NewServer creates a route-based REST API server instance

func (*Server) AutoMigrate added in v0.5.0

func (s *Server) AutoMigrate(models ...interface{})

func (*Server) Start

func (s *Server) Start()

Start initializes the API and starts running on the specified port Blocks on current thread

func (*Server) UseHealthcheck added in v0.6.0

func (s *Server) UseHealthcheck()

func (*Server) UseInMemoryDB added in v0.5.0

func (s *Server) UseInMemoryDB(debugMode bool)

func (*Server) UsePostgresDB added in v0.5.0

func (s *Server) UsePostgresDB(debugMode bool)

Jump to

Keyboard shortcuts

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