chiclientip

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: MIT Imports: 3 Imported by: 1

README

chiclientip Build Status

Getting started

Installation
go get gitea.dwysokinski.me/Kichiyaki/chiclientip
Usage
package main

import (
	"log"
	"net/http"

	"gitea.dwysokinski.me/Kichiyaki/chiclientip"
	"github.com/go-chi/chi/v5"
	"github.com/realclientip/realclientip-go"
)

func main() {
	stratXForwardedFor, err := realclientip.NewRightmostNonPrivateStrategy(http.CanonicalHeaderKey("X-Forwarded-For"))
	if err != nil {
		log.Fatalln("realclientip.NewRightmostNonPrivateStrategy", err)
	}
	strat := realclientip.NewChainStrategy(
		stratXForwardedFor,
		realclientip.RemoteAddrStrategy{},
	)

	router := chi.NewRouter()
	router.Use(chiclientip.ClientIP(strat))
	router.Get("/", func(w http.ResponseWriter, r *http.Request) {
		clientIP, _ := chiclientip.ClientIPFromContext(r.Context())
		_, _ = w.Write([]byte(clientIP))
	})

	http.ListenAndServe(":8080", router)
}

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Dawid Wysokiński - [email protected]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(strategy realclientip.Strategy) func(next http.Handler) http.Handler

ClientIP is a go-chi middleware that derives the client IP from realclientip.Strategy and stores it in the http.Request Context.

func ClientIPFromContext

func ClientIPFromContext(ctx context.Context) (string, bool)

ClientIPFromContext returns the client ip in context.Context if exists.

Types

This section is empty.

Jump to

Keyboard shortcuts

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