api

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package api provides HTTP REST API functionality for the Scanorama network scanner. It implements endpoints for scanning, discovery, host management, and system status.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host              string        `yaml:"host" json:"host"`
	Port              int           `yaml:"port" json:"port"`
	ReadTimeout       time.Duration `yaml:"read_timeout" json:"read_timeout"`
	WriteTimeout      time.Duration `yaml:"write_timeout" json:"write_timeout"`
	IdleTimeout       time.Duration `yaml:"idle_timeout" json:"idle_timeout"`
	MaxHeaderBytes    int           `yaml:"max_header_bytes" json:"max_header_bytes"`
	EnableCORS        bool          `yaml:"enable_cors" json:"enable_cors"`
	CORSOrigins       []string      `yaml:"cors_origins" json:"cors_origins"`
	RateLimitEnabled  bool          `yaml:"rate_limit_enabled" json:"rate_limit_enabled"`
	RateLimitRequests int           `yaml:"rate_limit_requests" json:"rate_limit_requests"`
	RateLimitWindow   time.Duration `yaml:"rate_limit_window" json:"rate_limit_window"`
	AuthEnabled       bool          `yaml:"auth_enabled" json:"auth_enabled"`
	APIKeys           []string      `yaml:"api_keys" json:"api_keys"`
}

Config holds API server configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns default API server configuration.

type ErrorResponse

type ErrorResponse struct {
	Error     string    `json:"error"`
	Timestamp time.Time `json:"timestamp"`
	RequestID string    `json:"request_id,omitempty"`
}

ErrorResponse represents a standard API error response.

type PaginatedResponse

type PaginatedResponse struct {
	Data       interface{} `json:"data"`
	Pagination struct {
		Page       int   `json:"page"`
		PageSize   int   `json:"page_size"`
		TotalItems int64 `json:"total_items"`
		TotalPages int   `json:"total_pages"`
	} `json:"pagination"`
}

PaginatedResponse represents a paginated API response.

type PaginationParams

type PaginationParams struct {
	Page     int `json:"page"`
	PageSize int `json:"page_size"`
	Offset   int `json:"offset"`
}

PaginationParams represents pagination parameters.

type Server

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

Server represents the API server.

func New

func New(cfg *config.Config, database *db.DB) (*Server, error)

New creates a new API server instance.

func (*Server) GetAddress

func (s *Server) GetAddress() string

GetAddress returns the server address.

func (*Server) GetPaginationParams

func (s *Server) GetPaginationParams(r *http.Request) PaginationParams

GetPaginationParams extracts pagination parameters from request.

func (*Server) GetQueryParam

func (s *Server) GetQueryParam(r *http.Request, key, defaultValue string) string

GetQueryParam gets a query parameter with optional default value.

func (*Server) GetQueryParamBool

func (s *Server) GetQueryParamBool(r *http.Request, key string, defaultValue bool) bool

GetQueryParamBool gets a boolean query parameter with optional default value.

func (*Server) GetQueryParamInt

func (s *Server) GetQueryParamInt(r *http.Request, key string, defaultValue int) (int, error)

GetQueryParamInt gets an integer query parameter with optional default value.

func (*Server) GetRouter

func (s *Server) GetRouter() *mux.Router

GetRouter returns the configured router.

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning checks if the server is running.

func (*Server) ParseJSON

func (s *Server) ParseJSON(r *http.Request, v interface{}) error

ParseJSON parses JSON request body into the provided struct.

func (*Server) Start

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

Start starts the API server.

func (*Server) Stop

func (s *Server) Stop() error

Stop gracefully stops the API server.

func (*Server) ValidateRequest

func (s *Server) ValidateRequest(r *http.Request) error

ValidateRequest performs basic request validation.

func (*Server) WriteJSON

func (s *Server) WriteJSON(w http.ResponseWriter, r *http.Request, statusCode int, data interface{})

WriteJSON writes a JSON response.

func (*Server) WritePaginatedResponse

func (s *Server) WritePaginatedResponse(
	w http.ResponseWriter,
	r *http.Request,
	data interface{},
	params PaginationParams,
	totalItems int64,
)

WritePaginatedResponse writes a paginated response.

Directories

Path Synopsis
Package handlers provides HTTP request handlers for the Scanorama API.
Package handlers provides HTTP request handlers for the Scanorama API.
Package middleware provides HTTP middleware functions for the Scanorama API server.
Package middleware provides HTTP middleware functions for the Scanorama API server.

Jump to

Keyboard shortcuts

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