How zeenox-stack/url-shortener-backend-with-go works
Overall Project Type and Purpose
• This is a backend microservice solely responsible for URL shortening.
• It consists of a single executable, as seen with main.go and its dependencies in go.mod and go.sum.
• The service is designed for handling HTTP requests (both POST for URL creation and GET for redirection) and provides a lightweight in-memory datastore.
Key Components
• Client Applications: Represent users (or any client like cURL, JavaScript Fetch, or Axios) that interact with the service.
• HTTP Server: The Go application running on localhost:8000.
• POST /shorten endpoint – accepts a JSON payload with the URL to be shortened.
• GET /<hashed_key> endpoint – processes redirection to the original URL.
• URL Shortener Service:
• Logic for processing input requests.
• Invokes URL hashing using SHA-256.
• Implements up to 1000 URL storage with automatic reset.
• In-Memory Data Store:
• A URL map used for storing mappings between short keys and original URLs.
• Incorporates sync.Mutex for concurrent access safety.
• (Optional) Maintenance/Reset Handler:
• Although not explicitly detailed, it is noted that storage resets after 1000 URLs. This mechanism could be represented as part of the URL Shortener Service.
Relationships and Interactions
• URL Shortener Service to the In-Memory Data Store, emphasizing that it writes to and reads from this store.
Architectural Patterns and Design Principles
• The service follows a layered architecture:
• Presentation Layer: HTTP request handling.
• Business Logic Layer: URL hashing, validation, URL mapping, and storage management.
• Data Layer: In-memory map.
• Concurrency control is a key design point (via sync.Mutex) – highlighting the importance of thread safety when multiple requests are processed in parallel.