wait

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

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

Go to latest
Published: Oct 3, 2017 License: MIT Imports: 2 Imported by: 0

README

WaitServerStart

Coverage Status Build Status Go Report Card GitHub license GoDoc

Simple waiter of server start

func main() {
	// Start a server
	srv, address := startHttpServer()

	// Wait starting of server
	<-wait.ByAddress(address)

	// Testing
	resp, err := http.Get(address)
	if err != nil {
		fmt.Println("RESPONSE")
		panic(err)
	}
	fmt.Println("Server is run...")
	_ = resp.Body.Close()
	if err := srv.Shutdown(context.Background()); err != nil {
		panic(err)
	}

	// Output: Server is run...
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByAddress

func ByAddress(address string) <-chan struct{}

ByAddress - wait of starting server

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	wait "github.com/Konstantin8105/WaitServerStart"
)

func startHttpServer() (*http.Server, string) {
	port := 8090
	address := fmt.Sprintf("http://127.0.0.1:%d", port)

	srv := &http.Server{Addr: fmt.Sprintf(":%d", port)}

	go func() {
		time.Sleep(5 * time.Millisecond)
		_ = srv.ListenAndServe()
	}()

	return srv, address
}

func main() {
	// Start a server
	srv, address := startHttpServer()

	// Wait starting of server
	<-wait.ByAddress(address)

	// Testing
	resp, err := http.Get(address)
	if err != nil {
		fmt.Println("RESPONSE")
		panic(err)
	}
	fmt.Println("Server is run...")
	_ = resp.Body.Close()
	if err := srv.Shutdown(context.Background()); err != nil {
		panic(err)
	}

}
Output:

Server is run...

Types

This section is empty.

Jump to

Keyboard shortcuts

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