hippo

package module
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: MIT Imports: 9 Imported by: 0

README

hippo

The hippo helps servers start up safely and shut down gracefully.

Build Status Go Report Card

Hippo

Image of Yaktocat

Import it in your program as:

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

1. Simple server

Simple server; Example

func main() {
    hippo := hippo.NewHippo(&Server{}, nil)
    if err := hippo.Start(); err != nil {
        panic(err)
    }
}

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

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

func (s *Server) Stop() error {
    return nil
}
hippo has been started
    server has been started
    server has been stopped
hippo has been stopped

2. Normal server

Shutting down the server gracefully; Example

func main() {
    hippo := hippo.NewHippo(&Server{}, nil)
    if err := hippo.Start(); err != nil {
        panic(err)
    }
}

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

func (s *Server) Start() error {
    s.Log.Print("server has been started")

    for {
        // repetitive work
        s.Log.Print("working on it")
        
        // Intentional error
        return errors.New("intentional error")
        
        select {
        case <-s.Ctx.Done(): // for gracefully shutdown
            s.Log.Print("hippo asked me to stop working")
            return nil
        case <-time.After(2 * time.Second):
        }
    }
}

func (s *Server) Stop() error {
    s.Log.Print("server has been stopped")
return nil
}

Output

hippo has been started
  server has been started
    working on it
    an error occurred while the server was running: intentional error
  server has been stopped
hippo has been stopped

3. Server working with HTTP Server

Shutting down the server including HTTP server; Example

Output

hippo has been started
    http server has been started
    server has been started
        repetitive work
        repetitive work
        repetitive work
        received signal, shutting down..
    server has been stopped
    http server has been stopped
hippo has been stopped

4. Multiple servers

Shutting down multiple servers gracefully; Example

Output

hippo has been started
    all servers has been started
        http server has been started
        server-1 has been started
        server-1 has been started
            server-2 is working on it
            server-1 is working on it
            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-2 has been stopped
        server-1 has been stopped
        http server has been stopped
    all server has been stopped
hippo has been stopped

5. Server with Logrus

Example

func main() {
    hippo := hippo.NewHippo(&Server{}, &hippo.Config{
        Logger: logrus.New(),
    })
    if err := hippo.Start(); err != nil {
        panic(err)
    }
}

Documentation

Overview

Package 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
	Logger      StdLogger
}

Config is hippo configuration

type Hippo

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

Hippo helps servers start up safely and shut down gracefully..

func NewHippo

func NewHippo(server Server, config *Config) *Hippo

NewHippo allocates a new server to hippo.

func (*Hippo) Path

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

Path returns absolute directory

func (*Hippo) Start

func (e *Hippo) Start() error

Start starts server and opens error channel.

type Launcher

type Launcher struct {
	Hippo      *Hippo
	Log        StdLogger
	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

type StdLogger

type StdLogger interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
}

Directories

Path Synopsis
examples
http command
multiple command
normal command
simple command

Jump to

Keyboard shortcuts

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