A backend service written in Go, providing authentication, user management, and email support for the OpenConnect platform. This backend is containerized, uses PostgreSQL, and supports Google OAuth and SMTP email functionality.
- User authentication via Google OAuth 2.0
- Email support using SMTP (e.g., Gmail)
- PostgreSQL database with migration support via golang-migrate
- Dockerized environment for easy setup and deployment
- Modular Go architecture using
cmd/,internal/, andscripts/ - File upload support
docker-compose up --buildThis will spin up:
openconnect-db: A PostgreSQL 15 databaseopenconnect-api: The Go backend service
Create a .env file based on the following template:
DB_DSN=postgres://openconnect:1234@db:5432/openconnect?sslmode=disable
SMTPPORT=587
SMTPSENDER=example@gmail.com
SMTPHOST=smtp.example.com
SMTPUSERNAME=example@gmail.com
SMTPPASS=your_password_here
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REDIRECT_URL=http://localhost:4000/auth/google/callback
FRONTEND_URL=http://localhost:5173.
├── cmd/api/ # App entrypoint (main.go)
│ ├── app/ # Application logic
│ ├── config/ # Configuration loaders
│ └── server/ # HTTP server logic
├── internal/ # Internal Go modules (auth, mailer, data, etc.)
│ ├── data/ # DB models
│ ├── jsonlog/ # Custom logger
│ └── mailer/ # Email templates and mail service
├── migrations/ # SQL migrations (up/down)
├── scripts/entrypoint.sh # Docker init script (runs migrations)
├── uploads/ # Upload directory
├── Dockerfile # Backend image build
├── docker-compose.yml # Multi-container setup
├── go.mod / go.sum # Go module definitions
└── .env.example # Env templateMigrations are automatically run during container startup via:
migrate -path /migrations -database ${DB_DSN} upYou can also run them manually inside the API container:
docker-compose exec api migrate -path /migrations -database ${DB_DSN} up- Create OAuth credentials at Google Cloud Console
- Use:
- Authorized redirect URI:
http://localhost:4000/auth/google/callback
- Authorized redirect URI:
- Add credentials to
.env
- Enable App Passwords for Gmail.
- Set the following in
.env:
SMTPHOST=smtp.gmail.com
SMTPPORT=587
SMTPUSERNAME=your@gmail.com
SMTPPASS=your_app_password
SMTPSENDER=Your Name <your@gmail.com>Run Locally Without Docker
go run ./cmd/apiMake sure to run migrations manually using:
migrate -path ./migrations -database ${DB_DSN} upgithub.com/julienschmidt/httprouter – HTTP routing
github.com/lib/pq – PostgreSQL driver
golang.org/x/oauth2 – Google OAuth 2.0
github.com/go-mail/mail/v2 – SMTP mailing
entrypoint.sh
This script:
- Waits for PostgreSQL to be ready
- Runs migrations
- Starts the API server
POST /auth/google→ OAuth loginGET /user/profile→ Protected routePOST /mail/send→ Trigger SMTP email
Pull requests are welcome! Please ensure:
- Code is formatted (
gofmt) - You add tests for new features
- You document new env vars or routes
MIT — OpenConnectOUSL