Initial commit: Open sourcing all of the Maple Open Technologies code.

This commit is contained in:
Bartlomiej Mika 2025-12-02 14:33:08 -05:00
commit 755d54a99d
2010 changed files with 448675 additions and 0 deletions

View file

@ -0,0 +1,41 @@
package middleware
import (
"net/http"
"time"
"go.uber.org/zap"
)
// LoggerMiddleware logs HTTP requests
func LoggerMiddleware(logger *zap.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Wrap response writer to capture status code
wrapped := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
next.ServeHTTP(wrapped, r)
duration := time.Since(start)
logger.Info("HTTP request",
zap.String("method", r.Method),
zap.String("path", r.URL.Path),
zap.Int("status", wrapped.statusCode),
zap.Duration("duration", duration),
)
})
}
}
type responseWriter struct {
http.ResponseWriter
statusCode int
}
func (rw *responseWriter) WriteHeader(code int) {
rw.statusCode = code
rw.ResponseWriter.WriteHeader(code)
}

View file

@ -0,0 +1,37 @@
package middleware
import (
"context"
"errors"
"net/http"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/config/constants"
)
// TenantMiddleware extracts tenant ID from JWT session context and adds to context
// This middleware must be used after JWT middleware in the chain
func TenantMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get tenant from JWT session context (set by JWT middleware)
tenantID, ok := r.Context().Value(constants.SessionTenantID).(string)
if !ok || tenantID == "" {
http.Error(w, "tenant context required", http.StatusUnauthorized)
return
}
// Add to context with constants.ContextKeyTenantID for handler access
ctx := context.WithValue(r.Context(), constants.ContextKeyTenantID, tenantID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// GetTenantID retrieves tenant ID from context
func GetTenantID(ctx context.Context) (string, error) {
tenantID, ok := ctx.Value(constants.ContextKeyTenantID).(string)
if !ok || tenantID == "" {
return "", errors.New("tenant_id not found in context")
}
return tenantID, nil
}