pkg

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxFileSize       = 100 * 1024 * 1024 // 100MB
	MaxRequestSize    = 110 * 1024 * 1024 // 110MB to account for multipart form data
	RateLimitRequests = 100               // requests per minute
	// Time to keep IP rate limiters in memory
	RateLimitWindow = 1 * time.Hour
)
View Source
const (
	DefaultAddr         = ":80"
	DefaultReadTimeout  = 30 * time.Second
	DefaultWriteTimeout = 30 * time.Second
	DefaultIdleTimeout  = 120 * time.Second
	ShutdownTimeout     = 30 * time.Second
)

Server configuration constants

Variables

View Source
var (
	ErrInvalidCredentials = errors.New("invalid credentials")
	ErrAuthFailed         = errors.New("authentication failed")
)

Common errors

View Source
var (
	AllowedFileExtensions = []string{
		".txt", ".pdf", ".doc", ".docx", ".xls", ".xlsx",
		".jpg", ".jpeg", ".png", ".gif", ".zip", ".csv",
	}
	DisallowedPaths = []string{
		"..", "//", "\\", "%2e", "%2f",
	}
)
View Source
var (
	ErrEmptyBucket   = fmt.Errorf("bucket name cannot be empty")
	ErrEmptyUserID   = fmt.Errorf("userID cannot be empty")
	ErrEmptyFilename = fmt.Errorf("filename cannot be empty")
	ErrNilContent    = fmt.Errorf("content cannot be nil")
	ErrNilContext    = fmt.Errorf("context cannot be nil")
)

Common errors

View Source
var (
	ErrInvalidFilename = fmt.Errorf("invalid filename")
)

Common errors

View Source
var (
	ErrMissingEnvVar = errors.New("required environment variable not set")
)

Common errors

View Source
var RequiredEnvVars = []string{
	"AUTH_API_URL",
	"S3_BUCKET",
}

RequiredEnvVars lists the environment variables that must be set

Functions

func RateLimiter

func RateLimiter(next http.Handler) http.Handler

RateLimiter implements a per-IP rate limiting middleware

func RequestLogger

func RequestLogger(next http.Handler) http.Handler

RequestLogger logs all incoming requests

func RequestSizeLimiter

func RequestSizeLimiter(next http.Handler) http.Handler

RequestSizeLimiter limits the size of incoming requests

func SecurityHeaders

func SecurityHeaders(next http.Handler) http.Handler

SecurityHeaders adds security-related HTTP headers to the response

func ShutdownRateLimiter

func ShutdownRateLimiter()

ShutdownRateLimiter gracefully shuts down the rate limiter

func ValidateFilePath

func ValidateFilePath(path string) error

ValidateFilePath checks if the file path is safe and allowed

Types

type AuthResult

type AuthResult struct {
	Authenticated bool
	UserID        string
	Username      string
}

AuthResult represents the result of an authentication attempt

type Authenticator

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

Authenticator handles user authentication against an external API

func NewAuthenticator

func NewAuthenticator() *Authenticator

NewAuthenticator creates a new authenticator with the configured auth URL

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(ctx context.Context, username, password string) (AuthResult, error)

Authenticate validates user credentials against the authentication API. It returns an AuthResult containing the authentication status and user details. If authentication fails, it returns an appropriate error.

type AuthenticatorInterface

type AuthenticatorInterface interface {
	Authenticate(ctx context.Context, username, password string) (AuthResult, error)
}

Authenticator defines the interface for authentication operations

type S3Client

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

S3Client handles S3 operations

func NewS3Client

func NewS3Client(ctx context.Context, bucket string) (*S3Client, error)

NewS3Client creates a new S3 client with the provided bucket

func (*S3Client) UploadFile

func (c *S3Client) UploadFile(ctx context.Context, userID string, filename string, content io.Reader) error

UploadFile uploads a file to S3 with the given userID and filename. The file will be stored in the "incoming/{userID}/{filename}" path.

type S3Interface

type S3Interface interface {
	UploadFile(ctx context.Context, userID string, filename string, content io.Reader) error
}

S3Interface defines the interface for S3 operations

type Server

type Server struct {
	Addr         string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
	Handler      http.Handler
}

Server represents the WebDAV server configuration

func NewServer

func NewServer() (*Server, error)

NewServer creates a new WebDAV server with default configuration. It loads environment variables and initializes all required components.

func (*Server) ListenAndServe

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

ListenAndServe starts the WebDAV server with graceful shutdown support

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface for direct usage in other HTTP servers

type WebDAVHandler

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

WebDAVHandler handles WebDAV requests, supporting only PUT operations for file uploads to S3 storage.

func NewWebDAVHandler

func NewWebDAVHandler(authenticator AuthenticatorInterface, s3Client S3Interface) *WebDAVHandler

NewWebDAVHandler creates a new WebDAV handler with the given authenticator and S3 client. The handler only supports PUT operations, all other operations return 405 Method Not Allowed.

func (*WebDAVHandler) ServeHTTP

func (h *WebDAVHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface. It handles authentication and routes PUT requests to handlePut.

Jump to

Keyboard shortcuts

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