A social media platform built with Go, featuring user authentication, posts, and social networking capabilities.
- User Management: Create, read, update, and delete user accounts
- Authentication: JWT-based authentication for secure API access
- Posts: Create, read, update, and delete posts with like functionality
- Social Features: Follow/unfollow users, view followers and following lists
- Account Management: Change password and delete account functionality
- Password Security: Hashed passwords using bcrypt for secure storage
Devbook consists of two main components:
- API (
/api): RESTful API server built with Go and Gorilla Mux - Webapp (
/webapp): Web frontend for interacting with the API
- Go 1.26.0
- Gorilla Mux: HTTP router and URL matcher
- MySQL: Database
- JWT: Authentication (dgrijalva/jwt-go)
- Crypto: Password hashing (golang.org/x/crypto)
- Email Validation: badoux/checkmail
- Environment Configuration: joho/godotenv
- Go 1.26.0
- Gorilla Mux: HTTP router
- HTML Templates: Server-side rendering
- Environment Configuration: joho/godotenv
- Go 1.26.0+ (for webapp) and Go 1.26.0+ (for api)
- MySQL 5.7+
- Git
git clone <repository-url>
cd devbook# Start your MySQL server
mysql -u root -p < api/sql/sql.sql
# If needed, load seed data
mysql -u root -p devbook < api/sql/data.sqlAlternatively, if you have Docker:
docker run --name mysql-devbook -e MYSQL_ROOT_PASSWORD=root -d -p 3306:3306 mysql:latest
mysql -h 127.0.0.1 -u root -p -e "source api/sql/sql.sql"Create or update /api/.env with your configuration:
DB_USERNAME=golang
DB_PASSWORD=golang
DB_NAME=devbook
API_PORT=5001
SECRET_KEY=<your-secret-key>The default SECRET_KEY is provided in .env, but for production, generate a new one:
# Generate a secure random key (run from project root)
# Then base64 encode it and add to .envCreate or update /webapp/.env with your configuration:
API_URL=http://localhost:5001
APP_PORT=3000
HASH_KEY=<your-hash-key>
BLOCK_KEY=<your-block-key>The HASH_KEY and BLOCK_KEY are used for secure cookie encryption. You can generate random keys:
# Use any secure random key generation method and base64 encodecd api
go mod download
go run main.goThe API will listen on http://localhost:5001
cd webapp
go mod download
go run main.goThe webapp will listen on http://localhost:3000
In separate terminal windows:
# Terminal 1 - API
cd api && go run main.go
# Terminal 2 - Webapp
cd webapp && go run main.goPOST /login- Authenticate user and receive JWT token
POST /users- Create a new userGET /users- Get all users (requires auth)GET /users/{userId}- Get user details (requires auth)PUT /users/{userId}- Update user profile (requires auth)DELETE /users/{userId}- Delete user account (requires auth)POST /users/{userId}/update-password- Change user password (requires auth)
POST /users/{userId}/follow- Follow a user (requires auth)POST /users/{userId}/unfollow- Unfollow a user (requires auth)GET /users/{userId}/followers- Get user's followers (requires auth)GET /users/{userId}/following- Get users the user is following (requires auth)
POST /posts- Create a new post (requires auth)GET /posts- Get all posts (requires auth)GET /posts/{postId}- Get post details (requires auth)PUT /posts/{postId}- Update a post (requires auth)DELETE /posts/{postId}- Delete a post (requires auth)GET /users/{userId}/posts- Get posts by a specific user (requires auth)POST /posts/{postId}/like- Like a post (requires auth)POST /posts/{postId}/unlike- Unlike a post (requires auth)
- id: Primary key (auto-increment)
- name: User's full name
- nick: Unique username
- email: Unique email address
- password: Hashed password
- created_at: Account creation timestamp- id: Primary key (auto-increment)
- title: Post title
- content: Post content
- author_id: Foreign key to users table
- likes: Number of likes
- created_at: Post creation timestamp- user_id: User being followed
- follower_id: User who is following
- Primary key: (user_id, follower_id)Create a Dockerfile for the API:
FROM golang:1.26.0-alpine AS builder
WORKDIR /app
COPY . .
WORKDIR /app/api
RUN go build -o api .
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/api/api .
COPY api/.env .
CMD ["./api"]Build and run:
docker build -t devbook-api .
docker run -p 5001:5001 --env-file api/.env devbook-api- Environment Variables: Use secure secret management (AWS Secrets Manager, HashiCorp Vault, etc.)
- Database: Use managed MySQL service (AWS RDS, Google Cloud SQL, etc.)
- HTTPS: Use a reverse proxy (Nginx, Caddy) with SSL certificates
- Logging: Implement structured logging for production monitoring
- Error Handling: Set appropriate error logging and alerting
- Rate Limiting: Consider implementing rate limiting on API endpoints
- CORS: Configure CORS appropriately for your domain
devbook/
├── api/
│ ├── src/
│ │ ├── config/ # Configuration loading
│ │ ├── controllers/ # Request handlers
│ │ ├── models/ # Data structures
│ │ ├── repositories/ # Database operations
│ │ ├── router/ # Route definitions
│ │ ├── middlewares/ # Authentication & logging
│ │ ├── security/ # Security utilities
│ │ ├── db/ # Database connection
│ │ └── responses/ # Response utilities
│ ├── sql/ # Database setup
│ ├── main.go
│ └── go.mod
│
├── webapp/
│ ├── src/
│ │ ├── config/ # Configuration loading
│ │ ├── router/ # Route definitions
│ │ ├── templates/ # HTML templates
│ │ ├── cookies/ # Cookie management
│ │ └── utils/ # Utility functions
│ ├── main.go
│ └── go.mod
│
├── README.md
└── .gitignore
cd api
go test ./...
cd ../webapp
go test ./...Example using curl:
# Create a user
curl -X POST http://localhost:5001/users \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","nick":"johndoe","email":"john@example.com","password":"secure123"}'
# Login
curl -X POST http://localhost:5001/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"secure123"}'
# Use the returned token in subsequent requests
curl -X GET http://localhost:5001/users \
-H "Authorization: Bearer <token>"- Verify MySQL is running and accessible
- Check credentials in
.envfile match your MySQL setup - Ensure the database exists and tables are created
# Find process using port 5001 (API)
lsof -i :5001
# Find process using port 3000 (Webapp)
lsof -i :3000
# Kill the process (macOS/Linux)
kill -9 <PID>- Ensure SECRET_KEY in
/api/.envis set and matches between API instances - Check that JWT tokens aren't expired
- Verify token is being sent in Authorization header correctly
[Add your license information here]
[Add contribution guidelines here]