🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

github.com/0xOthmane/golang_projects/rss_aggregator

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/0xOthmane/golang_projects/rss_aggregator

v0.0.0-20230712092434-daf184200806
Source
Go
Version published
Created
Source
go mod init github.com/0xOthmane/golang_projects/rss_aggregator
go get github.com/joho/godotenv
go mod tidy
go mod vendor
go build && ./rss_aggregator

Use of chi lightweight, idiomatic and composable router for building Go HTTP services

go get github.com/go-chi/chi
go get github.com/go-chi/cors
go mod vendor
go mod tidy
go mod vendor

Create a Router and a Server (port:8080 defined in .env file).

Define helper functions that write an HTTP response with: status code, JSON body, content type: application/json.

Test the helper function with sending a request (GET/POST/..) to http://localhost:8080/v1/healthz. Or Specify what type of request you allow by changing v1Router.HandleFunc() to v1Router.Get()

Setup Postgresql and pgAdmin with Docker

sudo mkdir -p /var/lib/postgresql/data
docker pull postgres
docker run -itd -e POSTGRES_USER=USERNAME -e POSTGRES_PASSWORD=PASSWORD -p 5432:5432 -v /data:/var/lib/postgresql/data --name postgresql postgres
docker pull dpage/pgadmin4
docker run --name pgadmin -p 5051:80 -e "PGADMIN_DEFAULT_EMAIL=YOUR_EMAIL" -e "PGADMIN_DEFAULT_PASSWORD=YOUR_PASSWORD" -d dpage/pgadmin4

Tools

  • database/sql: This is part of Go's standard library. It provides a way to connect to a SQL database, execute queries, and scan the results into Go types.
  • sqlc: SQLC is an Go program that generates Go code from SQL queries. It's not exactly an ORM, but rather a tool that makes working with raw SQL almost as easy as using an ORM.
  • Goose: Goose is a database migration tool written in Go. It runs migrations from the same SQL files that SQLC uses, making the pair of tools a perfect fit.

Install SQLC

SQLC is just a command line tool, it's not a package that we need to import. I recommend installing it using go install:

go install github.com/kyleconroy/sqlc/cmd/sqlc@latest

Then run sqlc version to make sure it's installed correctly.

Install Goose

Like SQLC, Goose is just a command line tool. I also recommend installing it using go install:

go install github.com/pressly/goose/v3/cmd/goose@latest

Run goose -version to make sure it's installed correctly.

Run migration

cd in sql/schema folder and run:

goose postgres postgres://USERNAME:PASSWORD@HOST:5432/db up

Create user query

At the root of the project, create a yaml file sqlc.yaml. Inside /sql folder, create another /queries folder and add a users.sql file.

cd $PROJECT_ROOT
sqlc generate

Open connection

Using the sql package from the Go's standard library we open a connection with postgres db.

Handler to create user

Endpoint: POST /v1/users Create a User model to adapt the User struct generated by sqlc.

Api Key

Create a new migration sql file in sql/schema and run command: goose postgres postgres://USER:PASS@HOST:5432/db up. Update the query to create a user and regenerate database package with sqlc sqlc generate. Add a new query to get a user by API key.

Each time you update your queries or schema you'll need to regenerate your Go code with sqlc generate. If you update the schema you'll also need to migrate your database up (and maybe down). Create a new package in internal folder /internal/auth and create a function GetAPIKey wich will extracts the key from the HTTP request header.

Create feed

Add a new migration and create a new table for feeds and appropriate columns. run goose command from /sql/schema folder Add a new query to create a feed, then use sqlc generate to generate the Go code. Create function to map a dbFeed to a Feed, and a slice of dbFeeds to a slice of Feed. Add a new query to get all the feeds in db (no auth).

Feed follows (many to many relationships)

Add a new migration and create a new table for feed follows (constraint unique(user_id, feed_id)).

FAQs

Package last updated on 12 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts