http

package module
v0.0.0-...-c0075a2 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 6 Imported by: 4

README

http server

http server implemented by gin

Usage

package http

import (
	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
	"net/http"
	"testing"
	"time"
)

func TestNewServer(t *testing.T) {
	g := gin.Default()
	g.Handle("GET", "/", func(c *gin.Context) {
		c.String(http.StatusOK, "OK")
	})

	server := NewServer(g, ":8080", func(server *Server) {
		server.ShutdownTimeout = time.Second
	})

	go func() {
		time.Sleep(2 * time.Second)

		resp, err := http.Get("http://localhost:8080")
		assert.NoError(t, err)
		assert.Equal(t, resp.StatusCode, http.StatusOK)

		time.Sleep(1 * time.Second)
		err = server.Shutdown()
		assert.NoError(t, err)
		t.Log("shutdown completed")
	}()

	_ = server.Start()
}

Documentation

Index

Constants

View Source
const DefaultShutdownTimeout = time.Minute

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options func(*Server)

func WithShutdownTimeout

func WithShutdownTimeout(duration time.Duration) Options

WithShutdownTimeout duration of graceful shutdown

type Server

type Server struct {
	ShutdownTimeout time.Duration
	// contains filtered or unexported fields
}

Server gin implement http server

func NewServer

func NewServer(e *gin.Engine, addr string, options ...Options) *Server

NewServer creates a new server instance with default settings

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown shuts down the server and close with graceful shutdown duration

func (*Server) Start

func (s *Server) Start() (err error)

Start to start the server and wait for it to listen on the given address

Jump to

Keyboard shortcuts

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