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 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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