gohttp

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: GPL-3.0 Imports: 22 Imported by: 4

Documentation

Index

Constants

View Source
const (
	MIMEAppJSON            = "application/json"
	MIMEHtml               = "text/html"
	MIMEAppJSONCharsetUTF8 = MIMEAppJSON + "; " + charsetUTF8
	HeaderContentType      = "Content-Type"
)

Variables

This section is empty.

Functions

func GetHandlerNotFound

func GetHandlerNotFound(l golog.MyLogger, rootPathNotFoundCounter prometheus.Counter) http.HandlerFunc

func GetHealthHandler

func GetHealthHandler(l golog.MyLogger) http.HandlerFunc

func GetInfoHandler added in v0.3.1

func GetInfoHandler(s *Server) http.HandlerFunc

func GetLoginPostHandler added in v0.3.1

func GetLoginPostHandler(s *Server) http.HandlerFunc

func GetReadinessHandler

func GetReadinessHandler(l golog.MyLogger) http.HandlerFunc

func GetStaticPageHandler added in v0.3.1

func GetStaticPageHandler(title string, description string, l golog.MyLogger) http.HandlerFunc

func GetTimeHandler

func GetTimeHandler(l golog.MyLogger) http.HandlerFunc

func TraceRequest

func TraceRequest(handlerName string, r *http.Request, l golog.MyLogger)

func WaitForHttpServer

func WaitForHttpServer(listenAddress string, waitDuration time.Duration, numRetries int)

WaitForHttpServer attempts to establish a TCP connection to listenAddress in a given amount of time. It returns upon a successful connection; otherwise exits with an error.

Types

type AppInfo added in v0.3.1

type AppInfo struct {
	App        string `json:"app"`
	Version    string `json:"version"`
	Repository string `json:"repository"`
	Build      string `json:"build"`
}

type Authentication added in v0.3.1

type Authentication interface {
	AuthenticateUser(user, passwordHash string) bool
	GetUserInfoFromLogin(login string) (*UserInfo, error)
}

func NewSimpleAdminAuthenticator added in v0.3.1

func NewSimpleAdminAuthenticator(mainAdminUser, mainAdminPassword, mainAdminEmail string, mainAdminId int, jwtCheck JwtChecker) Authentication

NewSimpleAdminAuthenticator Function to create an instance of SimpleAdminAuthenticator

type JwtChecker added in v0.3.1

type JwtChecker interface {
	ParseToken(jwtToken string) (*JwtCustomClaims, error)
	GetTokenFromUserInfo(userInfo *UserInfo) (*jwt.Token, error)
	JwtMiddleware(next http.Handler) http.Handler
	GetLogger() golog.MyLogger
	GetJwtDuration() int
	GetIssuerId() string
}

func NewJwtChecker added in v0.3.1

func NewJwtChecker(secret, issuer, subject string, duration int, l golog.MyLogger) JwtChecker

NewJwtChecker creates a new JwtChecker

type JwtCustomClaims added in v0.3.1

type JwtCustomClaims struct {
	jwt.RegisteredClaims
	User *UserInfo
}

JwtCustomClaims are custom claims extending default ones.

func GetJwtCustomClaimsFromContext added in v0.3.1

func GetJwtCustomClaimsFromContext(r *http.Request) *JwtCustomClaims

GetJwtCustomClaimsFromContext returns the JWT Custom claims from the received request with context

type JwtInfo added in v0.3.1

type JwtInfo struct {
	Secret   string `json:"secret"`
	Duration int    `json:"duration"`
	IssuerId string `json:"issuer_id"`
	Subject  string `json:"subject"`
	// contains filtered or unexported fields
}

func (*JwtInfo) GetIssuerId added in v0.3.1

func (ji *JwtInfo) GetIssuerId() string

func (*JwtInfo) GetJwtDuration added in v0.3.1

func (ji *JwtInfo) GetJwtDuration() int

func (*JwtInfo) GetLogger added in v0.3.1

func (ji *JwtInfo) GetLogger() golog.MyLogger

func (*JwtInfo) GetTokenFromUserInfo added in v0.3.1

func (ji *JwtInfo) GetTokenFromUserInfo(userInfo *UserInfo) (*jwt.Token, error)

func (*JwtInfo) JwtMiddleware added in v0.3.1

func (ji *JwtInfo) JwtMiddleware(next http.Handler) http.Handler

