Skip to content

tiny-go/lite

Repository files navigation

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