af

package module
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2021 License: BSD-2-Clause Imports: 13 Imported by: 0

README

What is AF

The name of AF comes from air refueling.

AF is a package of Go.

You can use the AF write a graceful reload HTTP server easily.

Support

  • *nix
  • Windows (only build and run)

Features

  • graceful reload
  • graceful stop
  • custom signal handler

Install

go get -u github.com/ailncode/af
#or
go mod edit -require=github.com/ailncode/af@latest

Usage

  1. Import the AF
import "github.com/ailncode/af"
  1. Simple use
package main

import(
	"fmt"
	"net/http"
	"github.com/ailncode/af"
)

func main(){
    http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request) {
		fmt.Fprintln(w, "ok")
	})
    refuel := af.Default()
    //refuel.Addr = ":8080"
    //:8080 is the default Addr
    //refuel.Handler = http.DefaultServeMux
    //http.DefaultServeMux is the default HTTP handler
    //refuel.ShutdownTimeOut = time.Second * 10
    //time.Second * 10 is the default ShutdownTimeOut
    refuel.Run()
    //You can check error in here.
}
  1. Reload & Stop your server
#Reload
kill -USR2 <pid>
#Stop
kill -INT <pid> #or kill -TERM <pid>
  1. Use your custom Listen Address Handler ShutdownTimeOut
package main

import(
    "net/http"
    "time"
    "github.com/ailncode/af"
    "github.com/gin-gonic/gin"
)

func main(){
    //Use handler like *gin.Engine
    router :=gin.Default()
	router.GET("/", func(context *gin.Context) {
		context.String(http.StatusOK,"ok")
	})
    refuel := af.Default()
    refuel.Handler = router
    //You can use the below code also.
    //af := af.NewWithHandler(r)
    refuel.Addr = ":80"
    refuel.ShutdownTimeOut = time.Second * 10
    refuel.Run()
}
  1. Use your custom system signal handler
package main

import (
	"github.com/ailncode/af"
	"syscall"
	"time"
)

func main() {
	refuel := af.Default()
	refuel.ShutdownTimeOut = time.Second * 10
	refuel.HandleSignal(func(r *af.AF) {
		r.Stop()
	}, syscall.SIGINT, syscall.SIGTERM)
	refuel.HandleSignal(func(r *af.AF) {
		r.Reload()
		//You can check error in here
	}, syscall.SIGUSR2)
	refuel.Run()
}
  1. Use your custom server
package main

import (
	"af"
	"net/http"
)

func main() {
	server := &http.Server{
		Addr:"8080",
		Handler:http.DefaultServeMux,
		//...
	}
	refuel := af.NewWithServer(server)
	refuel.Run()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AF

type AF struct {
	Addr            string
	Handler         http.Handler
	ShutdownTimeOut time.Duration
	// contains filtered or unexported fields
}

func Default

func Default() *AF

Default Get default AF

func NewWithHandler

func NewWithHandler(handler http.Handler) *AF

NewWithHandler Get AF with a http handler

func NewWithServer

func NewWithServer(server *http.Server) *AF

NewWithServer Get AF with a http server

func (*AF) HandleSignal

func (af *AF) HandleSignal(handler func(*AF), signals ...os.Signal)

HandleSignal Give a handler for signals

func (*AF) Reload

func (af *AF) Reload() error

Reload the AF

func (*AF) Run

func (af *AF) Run() error

Run the AF

func (*AF) Stop

func (af *AF) Stop()

Stop the AF

Jump to

Keyboard shortcuts

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