func (*JwtInfo) ParseToken added in v0.3.1

func (ji *JwtInfo) ParseToken(jwtToken string) (*JwtCustomClaims, error)

type PrometheusMiddleware added in v0.3.1

type PrometheusMiddleware interface {
	// WrapHandler wraps the given HTTP handler
	WrapHandler(handlerName string, handler http.Handler) http.HandlerFunc
}

func NewPrometheusMiddleware added in v0.3.1

func NewPrometheusMiddleware(registry prometheus.Registerer, buckets []float64) PrometheusMiddleware

NewPrometheusMiddleware returns a PrometheusMiddleware interface.

type Server

type Server struct {
	RootPathGetCounter  prometheus.Counter
	PathNotFoundCounter prometheus.Counter

	Authenticator Authentication
	JwtCheck      JwtChecker
	VersionReader VersionReader
	// contains filtered or unexported fields
}

Server is a struct type to store information related to all handlers of web server

func CreateNewServerFromEnvOrFail added in v0.3.1

func CreateNewServerFromEnvOrFail(
	defaultPort int,
	defaultServerIp string,
	myAuthenticator Authentication,
	myJwt JwtChecker,
	myVersionReader VersionReader,
	l golog.MyLogger,
) *Server

CreateNewServerFromEnvOrFail creates a new server from environment variables or fails

func NewGoHttpServer

func NewGoHttpServer(listenAddress string, Auth Authentication, JwtCheck JwtChecker, Ver VersionReader, logger golog.MyLogger) *Server

NewGoHttpServer is a constructor that initializes the server mux (routes) and all fields of the Server type

func (*Server) AddRoute

func (s *Server) AddRoute(pathPattern string, handler http.Handler)

AddRoute adds a handler for this web server

func (*Server) GetLog

func (s *Server) GetLog() golog.MyLogger

GetLog returns the log of this web server

func (*Server) GetPrometheusRegistry added in v0.3.1

func (s *Server) GetPrometheusRegistry() *prometheus.Registry

GetPrometheusRegistry returns the Prometheus registry of this web server

func (*Server) GetRouter

func (s *Server) GetRouter() *http.ServeMux

GetRouter returns the ServeMux of this web server

func (*Server) GetStartTime

func (s *Server) GetStartTime() time.Time

GetStartTime returns the start time of this web server

func (*Server) JsonResponse

func (s *Server) JsonResponse(w http.ResponseWriter, result interface{}) error

func (*Server) StartServer

func (s *Server) StartServer()

StartServer initializes all the handlers paths of this web server, it is called inside the NewGoHttpServer constructor

type SimpleAdminAuthenticator added in v0.3.1

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

SimpleAdminAuthenticator Create a struct that will implement the Authentication interface

func (*SimpleAdminAuthenticator) AuthenticateUser added in v0.3.1

func (sa *SimpleAdminAuthenticator) AuthenticateUser(userLogin, passwordHash string) bool

AuthenticateUser Implement the AuthenticateUser method for SimpleAdminAuthenticator

func (*SimpleAdminAuthenticator) GetUserInfoFromLogin added in v0.3.1

func (sa *SimpleAdminAuthenticator) GetUserInfoFromLogin(login string) (*UserInfo, error)

GetUserInfoFromLogin Get the JWT claims from the login User

type SimpleVersionWriter added in v0.3.1

type SimpleVersionWriter struct {
	Version AppInfo
}

SimpleVersionWriter Create a struct that will implement the VersionReader interface

func NewSimpleVersionReader added in v0.3.1

func NewSimpleVersionReader(app, ver, repo, build string) *SimpleVersionWriter

NewSimpleVersionReader is a constructor that initializes the VersionReader interface

func (SimpleVersionWriter) GetVersionInfo added in v0.3.1

func (s SimpleVersionWriter) GetVersionInfo() AppInfo

GetVersionInfo returns the version information of the application.

type UserInfo added in v0.3.1

type UserInfo struct {
	UserId    int    `json:"user_id"`
	UserName  string `json:"user_name"`
	UserEmail string `json:"user_email"`
	UserLogin string `json:"user_login"`
	IsAdmin   bool   `json:"is_admin"`
}

UserInfo are custom claims extending default ones.

type VersionReader added in v0.3.1

type VersionReader interface {
	GetVersionInfo() AppInfo
}

Jump to

Keyboard shortcuts

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