togo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2019 License: MIT Imports: 10 Imported by: 0

README

togo

Build Status GoDoc codecov Go Report Card Open Source Helpers

This package was inspired in gizmo from The New York Times and it offer a simple way to build your microservices with no external dependencies in Go.

Installation

go get github.com/opentogo/togo

Examples

  • They are available in the examples directory.
  • There are also examples within the GoDoc.

Log

It implements the Apache combined log format with application name as prefix.

[togo-example-service] Running at :3000
[togo-example-service] ::1 - - [10/Oct/2019:17:19:14 +0000] "GET /svc/togo HTTP/1.1" 404 19 "-" "curl/7.54.0" 0.0000
[togo-example-service] ::1 - - [10/Oct/2019:17:19:21 +0000] "GET /svc/togo/cats HTTP/1.1" 200 39 "-" "curl/7.54.0" 0.0001

Contributors

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Logger is the global logger for the server.
	Logger = log.New(os.Stdout, "", 0)
)

Functions

This section is empty.

Types

type Config

type Config struct {
	HTTPAddr     string `json:"http_addr"`
	HTTPPort     int    `json:"http_port"`
	IdleTimeout  int    `json:"idle_timeout"`
	ReadTimeout  int    `json:"read_timeout"`
	WriteTimeout int    `json:"write_timeout"`
	LogFilename  string `json:"log_filename"`
}

Config holds info required to configure a togo.server.

func LoadJSONFile

func LoadJSONFile(filename string) (config Config)

LoadJSONFile attempts to read a specified JSON file by provided filename. It then attempts to unmarshal the JSON data into a Config type object. It returns a populated Config or any errors it encountered during the JSON file load and parse.

type Resource

type Resource struct {
	Path    string
	Method  string
	Handler http.HandlerFunc
}

Resource holds info required for configuring endpoints and resources for the server.

func (Resource) SanitizedPath

func (r Resource) SanitizedPath(prefix string) string

SanitizedPath trims forward slash characters from the provided string prefix and Path of the resource. Returns the sanitized path string.

type Service

type Service interface {
	Prefix() string
	Middleware(http.HandlerFunc) http.HandlerFunc
	Resources() []Resource
}

Service is the basic interface that defines what to expect from any service.

type Togo

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

Togo is a microservice application server. To use, you create an instance, passing a name and a configuration. You then register services to your instance, and then call `Run()`. Example:

t := togo.Init("my-togo", togo.LoadJSONFile("config.json"))
t.Register(myservice.New())

if err := t.Run(); err != nil {
    fmt.Fprintf(os.Stderr, "%v", err)
    os.Exit(1)
}

func Init

func Init(appName string, config Config) *Togo

Init will set up the name, logging, and the HTTP server.

func (*Togo) Register

func (t *Togo) Register(service Service)

Register adds the specified togo.Service to the Togo server.

func (Togo) Run

func (t Togo) Run() error

Run will start the HTTP server for Togo.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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