This project is a RESTful API implemented in Go, designed to support user management, blog feed creation, and blog feed-follow functionality using PostgreSQL as the database. The project is structured into multiple phases to implement core features incrementally.
- Initialized HTTP Server using the
chipackage for routing. - Health Check Endpoint: A simple health check API to verify that the server is running.
- Configured PostgreSQL:
- Created a local PostgreSQL database.
- Setup database connection URL:
postgres://<your_username>:<your_password>@localhost:5432/<your_db_name>?sslmode=disable
- Configured
sqlc.yaml:- Set up the
queriesandschemadirectories within thesqlfolder.
- Set up the
- Database Connection:
- Established connection with PostgreSQL.
- Configured
connectionMaxIdleTime. - Passed the connection pointer to
handlers/handleDBconn.go.
-
Schema Migration:
- Created file i.e.
001_user.sqlinsql/schemafor creating, dropping and migrating of DB tables the i.e.usertable. This creates / drops table in database (here postgres) - Ran migrations using Goose:
cd sql/schema goose postgres CONN upCONNis the database connection URL excluding thesslmodeflag.
- Created file i.e.
-
Query Generation:
- Created sql query file i.e.
user.sqlin i.e.sql/queries. This generated go compiled query fxns file i.e.users.sql.goin lets sayinternal/databasewhich will be used to call queries fxn from go code. - Generated Go code using:
sqlc generate
- Created sql query file i.e.
POST /createUser: Create a user.GET /getUserByApiKey: Retrieve user by API key.
POST /createFeed: Create a feed for a user.
POST /createFeedFollow: Creating a feed_follow indicates that a user is now following a feed.GET /getAllFeedFollow: Retrieving all feed followed by a user.DELETE /deleteFeedFollow/{feed_follow_id}: Deleting a feed_follow is the same as “unfollowing” a feed.
- Go 1.20+
- PostgreSQL 14+
- Goose CLI
- sqlc CLI
-
Clone the repository:
git clone <repository-url> cd <repository-folder> -
Install dependencies:
go mod tidy -
Configure PostgreSQL and update the connection URL in the
.envfile. -
Run migrations:
cd sql/schema goose postgres CONN up -
Generate query code:
sqlc generate
Start the HTTP server:
go run main.go