Categorygithub.com/tiny-go/lite
modulepackage
1.0.1
Repository: https://github.com/tiny-go/lite.git
Documentation: pkg.go.dev

# README

lite

GoDoc License Build Status Report Card GoCover

Simple tool for building RESTful APIs

Installation

go get -u github.com/tiny-go/lite

Modules

You can use BaseModule that provides basic module functionality (such as register/unregister/list) or write your own implementation. BaseModule already contains Register, Unregister and Controllers methods and implements Module interface.

Controllers

Any golang func, struct or custom type can be used as a controller provided that it implements Controller interface and has some action methods, such as Get/GetAll/Post/PostAll/... (check the entire list in interfaces.go).

Dependencies

If you need to pass some dependencies (like config, database connection etc) to your module/controller use handler.Map(dep), it will be passed to the module/controller (use struct tag inject:"true" in front of the struct fields that should be injected). Take a look at example folder for more information (for instance example/auth/user/controller.go).

Usage

package main

import (
	"log"
	"net/http"

	"github.com/tiny-go/lite"
	// register codecs
	"github.com/tiny-go/codec/driver"
	_ "github.com/tiny-go/codec/driver/json"
	_ "github.com/tiny-go/codec/driver/xml"
)

func main() {
	// set default codec
	driver.Default("application/json")
	// create new handler
	handler := lite.NewHandler()
	// map dependencies
	handler.Map(depA)
	handler.Map(depB)
	handler.Map(depC)
	// register modules
	handler.Use(aliasOne, moduleA)
	handler.Use(aliasTwo, moduleB)
	// start HTTP server
	log.Fatal(http.ListenAndServe(":8080", handler))
}

# Packages

No description provided by the author

# Functions

GorillaParams extracts URI params from the request (when using gorolla/mux).
Modules iterates all registered modules applying provided func.
NewBaseModule is a constructor func for BaseModule.
NewHandler creates new HTTP handler.
ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.
Register makes module available with provided alias.

# Structs

BaseModule contains a basic set of logic and provides basic operations on resources (like "Register", "Unregister" etc).

# Interfaces

Controller interface is a bare minimal controller.
Handler interface describes HTTP API handler.
Module represents single module with lite API (it means that all its routes can be generated only once at startup).
PluralDeleter should be able to delete a list of models.
PluralGetter should be able to provide a list of available models.
PluralPatcher should be able to patch a list of models.
PluralPoster should be able to store a list of model to the storage.
PluralPutter should be able to update a list of models.
SingleDeleter should be able to delete a single model by primary key(s).
SingleGetter should be able to provide a single model by primary key(s).
SinglePatcher should be able to patch a single model by primary key(s).
SinglePoster should be able to store a single model to the storage.
SinglePutter should be able to update a single model by primary key(s).

# Type aliases

Methods is a collection of HTTP methods.
Params is a key/value map containing the request parameters from the URI.