server

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package server provides HTTP server implementation for memsh, including request handlers, middleware, and server configuration.

The server supports:

  • RESTful API endpoints for shell execution
  • Session management with persistent virtual filesystems
  • Tab completion via POST /complete
  • Session snapshot import/export
  • Health check endpoint
  • Web terminal UI

Index

Constants

View Source
const (
	// MaxRequestBodySize limits the size of incoming JSON request bodies (1 MB).
	MaxRequestBodySize = 1 << 20
	// MinTimeout is the minimum enforced per-request timeout even if --timeout=0.
	MinTimeout = 5 * time.Second
)

Variables

BaseOpts are shell options that should always be applied in server mode.

Functions

func APIKeyMiddleware

func APIKeyMiddleware(next http.Handler, key string, excludedPaths ...string) http.Handler

APIKeyMiddleware enforces Bearer token authentication on all endpoints except those specified in excludedPaths (typically GET / and GET /health).

Clients must include the header: Authorization: Bearer <key>

Returns 401 Unauthorized if the Authorization header is missing or malformed. Returns 403 Forbidden if the API key doesn't match. Allows unauthenticated access to paths in excludedPaths.

func CORSMiddleware

func CORSMiddleware(next http.Handler, allowedOrigin string) http.Handler

CORSMiddleware adds CORS headers for the specified allowed origin. Sets Access-Control-Allow-Origin, Allow-Methods, and Allow-Headers. Responds with 204 No Content to OPTIONS preflight requests.

func New

func New(cfg Config) (*http.Server, error)

New creates a new HTTP server with the given configuration.

The server includes:

  • Middleware chain (auth, security headers, CORS)
  • All route handlers registered
  • Proper timeouts configured

Returns an error if shell config loading fails.

func SecurityHeadersMiddleware

func SecurityHeadersMiddleware(next http.Handler) http.Handler

SecurityHeadersMiddleware adds security headers to all responses:

  • Content-Security-Policy: restricts resource loading to same-origin
  • X-Content-Type-Options: prevents MIME type sniffing
  • X-Frame-Options: prevents clickjacking (DENY all framing)
  • Referrer-Policy: controls referrer information in navigation

func StartCronScheduler

func StartCronScheduler(ctx context.Context, store *session.Store, baseOpts []shell.Option, timeout time.Duration)

StartCronScheduler starts the cron scheduler for the session store. It runs in a background goroutine and should be cancelled via the context when the server shuts down. The scheduler fires every minute for all active sessions, executing cron jobs from their respective /.crontab files.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON writes a JSON response with the given status code. The response body is encoded using json.NewEncoder with Content-Type set to "application/json". Errors during encoding are silently ignored as the headers have already been written.

Types

type Config

type Config struct {
	Addr         string         // TCP address to listen on (e.g., ":8080")
	TTL          time.Duration  // Session idle timeout before reaping
	Timeout      time.Duration  // Per-request execution timeout
	CORSOrigin   string         // CORS allowed origin (empty = no CORS)
	APIKey       string         // API key for Bearer authentication (empty = no auth)
	MaxSessions  int            // Maximum concurrent sessions (0 = unlimited)
	SessionStore *session.Store // Session store for persistence
}

Config holds the HTTP server configuration.

type Handler

type Handler struct {
	Store     *session.Store
	BaseOpts  []shell.Option
	Timeout   time.Duration
	StartTime time.Time
}

Handler dependencies.

func NewHandler

func NewHandler(store *session.Store, baseOpts []shell.Option, timeout time.Duration) *Handler

NewHandler creates a new HTTP handler with the given dependencies.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers all HTTP routes with the given mux.

Jump to

Keyboard shortcuts

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