useragent

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/poly-gun/go-middleware/middleware/useragent"
)

func main() {
	// Define a mux to handle + define routes.
	mux := http.NewServeMux()
	mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		ctx := r.Context()

		// Usage of the middleware's context value.
		value := useragent.Value(ctx)

		datum := map[string]interface{}{
			"user-agent": value,
		}

		defer json.NewEncoder(w).Encode(datum)

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

	// Wrap the mux instance with the user-agent middleware.
	server := httptest.NewServer(useragent.New().Handler(mux))

	defer server.Close()

	client := server.Client()
	request, e := http.NewRequest(http.MethodGet, server.URL, nil)
	if e != nil {
		e = fmt.Errorf("unexpected error while generating request: %w", e)

		panic(e)
	}

	// Set a user-agent as the request from a go httptest server instance includes a potentially non-deterministic value (e.g. "Go-http-client/1.1").
	request.Header.Set("User-Agent", "Go-HTTP-Testing-Client")

	response, e := client.Do(request)
	if e != nil {
		e = fmt.Errorf("unexpected error while generating response: %w", e)

		panic(e)
	}

	defer response.Body.Close()

	body, e := io.ReadAll(response.Body)
	if e != nil {
		e = fmt.Errorf("unexpected error while reading response body: %w", e)

		panic(e)
	}

	fmt.Println(string(body))

}
Output:

{"user-agent":"Go-HTTP-Testing-Client"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() middleware.Configurable[Options]

New creates a new instance of the Server middleware, implementing middleware.Configurable. If Server.Settings isn't called, then the Server.Handler function will hydrate the middleware's configuration with sane default(s) if applicable.

func Value

func Value(ctx context.Context) (agent string)

Value retrieves context value for the following package's middleware.

Types

type Options

type Options struct {
	// Level specifies whether a log message should be logged in the [Server] middleware component's [Server.Handler] function. Default is nil. A value of nil
	// causes the [Server.Handler] to skip logging of the user-agent header entirely. See the [slog.Leveler] interface for additional information.
	Level slog.Leveler
}

Options represents the configuration settings for the Server middleware component.

type Server

type Server struct {
	middleware.Configurable[Options]
	// contains filtered or unexported fields
}

Server represents a middleware component that applies configurable Options settings to HTTP requests. It embeds middleware.Configurable for Options configuration.

func (*Server) Handler

func (s *Server) Handler(next http.Handler) http.Handler

Handler applies middleware settings to modify the request context and set response headers. It forwards the request to the next handler in the chain.

func (*Server) Settings

func (s *Server) Settings(configuration ...func(o *Options)) middleware.Configurable[Options]

Settings applies configuration functions to modify the Server middleware's Options and returns the updated middleware instance.

Jump to

Keyboard shortcuts

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