https://github.com/andreistefanciprian/go_rest_api_demo
Explore building a CRUD REST API and frontend UI in Go
https://github.com/andreistefanciprian/go_rest_api_demo
golang jwt jwt-authentication rest-api
Last synced: 8 months ago
JSON representation
Explore building a CRUD REST API and frontend UI in Go
- Host: GitHub
- URL: https://github.com/andreistefanciprian/go_rest_api_demo
- Owner: andreistefanciprian
- Created: 2022-09-18T07:28:59.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-28T11:59:55.000Z (over 3 years ago)
- Last Synced: 2025-07-13T10:49:24.925Z (12 months ago)
- Topics: golang, jwt, jwt-authentication, rest-api
- Language: Go
- Homepage:
- Size: 122 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## Description
Easy REST API backend and Bootstrap frontend written in Golang using standard libraries.
The backend exposes CRUD (Create, Read, Update and Delete) endpoints to allow accessing and manipulating articles in a library stored in mysql db.

## API Specification (backend)
The operations that our endpoint will allow include:
* **Create** a new article in response to a valid POST request at /article/create.
* **Fetch** an article in response to a valid GET request at /article/view?id={id}.
* **Fetch** a list of all articles in response to a valid GET request at /articles.
* **Update** an article in response to a valid PUT request at /article/update?id={id}.
* **Delete** an article in response to a valid DELETE request at /article/delete?id={id}.
Used Go libraries:
* net/http (standard library for using web servers)
* gorm (mysql ORM)
* encoding/json (for transforming data from go struct to json and vice versa)
* jwt-go (for authenticating requests with JWT tokens)
## Frontend
I'm using https://getbootstrap.com/ to have a nice user interface without going deep on CSS, JS and HTML.
Used Go libraries:
* net/http
* html/template
## Requirements
* Golang (for writing the application code)
* Docker (for packaging and running the application)
* Any browser
* https://jwt.io/ for generating jwt tokens
## Run app with docker-compose
```
# start app
docker-compose up --build
# stop app
docker-compose down
# access frontend at localhost:8090/
```
## (Optional) Test REST API endpoints with curl or Postman
###### Generate jwt token with https://jwt.io/
Make sure you use the same secret key (JWT_SECRET_KEY) to generate a valid JWT token.
```
export JWT_TOKEN=""
```
###### Get all articles
```
curl -X GET http://localhost:8080/articles \
-H 'Content-Type: application/json' \
-H "Token:$JWT_TOKEN"
```
###### Create new article
```
curl -X POST http://localhost:8080/article/create \
-H 'Content-Type: application/json' \
-H "Token:$JWT_TOKEN" \
-d '''
{
"Title": "Book Title",
"desc": "Book Description",
"content": "Book Content"
}
'''
```
###### View article by id
```
curl -X GET 'http://localhost:8080/article/view?id=32' \
-H 'Content-Type: application/json' \
-H "Token:$JWT_TOKEN"
```
###### Update article by id
```
curl -X POST 'http://localhost:8080/article/update?id=32' \
-H 'Content-Type: application/json' \
-H "Token:$JWT_TOKEN"
-d '''
{
"Title": "Updated Book Title",
"desc": "Book Description",
"content": "Book Content"
}
'''
```
###### Delete article by id
```
curl -X POST 'http://localhost:8080/article/delete?id=32' \
-H "Token:$JWT_TOKEN"
```
###### Delete all articles
```
curl -X POST 'http://localhost:8080/articles/delete_all' \
-H "Token:$JWT_TOKEN"
```
## Debug commands
```
docker network ls
docker run --network go_rest_api_demo_demo -it --rm --name mysql-client mysql \
mysql -hmy_db -udemouser -pdemopassword -e "SELECT * from quickdemo.articles;"
```