This repository serves as an example of a completely dockerized Rest API, CRUD with a Database, and Websocket notifying events. It was created to investigate some techniques in Go. (with Gorilla 😢)
Keywords: JWT, Postgres, Rest API, Environment Variables, UUID, Middlewares, Specification Pattern
- Go 1.20.2 or higher
- Docker and Docker compose (I use Docker version 23.01.1 and docker-compose v2.17.0) Note: This repository includes a way to run it using Docker without having to install Go locally. It can move slowly for exploration and quickly for changes while still being useful.
- Make sure to download all Needed tools
- Clone the repository
git clone https://github.com/dasalgadoc/go-rest-websockets.git
- There are two options for moving further at this point: semi-dockerization or full-dockerization
This approach uses Docker to manage a Postgres database and localhost to compile and run the Go source code.
- Build up go project
go mod download
go get .
- Starts the database docker
docker-compose up --build -d postgres
- Uncomment environment variable in .env file
DATABASE_URL=postgres://postgres:postgres@localhost:54321/postgres?sslmode=disable
- Run the API
go run main.go
- Test the Ping Endpoint
curl --request GET \
--url http://localhost:8081/ping
In this method, a Postgres database is managed by Docker, and Golang Alpine code is copied, built, and sent as another docker image to execute.
- Make sure that the DATABASE_URL environment variable is commented .env file
# DATABASE_URL=postgres://postgres:postgres@localhost:54321/postgres?sslmode=disable
- Start the docker compose
docker-compose up --build
- Test the Ping Endpoint
curl --request GET \
--url http://localhost:8085/ping
You can run queries on Postgres docker using:
docker exec -it go-rest-websockets-postgres-1 psql -U postgres -d postgres -c "<QUERY>"
You can remove all the docker compose volumes using:
docker-compose down -v
If everything went ok, you can test the ping endpoint. Remember, if you use the Semi-dockerization the selected port its 8081 (.env PORT variable) Full-dockerization, will forward the 8081 port to 8085 (docker-compose).
Basic Heathcheck
curl --request GET \
--url http://localhost:<PORT>/ping
curl --request POST \
--url http://localhost:<PORT>/signup \
--header 'Content-Type: application/json' \
--data '{
"email": "example@example.com",
"password": "password"
}'
With a proper credentials this endpoint will generate a JWT token, useful for the next request
curl --request POST \
--url http://localhost:<PORT>/login \
--header 'Content-Type: application/json' \
--data '{
"email": "example@example.com",
"password": "password"
}'
curl --request GET \
--url http://localhost:<PORT>/me \
--header 'x-auth-token: <TOKEN>'
This endpoint generates the POST_ID, you can query the database for more.
curl --request POST \
--url http://localhost:<PORT>/post \
--header 'Content-Type: application/json' \
--header 'x-auth-token: <TOKEN>' \
--data '{
"post_content": "My custom content"
}'
curl --request GET \
--url http://localhost:<PORT>/post/<POST_ID> \
--header 'x-auth-token: <TOKEN>'
curl --request PUT \
--url http://localhost:<PORT>/post/<POST_ID> \
--header 'Content-Type: application/json' \
--header 'x-auth-token: <TOKEN>' \
--data '{
"post_content": "New content"
}'
curl --request DELETE \
--url http://localhost:<PORT>/post/<POST_ID> \
--header 'x-auth-token: <TOKEN>'
This repository explores Rest API and WebSockets using these libraries.
go get github.com/gorilla/mux
go get github.com/gorilla/websocket
Despite, I already used Gin in Solid Example, I decided to use the Gorilla alternatives without knowing these libraries was marked as archived at the end of 2022.
I won't change this repository to use others libraries, but I will link some alternatives:
For websockets