How sustatov027-max/url_shortener works
Purpose and Entry Point
A Go HTTP API for creating and resolving shortened URLs, backed by PostgreSQL. The process starts in cmd/app/main.go; generated Swagger artifacts are embedded alongside it in cmd/app/docs/docs.go.
Composition Root and Runtime
cmd/app/main.go loads configuration, initializes PostgreSQL, constructs repository/service/handler layers, and starts the HTTP server. Container deployment is supported by Dockerfile and docker-compose.yml; environment inputs are documented by .env.example.
HTTP/API Boundary
Routing and request/response handling live in internal/handler/router.go and internal/handler/url_handler.go. This boundary owns HTTP semantics—route dispatch, input decoding/validation, status codes, and Swagger-facing API behavior—while delegating URL operations to the service layer.
URL Application Service
internal/service/url_service.go implements the core use cases: accepting an original URL, generating or assigning its short identifier, persisting the mapping, and resolving a short identifier back to its destination. It is the business boundary between transport handlers and persistence, and depends on repository abstractions rather than PostgreSQL directly.
Persistence Abstraction and PostgreSQL Adapter
internal/repository/repository.go defines the storage-facing contract used by services. PostgreSQL-specific connection setup is in internal/repository/postgres/postgres.go, with URL mapping queries in internal/repository/postgres/url.go. PostgreSQL is the material external dependency and durable source of URL mappings.
Identifier Generation
pkg/generator/generator.go provides short-code generation independently of HTTP and database concerns. The URL service consumes this capability when creating mappings; generated values must cooperate with persistence uniqueness rules to prevent collisions.
Schema Lifecycle and API Documentation
migrations/000001_init_schema.up.sql and migrations/000001_init_schema.down.sql define creation and rollback of the database schema. API documentation is published as generated OpenAPI/Swagger files at cmd/app/docs/swagger.yaml and cmd/app/docs/swagger.json, exposing the handler contract to clients.
Primary Flow
Create flow: HTTP request → internal/handler/url_handler.go → internal/service/url_service.go → pkg/generator/generator.go plus repository interface → internal/repository/postgres/url.go → PostgreSQL. Resolve flow reverses the lookup path: request handler → service → repository/PostgreSQL → HTTP redirect or response. Configuration is isolated in internal/config/config.go, keeping environment and connection details outside domain flow.
Main entry points: main.go Application bootstrap · url_handler.go URL handler · url_service.go URL service · url.go URL mapping queries
Components 16
Runtime
- Application bootstrap · Composes dependencies and starts the HTTP server.
cmd/app/main.go - Configuration · Loads environment and connection settings.
internal/config/config.go - Container deployment
Dockerfile - Compose stack
docker-compose.yml
HTTP API
- HTTP router
internal/handler/router.go - URL handler · Owns request validation, responses, and redirects.
internal/handler/url_handler.go
Application
- URL service · Creates mappings and resolves short identifiers.
internal/service/url_service.go - Short-code generator
pkg/generator/generator.go
Persistence
- Repository contract · Persistence boundary consumed by the service.
internal/repository/repository.go - PostgreSQL setup
internal/repository/postgres/postgres.go - URL mapping queries
internal/repository/postgres/url.go - PostgreSQL · Durable source of URL mappings.
Schema & Docs
- Schema migration · Creates the URL mapping schema.
migrations/000001_init_schema.up.sql - Schema rollback · Rolls back the initial schema.
migrations/000001_init_schema.down.sql - OpenAPI documents · Published handler contract for API clients.
cmd/app/docs/swagger.yaml - Embedded Swagger
cmd/app/docs/docs.go
Connections 17
- Application bootstrap → Configuration: loads
- Application bootstrap → PostgreSQL setup: initializes
- Application bootstrap → HTTP router: starts
- HTTP router → URL handler: dispatches requests
- URL handler → URL service: create / resolve
- URL service → Short-code generator: generates code
- URL service → Repository contract: persists / looks up
- URL mapping queries → Repository contract: implements
- PostgreSQL setup → URL mapping queries: provides connection
- URL mapping queries → PostgreSQL: queries mappings
- Schema migration → PostgreSQL: creates schema
- Schema rollback → PostgreSQL: rolls back schema
- Embedded Swagger → OpenAPI documents: generated from
- URL handler → OpenAPI documents: documents contract
- Container deployment → Application bootstrap: runs
- Compose stack → Container deployment: orchestrates
- Compose stack → PostgreSQL: provisions