Sign inSign up

lecroissant2003/fms-backend

By lecroissant2003

Updated 11 days ago

FMS (Factory Management System) backend image. Includes API, WebSocket and OAuth2 integration.

Buildkit cache
Image
Languages & frameworks
API management
Web servers
0

4.3K

lecroissant2003/fms-backend repository overview

Factory Management System (FMS) - Backend Microservice

High-performance, asynchronous Django 5 REST API & WebSocket backend powering the Factory Management System (FMS). Built with Django REST Framework, Django Channels, Redis caching/channel layer, and Google OAuth2.


🏗️ Architecture & Core Modules

The backend is composed of domain-driven Django applications:

  • main: User management, custom authentication, role-based permissions (Admin, Manager, Supervisor).
  • core: Organization structure, departments, and workshop locations.
  • labor: Employee skill matrix, work shifts, and labor tracking.
  • project: Project lifecycles, milestone tracking, and task assignments.
  • product: Product catalogs, manufacturing processes, and BOM (Bill of Materials).
  • inventory: Suppliers, raw materials, stock levels, and consumption tracking.
  • production: Production lines, machine states, and manufacturing schedules.

🔐 Google OAuth2 & Authentication Flow

  • Pre-Registered Account Enforcement: Uses a CustomSocialAccountAdapter extending django-allauth. Automatic signup is disabled (SOCIALACCOUNT_AUTO_SIGNUP = False). Only existing system users with matching Google emails are allowed access; unauthenticated attempts return HTTP 403 with administrator instructions.
  • JWT Token Management: Powered by rest_framework_simplejwt & dj-rest-auth.
    • Token Rotation: ROTATE_REFRESH_TOKENS = True
    • Blacklisting: TOKEN_BLACKLIST_ENABLED = True
    • Lifetimes: 30-minute access and refresh tokens.
    • Endpoints: /api/token/, /api/token/refresh/, /api/token/blacklist/, /api/rest-auth/google/.

⚡ WebSockets & Asynchronous Messaging

  • ASGI Stack: Uses ProtocolTypeRouter (backend.asgi.application) combining HTTP and WebSocket protocols.
  • Handshake Authentication: Custom JWTAuthMiddleware and ConsumerBlock validate SimpleJWT access tokens upon initial WebSocket handshake connection.
  • Channel Layer: Backed by channels_redis.core.RedisChannelLayer for scalable multi-node message broadcasting.
  • Real-Time Security: Performs server-side DRF permission checks (check_object_permission) on WebSocket message payloads before sending updates to connected clients.

📡 Event Listening & Django Signals

  • Automated Data Sync: Implements a generic model change factory create_model_change_signal listening on post_save and post_delete model signals.
  • Real-Time Serialization: Automatically serializes model instance mutations (created, updated, deleted) using DRF serializers.
  • Live Push: Broadcasts events asynchronously (async_to_sync) over Redis Channel Layer groups directly to active frontend WebSocket listeners.

🚀 Backend Caching & Session Storage

Configured using django-redis with ZlibCompressor compression and JSONSerializer:

  1. Default Cache DB (redis://.../1):
    • Low-level object and view cache (KEY_PREFIX: fms).
    • Default TTL: 300 seconds.
    • Max connection pool size: 20 connections.
    • Resilience: IGNORE_EXCEPTIONS = True ensures graceful fallback to standard database queries if Redis is offline.
  2. Session Storage DB (redis://.../2):
    • SESSION_ENGINE = "django.contrib.sessions.backends.cache"
    • Dedicated Redis logical database (KEY_PREFIX: fms-session).
    • Session TTL: 24 hours (86400 seconds).

📊 Observability & Health Probes

  • Metrics Scrape: Integrated django_prometheus middleware exposing Prometheus metrics at /metrics.
  • Health Check: Dedicated probe endpoint at /api/health/ handled by HealthCheckMiddleware (bypasses ALLOWED_HOSTS for Kubernetes / Docker compose probes).
  • Execution Logging: RequestTimeLoggingMiddleware records execution latency in JSON format to /app/logs/ and outputs colorized rich console logs during development.

🛠️ Environment Variables Reference

VariableDescriptionDefault
SECRET_KEYDjango application secret keyRequired
DEBUGEnable debug modeFalse
ALLOWED_HOSTSComma-separated allowed hostnameslocalhost,127.0.0.1
DATABASE_ENGINEDatabase driver (postgresql_psycopg2 / sqlite3)sqlite3
REDIS_HOSTRedis cache hostname127.0.0.1
REDIS_PORTRedis cache port6379
GOOGLE_CLIENT_IDOAuth2 Google Client IDRequired for auth
GOOGLE_CLIENT_SECRETOAuth2 Google Client SecretRequired for auth
CORS_ORIGINSAllowed frontend CORS domainshttp://localhost:3000

Tag summary

Content type

Image

Digest

sha256:1191ddbc1

Size

52 MB

Last updated

11 days ago

docker pull lecroissant2003/fms-backend