This project is a backend service for managing notes with user accounts, built using Golang. It provides functionality for user authentication, note creation, retrieval, updating, and deletion. The service uses PostgreSQL as the database and implements JWT for authentication.
- Go (version 1.x or higher)
- PostgreSQL (version x.x or higher)
-
Clone the repository:
git clone https://github.com/gnotnek/golang-noteapp-backend.git cd golang-noteapp-backend
-
Install dependencies:
go mod download
-
Set up the environment:
- Copy the
.env.example
file to.env
- Update the
.env
file with your PostgreSQL credentials and JWT key:DB_HOST=your_host DB_PORT=your_port DB_USER=your_username DB_NAME=your_database_name DB_PASSWORD=your_password DB_SSLMODE=disable JWT_KEY=your_secret_key
- Copy the
-
Run the project:
go run main.go
The server should now be running on http://localhost:8080
.
The API provides endpoints for user registration, login, and note management. All note-related endpoints require authentication.
To access protected endpoints, include the JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>
- POST
/api/register
- Body:
{ "username": "your_username", "password": "your_password" }
- POST
/api/login
- Body:
{ "username": "your_username", "password": "your_password" }
- Response: JWT token
All note endpoints require authentication.
- POST
/api/notes
- Body:
{ "title": "Note Title", "body": "Note Content", }
- GET
/api/notes
- GET
/api/notes/:id
- PUT
/api/notes/:id
- Body:
{ "title": "Updated Title", "body": "Updated Content" }
- DELETE
/api/notes/:id
The project follows a typical MVC (Model-View-Controller) architecture with some additional layers:
- Models: Define the data structures (User and Notes).
- Controllers: Handle incoming HTTP requests and manage the application logic.
- Services: Implement the business logic and database operations.
- Middleware: Implement authentication checks.
- Routes: Define the API endpoints and connect them to controllers.
- Database: Manages the connection to PostgreSQL using GORM ORM.
- Auth: Handles JWT token creation and verification.
main.go
: Entry point of the application. Initializes the database and sets up the router.routes/route.go
: Defines all API routes and applies middleware.services/postgresql/database.go
: Manages database connection and initialization.services/postgresql/user_service.go
: Implements user-related database operations.services/postgresql/notes_service.go
: Implements note-related database operations.controllers/user_controller.go
: Handles user registration and login.controllers/note_controller.go
: Manages CRUD operations for notes.middleware/auth.go
: Implements JWT authentication middleware and sets the user ID in the context.auth/jwt.go
: Provides functions for creating and verifying JWT tokens, now including the user ID in the claims.models/user_model.go
andmodels/note_model.go
: Define the data structures for users and notes.
The application uses the Gin web framework for handling HTTP requests and GORM as the ORM for database operations. JWT is used for stateless authentication.
The application uses two main tables:
-
Users:
- ID (UUID, primary key)
- Username (string, unique)
- Password (string, hashed)
-
Notes:
- ID (UUID, primary key)
- Title (string)
- Body (string)
- UserID (UUID, foreign key to Users)
The schema is automatically managed by GORM's AutoMigrate feature.
- User registers or logs in, receiving a JWT token.
- The token contains the user's ID (UUID) in its claims.
- For authenticated requests, the client includes the token in the Authorization header.
- The AuthMiddleware verifies the token and sets the user ID in the request context.
- In note creation, the user ID is automatically extracted from the context and associated with the new note.