hippo

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: MIT Imports: 10 Imported by: 4

README

hippo

The hippo is an easy, fast, lightweight engine which supports gracefully shutting down the servers.

Build Status Go Report Card

Hippo

Import it in your program as:

import "github.com/devplayg/hippo/v2"

(Would you stop GRACEFULLY?)

Image of Yaktocat

1. Simple server

Simple server; Example

type Server struct {
	hippo.Launcher // DO NOT REMOVE; Launcher links server and engine each other.
}

func (s *Server) Start() error {
	return nil
}

func (s *Server) Stop() error {
	return nil
}
Run
engine := hippo.NewEngine(&SimpleServer{}, nil)
if err := engine.Start(); err != nil {
    panic(err)
}
Debug
engine := hippo.NewEngine(&SimpleServer{}, &hippo.Config{Debug:true})
if err := engine.Start(); err != nil {
    panic(err)
}
Log to a file (powered by logrus)
config := &hippo.Config{
    Debug:  true,
    LogDir: "/var/log/",
}
engine := hippo.NewEngine(&SimpleServer{}, config)
if err := engine.Start(); err != nil {
    s.Log.Error(err)
}

Output structure

engine has been started
    server has been started
    server has been stopped
engine has been stopped

2. Normal server

Shutting down the server gracefully; Example

type Server struct {
    hippo.Launcher // DO NOT REMOVE; Launcher links server and engine each other.
}

func (s *Server) Start() error {
    for {
        // Do your repetitive jobs

        // return errors.New("intentional error")

        select {
        case <-s.Ctx.Done(): // for gracefully shutdown
            s.Log.Debug("server canceled; no longer works")
            return nil
        case <-time.After(2 * time.Second):
        }
    }
}

func (s *Server) Stop() error {
    return nil
}

Output structure

engine has been started                      
    server has been started                      
        server is working on it                      
        server is working on it                      
        server is working on it                      
        received signal, shutting down..             
        server canceled; no longer works             
    server has been stopped                      
engine has been stopped  

3. Server working with HTTP Server

Shutting down the server including HTTP server; Example

Output structure

engine has been started                      
    server has been started                      
        HTTP server has been started
        USER server has been started                 
            server is working on it                      
            server is working on it                      
            received signal, shutting down..
            HTTP server received signal; no longer works
            USER server received signal; no longer works             
        HTTP server has been stopped                 
        USER server has been stopped                 
    server has been stopped                      
engine has been stopped

4. Multiple servers

Shutting down multiple servers gracefully; Example

Output structure

engine has been started
    HTTP server has been started
    server-1 has been started
    server-2 has been started
        server-1 is working on it
        server-2 is working on it
            received signal, shutting down..
        server-2 canceled; no longer works
        server-1 canceled; no longer works
    server-1 has been stopped
    server-2 has been stopped
    HTTP server has been stopped
engine has been stopped

Documentation

Overview

Hippo is an easy, fast, lightweight server framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Name        string
	Description string
	Version     string
	LogDir      string
	Debug       bool
	Trace       bool
	CertFile    string
	KeyFile     string
	Insecure    bool
}

Hippo configuration struct

type Engine

type Engine struct {
	Config *Config
	// contains filtered or unexported fields
}

Engine supports engine framework.

func NewEngine

func NewEngine(server Server, config *Config) *Engine

NewEngine allocates a new server to engine.

func (*Engine) Path added in v2.1.2

func (e *Engine) Path(path string) string

Path returns absolute directory

func (*Engine) Start

func (e *Engine) Start() error

Start starts server and opens error channel.

type Launcher

type Launcher struct {
	Engine     *Engine
	Log        *logrus.Logger
	Ctx        context.Context
	Cancel     context.CancelFunc
	WorkingDir string
}

type Server

type Server interface {
	Start() error
	Stop() error
	// contains filtered or unexported methods
}

Server is the interface that configures the server

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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