server

package module
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: BSD-3-Clause Imports: 12 Imported by: 0

README

http-server - HTTP Routing, Logging & Telemetry

Documentation

Official godoc documentation (with examples) can be found at the Package Registry.

Usage

Add Package Dependency
go get -u github.com/x-ethr/go-http-server/v2
Import & Implement

main.go

package main

import (
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "log/slog"
    "net/http"
    "os"

    "github.com/x-ethr/go-http-server/v2"
    "github.com/x-ethr/go-http-server/v2/writer"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())

    mux := http.NewServeMux()

    mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
        var response = map[string]interface{}{
            "key": "value",
        }

        w.WriteHeader(http.StatusOK)
        w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(response)
    })

    // Add Response Writer
    handler := writer.Handle(mux)

    // Start the HTTP server
    slog.Info("Starting Server ...", slog.String("local", fmt.Sprintf("http://localhost:%s", "8080")))

    api := server.Server(ctx, handler, "8080")

    // Issue Cancellation Handler
    server.Interrupt(ctx, cancel, api)

    // <-- Blocking
    if e := api.ListenAndServe(); e != nil && !(errors.Is(e, http.ErrServerClosed)) {
        slog.ErrorContext(ctx, "Error During Server's Listen & Serve Call ...", slog.String("error", e.Error()))

        os.Exit(100)
    }

    // --> Exit
    {
        slog.InfoContext(ctx, "Graceful Shutdown Complete")

        // Waiter
        <-ctx.Done()
    }
}

Contributions

See the Contributing Guide for additional details on getting started.

Documentation

Overview

Package server - HTTP Server.

Example
ctx, cancel := context.WithCancel(context.Background())

mux := http.NewServeMux()

mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
	var response = map[string]interface{}{
		"key": "value",
	}

	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(response)
})

// Add Response Writer
handler := writer.Handle(mux)

// Start the HTTP server
slog.Info("Starting Server ...", slog.String("local", fmt.Sprintf("http://localhost:%s", "8080")))

api := server.Server(ctx, handler, "8080")

// Issue Cancellation Handler
server.Interrupt(ctx, cancel, api)

// <-- Blocking
if e := api.ListenAndServe(); e != nil && !(errors.Is(e, http.ErrServerClosed)) {
	slog.ErrorContext(ctx, "Error During Server's Listen & Serve Call ...", slog.String("error", e.Error()))

	os.Exit(100)
}

// --> Exit
{
	slog.InfoContext(ctx, "Graceful Shutdown Complete")

	// Waiter
	<-ctx.Done()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var Health http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
	response := map[string]string{
		"status": "ok",
	}

	w.Header().Set("Content-Type", "Application/JSON")
	w.WriteHeader(http.StatusOK)

	json.NewEncoder(w).Encode(response)

	return
}

Functions

func Interrupt

func Interrupt(ctx context.Context, cancel context.CancelFunc, server *http.Server)

Interrupt is a graceful interrupt + signal handler for an HTTP server.

func Server

func Server(ctx context.Context, handler http.Handler, port string) *http.Server

Server initializes a http.Server.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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