A robust Go-based REST API service for the ATFI (Event Commitment Platform) that handles user profiles, event management, and check-in functionality with blockchain integration.
- User Profile Management: Create, read, and update user profiles with wallet addresses
- Event Management: Complete event lifecycle with on-chain and off-chain data
- Blockchain Integration: Ethereum/Base Sepolia integration for smart contract interactions
- Check-in System: QR code-based event check-in and validation
- PostgreSQL Database: Reliable data persistence with pgx driver
- CORS Support: Cross-origin resource sharing for frontend integration
- Health Checks: Database and service health monitoring
- Go 1.21+ - Install Go
- PostgreSQL - Install PostgreSQL
- Ethereum Node - Base Sepolia RPC endpoint (default provided)
git clone <repository-url>
cd backendgo mod downloadCopy the example environment file:
cp .env.example .envEdit .env with your configuration:
# Database Configuration
DATABASE_URL=postgres://user:password@localhost/atfi_db?sslmode=disable
# Server Configuration
PORT=8080
# Ethereum RPC Configuration
RPC_URL=https://base-sepolia-rpc.publicnode.comCreate a PostgreSQL database:
CREATE DATABASE atfi_db;The application will automatically create the necessary tables on first run.
go run main.gogo build -o atfi-backend main.go
./atfi-backendInstall Air for live development:
go install github.com/cosmtrek/air@latest
airhttp://localhost:8080/api/v1
GET /health
GET /api/v1/test-db
POST /api/v1/profiles
Content-Type: application/json
{
"wallet_address": "0x...",
"name": "John Doe",
"email": "john@example.com"
}GET /api/v1/profiles/{walletAddress}PUT /api/v1/profiles/{walletAddress}
Content-Type: application/json
{
"name": "Updated Name",
"email": "updated@example.com"
}POST /api/v1/profiles/upsert
Content-Type: application/json
{
"wallet_address": "0x...",
"name": "John Doe",
"email": "john@example.com"
}POST /api/v1/events
Content-Type: application/json
{
"event_id": 1,
"title": "Amazing Event",
"description": "Event description",
"location": "Event location",
"image_url": "https://example.com/image.jpg",
"is_public": true,
"require_approval": false,
"organizer_address": "0x..."
}GET /api/v1/events?page=1&limit=10&status=REGISTRATION_OPEN&organizer=0x...GET /api/v1/events/{eventId}PUT /api/v1/events/{eventId}/status
Content-Type: application/json
{
"status": "REGISTRATION_OPEN"
}POST /api/v1/events/{eventId}/settle
Content-Type: application/json
{
"attended_participants": ["0x...", "0x..."]
}GET /api/v1/events/{eventId}/attendedPOST /api/v1/events/{eventId}/register
Content-Type: application/json
{
"userAddress": "0x...",
"transactionHash": "0x..." // Optional for smart contract registration
}GET /api/v1/events/{eventId}/registration?user=0x...POST /api/v1/checkin
Content-Type: application/json
{
"event_id": 1,
"user_address": "0x...",
"validator_address": "0x...",
"qr_data": "{\"eventId\":\"1\",\"userAddress\":\"0x...\"}"
}POST /api/v1/checkin/validate
Content-Type: application/json
{
"qr_data": "{\"eventId\":\"1\",\"userAddress\":\"0x...\"}",
"validator_address": "0x..."
}GET /api/v1/events/{eventId}/checkinsThe ATFI platform uses PostgreSQL as its primary database with the following actual schema:
User profile information linked to wallet addresses.
Columns:
id(UUID, Primary Key) - Unique identifier for the profile (managed externally)wallet_address(Text, Unique, Not Null) - Ethereum wallet addressname(Text, Not Null) - Display name of the useremail(Text, Unique, Nullable) - Email address for notifications
Constraints:
- Primary key on
id - Unique constraint on
wallet_address - Unique constraint on
email(if provided)
On-chain event data synchronized from smart contracts.
Columns:
event_id(Bigint, Primary Key) - Unique event identifier from smart contractvault_address(Text, Not Null, Unique) - Address of the vault contract for this eventorganizer_address(Text, Not Null) - Event organizer's wallet addressstake_amount(Numeric, Not Null) - Required stake amount (PostgreSQL numeric type for precision)max_participant(Bigint, Not Null) - Maximum number of participants allowedregistration_deadline(Numeric, Not Null) - Registration close time (timestamp as numeric)event_date(Numeric, Not Null) - Event start time (timestamp as numeric)
Constraints:
- Primary key on
event_id - Unique constraint on
vault_address(ensures one vault per event)
Off-chain event metadata for display purposes.
Columns:
event_id(Bigint, Primary Key, Foreign Key) - Referencesevents_onchain.event_idtitle(Text, Not Null) - Event titledescription(Text, Nullable) - Detailed event descriptionimage_url(Text, Nullable) - Event banner/thumbnail URLstatus(USER-DEFINED, Not Null) - Event status (custom PostgreSQL enum type)
Status Values: The status uses a PostgreSQL user-defined enum type that includes values like:
REGISTRATION_OPEN- Accepting participant registrationsREGISTRATION_CLOSED- No longer accepting registrationsLIVE- Event is currently activeSETTLED- Event completed and rewards distributedVOIDED- Event cancelled (emergency case)
Constraints:
- Primary key on
event_id - Foreign key constraint to
events_onchain.event_id
Core participant tracking table (note: singular table name in actual schema).
Columns:
id(UUID, Primary Key) - Unique participant record identifier (auto-generated)event_id(Bigint, Not Null, Unique) - Referencesevents_onchain.event_iduser_id(UUID, Not Null, Unique) - Referencesprofiles.idis_attend(Boolean, Not Null, Default: false) - Whether participant attended the eventis_claim(Boolean, Not Null, Default: false) - Whether participant has claimed their rewards
Important Constraints:
- Unique constraint on
event_id- Only ONE participant record per event - Unique constraint on
user_id- Only ONE event participation per user at a time - Foreign key constraints to
events_onchainandprofiles
profiles (1) ←→ (1) participant ←→ (1) events_onchain
↓ ↓
events_metadata
Important Relationship Notes:
- The
participanttable enforces a 1:1 relationship between events and users - Each event can only have one participant record
- Each user can only participate in one event at a time
- This design suggests events are structured for single-participant scenarios or specific business logic
-- Core user profiles table
CREATE TABLE public.profiles (
id uuid NOT NULL,
wallet_address text NOT NULL UNIQUE,
name text NOT NULL,
email text UNIQUE,
CONSTRAINT profiles_pkey PRIMARY KEY (id)
);
-- On-chain event data
CREATE TABLE public.events_onchain (
event_id bigint NOT NULL,
vault_address text NOT NULL UNIQUE,
organizer_address text NOT NULL,
stake_amount numeric NOT NULL,
max_participant bigint NOT NULL,
registration_deadline numeric NOT NULL,
event_date numeric NOT NULL,
CONSTRAINT events_onchain_pkey PRIMARY KEY (event_id)
);
-- Event metadata for display
CREATE TABLE public.events_metadata (
event_id bigint NOT NULL,
title text NOT NULL,
description text,
image_url text,
status USER-DEFINED NOT NULL,
CONSTRAINT events_metadata_pkey PRIMARY KEY (event_id),
CONSTRAINT events_metadata_event_id_fkey FOREIGN KEY (event_id) REFERENCES public.events_onchain(event_id)
);
-- Participant tracking (1:1 relationship)
CREATE TABLE public.participant (
id uuid NOT NULL DEFAULT gen_random_uuid(),
event_id bigint NOT NULL UNIQUE,
user_id uuid NOT NULL UNIQUE,
is_attend boolean NOT NULL DEFAULT false,
is_claim boolean NOT NULL DEFAULT false,
CONSTRAINT participant_pkey PRIMARY KEY (id),
CONSTRAINT participant_event_id_fkey FOREIGN KEY (event_id) REFERENCES public.events_onchain(event_id),
CONSTRAINT participant_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);Unique Design Patterns:
- Single Participant per Event: The unique constraint on
event_idin theparticipanttable means each event can only have one participant record - Exclusive Event Participation: The unique constraint on
user_idmeans a user cannot participate in multiple events simultaneously - External ID Management: Profile IDs are managed externally (not auto-generated)
- High Precision Numbers: Using PostgreSQL
numerictype for financial amounts ensures exact precision - Custom Status Types: Event status uses a user-defined enum for type safety
Business Logic Implications:
- This schema suggests a different event model than typical multi-participant events
- May be designed for one-on-one events, consultations, or exclusive experiences
- The constraints enforce exclusivity and prevent conflicts in participation
The application expects this exact schema structure. The table creation order is:
profiles- User profiles (no dependencies)events_onchain- Event data (no dependencies)events_metadata- Referencesevents_onchainparticipant- References bothevents_onchainandprofiles
Given the unique constraints:
- The schema naturally limits scale due to 1:1 relationships
- Indexes on foreign keys provide good join performance
- Unique constraints provide fast lookups for event and user validation
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgres://user:password@localhost/atfi_db?sslmode=disable |
PORT |
Server port | 8080 |
RPC_URL |
Ethereum RPC URL | https://base-sepolia-rpc.publicnode.com |
By default, the API allows requests from:
http://localhost:3000http://localhost:3001http://localhost:3002
go test ./...go test ./handlers
go test ./modelsgo test -cover ./...FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o main .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]version: '3.8'
services:
db:
image: postgres:15
environment:
POSTGRES_DB: atfi_db
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
api:
build: .
ports:
- "8080:8080"
environment:
DATABASE_URL: postgres://user:password@db:5432/atfi_db?sslmode=disable
depends_on:
- dbThe application uses Go's standard log package with structured messages for:
- Database connections
- Ethereum client connections
- API requests and responses
- Error conditions
/health- Basic service health check/api/v1/test-db- Database connectivity test
- Input validation using Gin's binding features
- SQL injection prevention with parameterized queries
- CORS configuration for cross-origin requests
- Environment variable protection (not logged)
- Ethereum transaction validation
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
# Check PostgreSQL is running
pg_isready -h localhost -p 5432
# Verify database exists
psql -h localhost -U postgres -l- Verify RPC URL is accessible
- Check network connectivity
- Ensure Base Sepolia is the correct network
# Find process using port 8080
lsof -i :8080
# Kill process
kill -9 <PID>- Check the application logs for detailed error messages
- Verify all environment variables are set correctly
- Ensure database schema is up to date
- Test API endpoints using a tool like Postman or curl
- Connection pooling with pgx
- Efficient JSON handling with gin
- Optimized database queries
- Minimal memory footprint
Built with ❤️ using Go, Gin, PostgreSQL, and Ethereum