httpserver

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ServerDefaultReadTimeout  = 600
	ServerDefaultWriteTimeout = 600
	ServerDefaultPort         = 5000
	ServerDefaultName         = "http"
)
View Source
const (
	// misc server options
	OptAuthTokenHeader        = "authTokenHeader"
	OptAuthTokenSecret        = "authTokenSecret"
	OptDefaultSecurityHeaders = "defaultSecurityHeaders"
)

Variables

This section is empty.

Functions

func NewRouter

func NewRouter(serverName string, debug bool, logger *log.Logger) *gin.Engine

NewRouter creates a new gin router with standardized logging

func ValidateJSON added in v0.3.0

func ValidateJSON(c *gin.Context, obj interface{}) bool

ValidateJSON validates an incoming JSON request against a struct with validation tags Example usage:

type LoginRequest struct {
    Username string `json:"username" binding:"required" validate:"email"`
    Password string `json:"password" binding:"required" validate:"min=8,max=32"`
}

func LoginHandler(c *gin.Context) {
    var req LoginRequest
    if !ValidateJSON(c, &req) {
        return // Validation failed and error response already sent
    }
    // Continue with valid request
}

func ValidateQuery added in v0.3.0

func ValidateQuery(c *gin.Context, obj interface{}) bool

ValidateQuery validates URL query parameters against a struct with validation tags

Types

type OptionsFunc added in v0.3.1

type OptionsFunc func(*Server) error

type Server

type Server struct {
	Config *ServerConfig
	Router *gin.Engine
	Server *http.Server
	Logger *log.Logger
}

func NewServer

func NewServer(cfg *ServerConfig, logger *log.Logger) (*Server, error)

NewServer creates a new http server.

Example usage:

cfg := &ServerConfig{...}
server, err := NewServer(cfg)
if err != nil {
  log.Fatal(err)
}
server.Start()

func (*Server) AddMiddleware

func (s *Server) AddMiddleware(middlewareFunc gin.HandlerFunc)

AddMiddleware adds the specified middleware function to the server's router. The middlewareFunc parameter should be a function that accepts a *gin.Context parameter. The function is added as middleware to the server's router using the Use() method of the gin.Engine. This allows the middleware to be executed for each incoming request before reaching the final handler. Example usage:

server.AddMiddleware(myMiddleware)

func myMiddleware(ctx *gin.Context) {
  // do something before reaching the final handler
  ctx.Next()
  // do something after the final handler
}

Note: The AddMiddleware method is defined on the Server struct which contains a Router field of type gin.Engine.

func (*Server) Group

func (s *Server) Group(relativePath string) *gin.RouterGroup

Group creates a new RouterGroup with the specified relativePath. A RouterGroup is used to group routes together and apply common middleware and settings. The relativePath parameter is the base path for all routes added to the group. The returned value is a pointer to the newly created RouterGroup.

Example usage:

server := &Server{
  // initialize other fields
  Router: gin.New(),
}

group := server.Group("/api") group.GET("/users", getUsers)

This will create a group with the base path "/api" and add a route for GET "/users". All routes added to the group will have the "/api" prefix.

func (*Server) ProcessOptions added in v0.3.1

func (s *Server) ProcessOptions(withOptions ...OptionsFunc) error

ProcessOptions process default options for server

func (*Server) Route

func (s *Server) Route() *gin.Engine

Route returns the gin.Engine instance associated with the Server.

It is used to access the underlying gin.Engine for adding routes and defining middleware.

Example usage:

server := Server{
  // initialize other fields
}
engine := server.Route()
engine.GET("/hello", func(c *gin.Context) {
  // handle request
})

Note: The gin.Engine instance is stored in the Router field of the Server struct.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server by calling the Shutdown method of the underlying httpserver.Server. It takes a context.Context object as a parameter, which can be used to control the shutdown process. The method returns an error if the shutdown process fails.

func (*Server) Start

func (s *Server) Start() error

Start starts the HTTP server of the Server instance. If the Server's TLSConfig is nil, it starts the server using ListenAndServe method of httpserver.Server. Otherwise, it starts the server using ListenAndServeTLS method of httpserver.Server. If the returned error from the server is not http.ErrServerClosed, it is returned. Otherwise, nil is returned.

Usage example:

func (a *SampleApplication) Run() {
    // register http destructor callback
    blueprint.RegisterDestructor(func() error {
        return a.httpServer.Shutdown(a.container.GetContext())
    })

    // Start  application - http server
    a.container.Run(func(app interface{}) error {
        go func() {
            log.Info().Msg(fmt.Sprintf("Running Sample Application API at %s:%d", a.httpServer.Config.Host, a.httpServer.Config.Port))
            a.container.AbortFatal(a.httpServer.Start())
        }()
        return nil
    })
}

func (*Server) UseAuth

func (s *Server) UseAuth(provider auth.Provider)

UseAuth registers an auth middleware

func (*Server) UseCSRFProtection added in v0.3.0

func (s *Server) UseCSRFProtection()

UseCSRFProtection adds CSRF protection to the server

func (*Server) UseDefaultSecurityHeaders added in v0.3.0

func (s *Server) UseDefaultSecurityHeaders()

UseDefaultSecurityHeaders adds default security headers to a server

func (*Server) UseRateLimiting added in v0.3.0

func (s *Server) UseRateLimiting(ratePerMinute int)

UseRateLimiting adds rate limiting middleware to the server ratePerMinute specifies the allowed requests per minute

func (*Server) UseSecurityHeaders added in v0.3.0

func (s *Server) UseSecurityHeaders(config *security.SecurityConfig)

UseSecurityHeaders adds default security headers to a server

func (*Server) UseSession added in v0.3.0

func (s *Server) UseSession(config *session.Config, backend kv.KV, logger *log.Logger) *session.SessionManager

UseSessionWithMemoryStore adds session middleware with provided storage

type ServerConfig

type ServerConfig struct {
	Host         string            `json:"host"`
	Port         int               `json:"port"`
	ReadTimeout  int               `json:"readTimeout"`
	WriteTimeout int               `json:"writeTimeout"`
	Debug        bool              `json:"debug"`
	Options      map[string]string `json:"options"`
	tlsProvider.ServerConfig
}

func NewServerConfig

func NewServerConfig() *ServerConfig

func (*ServerConfig) GetOption

func (c *ServerConfig) GetOption(key string, defaultValue string) string

GetOption retrieves the value associated with the specified key from the Options map of the ServerConfig. If the key exists, the corresponding value is returned. Otherwise, the defaultValue is returned. The Options map is defined as map[string]string in the ServerConfig struct. Example usage:

serverConfig := ServerConfig{
  // initialize other fields
  Options: map[string]string{
    "key1": "value1",
    "key2": "value2",
  },
}
option := serverConfig.GetOption("key1", "default")
// option is "value1"
option := serverConfig.GetOption("key3", "default")
// option is "default"

func (*ServerConfig) NewServer

func (c *ServerConfig) NewServer(logger *log.Logger) (*Server, error)

func (*ServerConfig) Validate

func (c *ServerConfig) Validate() error

type ValidationError added in v0.3.0

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

ValidationError represents a field validation error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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