dbrapi0.smarti.me – Backend API Server
Overview
dbrapi0.smarti.me is a Node.js backend API server designed to manage activation codes, user management, voting, language localization, and integration with external services. The project is structured for modularity, maintainability, and scalability, with clear separation between routing, business logic, database access, and utility functions.
Table of Contents
- Project Structure
- Folder and File Descriptions
- API Endpoints
- Database Models
- Configuration
- Startup and Deployment
- Example Data Flow
- Utility and Legacy Scripts
- Architecture Diagrams (Mermaid)
- Contributing
- License
Project Structure
.
├── apis/
├── controllers/
├── middleware/
├── models/
├── storagecontroller/
├── .env
├── dbrApiServer0.js
├── dbrApiServer0.log
├── package.json
├── startapi0
├── z
├── zdbConnect-backup.js
├── zdrb-test-api.php
Folder and File Descriptions
Root Directory
-
.env
Environment variables for database connections, API keys, and configuration. -
dbrApiServer0.js
Main entry point. Sets up the HTTP server, parses requests, and routes them to controllers. -
dbrApiServer0.log
Log file for server output and errors. -
package.json
Node.js project manifest (dependencies, scripts, metadata). -
startapi0
Shell script to start the server withnodemonand redirect logs. -
z
Shell script to compress the log file. -
zdbConnect-backup.js
Utility for database connection and queries (backup or testing). -
zdrb-test-api.php
Legacy PHP script for testing or integration.
apis/
Backend controllers for interacting with external APIs or backend services.
-
updateVABackendController.js
Sends HTTPS POST requests to update Voting Assistant (VA) status. -
userByPartnerIdBackendController.js
Handles user-related backend API calls by partner ID. -
webAppBackendController.js
Sends vote requests to the web app backend. -
webAppPositionBackendController.js
Handles position-related backend API calls. -
webAppVABackendController.js
Sends VA-related requests to the web app backend.
controllers/
Route handlers for each API endpoint.
-
activateId8Code.js
Handles activation of ID8 codes. -
activateId8CodeDetails5b9e3c2.js
Handles activation with additional details. -
activateId8CodePartnerIdDetails.js
Handles activation with partner ID and details. -
activateId8CodeWithDetails.js
Handles activation with more detailed information. -
getAllLast2Rows.js
Fetches the last two rows from a data source. -
getApps.js
Returns a list of applications. -
getLangsAll.js
Returns all language records. -
storeLangs.js
Handles storing and updating language records. -
storeAppLangs.js
Handles storing and updating application-specific language records. -
getPPRRP.js, getPRRP.js
Fetches position or public data with redundancy/retry logic. -
getVASwitch.js, getVASwitchRRP.js
Handles VA switch state retrieval. -
updateVASwitch.js, updateVASwitchVPH.js
Handles updating VA switch state. -
updateVoteUTCs.js
Handles updating votes with UTC timestamps. -
getPublicData.js, getPublicDataRRP.js
Fetches public data, possibly with redundancy/retry.
middleware/
Reusable logic for request processing, validation, and utility functions.
-
activationEmailStore.js
Handles storing and updating email/UDID pairs, generating unique codes, and logging. -
requestCounter.js
Tracks API request counts. -
requestRetryCounter.js
Tracks API retry counts.
models/
Database access logic for various tables.
-
dbQuery.js
Core database query functions. -
mDPartners.js
Handles queries and updates for thed_partnerstable (e.g., voting assistant status). -
mDAppLoginCodeLog.js
Handles logging of app login codes and email/UDID pairs. -
mApiLog.js
Handles API logging, supports multiple log tables. -
mDbrPP.js
Handles queries for position and public data. -
mLangs.js
Handles language records, including backup, replace, and app-specific language management. -
mAppBeApiRequests.js
Tracks backend API requests per minute. -
mDbrAppBeApiProcess.js
Tracks backend API processing times. -
mDbrAppBeApiRR.js
Tracks backend API retry requests.
storagecontroller/
Handles file storage and retrieval.
- readJson.js
Reads JSON files from disk, used for serving public data.
API Endpoints
Each controller in the controllers/ folder typically exposes one or more endpoints.
Example endpoint documentation:
POST /activateId8Code
- Description: Activates an ID8 code for a user.
- Request Body:
email(string): User emailudid(string): Device UDIDpartnerId(string, optional): Partner identifier
- Response:
status(string): Success or errorcode(string): Activation code
- Errors:
400: Missing parameters500: Internal server error
(Repeat for each endpoint as needed.)
Database Models
Models in the models/ folder encapsulate all database access.
-
mDPartners.js:
- Table:
d_partners - Operations: Query/update voting assistant status, partner info.
- Table:
-
mDAppLoginCodeLog.js:
- Table:
d_app_login_code_log - Operations: Log app login codes, email/UDID pairs.
- Table:
-
mLangs.js:
- Tables:
langs,langs_backup,app_langs - Operations: Manage language records, backup/restore, app-specific languages.
- Tables:
-
mApiLog.js:
- Tables:
api_log,api_log2, ... - Operations: Log API requests and responses.
- Tables:
(Repeat for each model as needed.)
Configuration
.env Example
DB_HOST=localhost
DB_USER=myuser
DB_PASS=mypassword
DB_NAME=mydatabase
API_KEY=your_api_key_here
LOG_PATH=./dbrApiServer0.log
-
DB_HOST, DB_USER, DB_PASS, DB_NAME:
Database connection settings. -
API_KEY:
Key for authenticating with external services. -
LOG_PATH:
Path to the server log file.
Startup and Deployment
-
Install dependencies:
npm install -
Configure environment:
- Copy
.env.exampleto.envand fill in your values.
- Copy
-
Start the server:
./startapi0 -
Log rotation:
- Use the
zscript to compress logs:./z
- Use the
Example Data Flow
- Client sends a request to
/activateId8Code. - Server (
dbrApiServer0.js) routes the request tocontrollers/activateId8Code.js. - Controller validates input, uses
middleware/activationEmailStore.jsto store email/UDID, and callsmodels/mDAppLoginCodeLog.jsto log the activation. - If needed, controller calls an external API via
apis/userByPartnerIdBackendController.js. - Response is sent back to the client, and the action is logged in
models/mApiLog.js.
Utility and Legacy Scripts
-
zdbConnect-backup.js:
Standalone script for database connection and queries, useful for testing or backup. -
zdrb-test-api.php:
Legacy PHP script for testing API endpoints or database connectivity.
Architecture Diagrams (Mermaid)
Project Structure
High-Level Architecture
Request Flow
Database Interaction
API Endpoint Routing
External API Integration
Contributing
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request with a clear description of your changes.
License
This project is licensed under the MIT License.