api

package
v0.0.0-...-63bdbfa Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package api sets up the HTTP server, configures middleware and routes

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestToIP

func RequestToIP(r *http.Request) (string, error)

RequestToIP finds client IP address from an HTTP request. HTTP Request RemoteAddr provides us with the address of the client or last proxy, therefore we apply some logic to provide more reliable results

  1. First we try to get IP from the 'X-Forwarded-For' header
  2. If the header is set, then we get the first IP in this list: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
  3. If the header is not set - fall back to RemoteAddr

Potential improvements depending on requirements are:

  • Add configuration to ignore provided ip ranges
  • Add configuration to ignore 'X-Forwarded-For' header (probably we dont want to trust 'X-Forwarded-For' from public internet)

Types

type Middleware

type Middleware struct {
	DB  *db.DBConnection
	Log *logr.Logger
}

Middleware object contains configuration for the RequestMiddleware

func (*Middleware) RequestMiddleware

func (a *Middleware) RequestMiddleware(next http.Handler) http.Handler

RequestMiddleware assigns a unique ID to each request and verifies that IP address is not blacklisted.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server stores HTTP server configuration

func NewServer

func NewServer(listenAddr string, dbConn *db.DBConnection, mailer *m.Mailer, logger *logr.Logger, certPath, certKeyPath *string) *Server

NewServer creates a new HTTP server listenAddr must be in format: "<INTERFACE_ADDRESS>:<PORT>"

func (*Server) Start

func (s *Server) Start() chan error

Start an instance of HTTP server

example usage:

ch := server.Start()
err = <-ch
if err != nil {
	panic(err.Error())
}

Directories

Path Synopsis
Package handler implements handlers for HTTP routes
Package handler implements handlers for HTTP routes

Jump to

Keyboard shortcuts

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