https://github.com/jojoarianto/learn-1-go-api
Serverless + Rest Api + Go
https://github.com/jojoarianto/learn-1-go-api
golang microservice serverless
Last synced: 4 months ago
JSON representation
Serverless + Rest Api + Go
- Host: GitHub
- URL: https://github.com/jojoarianto/learn-1-go-api
- Owner: jojoarianto
- Created: 2019-02-26T03:35:34.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-27T05:38:08.000Z (about 7 years ago)
- Last Synced: 2025-01-11T10:51:53.586Z (about 1 year ago)
- Topics: golang, microservice, serverless
- Language: Go
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Serverless with Golang
## Prerequisites
- Go 1.1x
- Serverless Framework
- AWS SAM CLI
- Docker
## Goal
- [X] API to [get all user](#get-all-users)
- [X] API to [get user by id](#get-user-by-id)
- [x] API to [create user](#create-user)
- [ ] API to delete user
- [ ] API to update user
## Run in local with SAM (AWS-SAM-CLI)
Compile go project
```bash
# see Makefile
make build
```
Run with SAM, (required : docker, postgres)
```bash
# http://localhost:8000/
sam local start-api -p 8000 --env-vars local.env.json
```
## Deployment
```bash
# set your serverless config
serverless config credentials --provider aws --key --secret
# deploy
serverless deploy
```
## Get All Users
> live demo [https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/user](https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/user)
Request
```bash
curl --request GET \
--url https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/users
```
Response
```json
"data": [
...
{
"user_id": 6,
"email": "user_6@gmail.com"
},
{
"user_id": 7,
"email": "user_7@hotmail.com"
},
...
]
}
```
## Get User By Id
> live demo [https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/user/2](https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/user/2)
Request
```bash
curl --request GET \
--url https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/user/14
```
Response
```json
{
"data": {
"user_id": 14,
"email": "create-something-cool@gmail.com"
}
}
```
## Create User
Request
```bash
curl --request POST \
--url https://0d8n48wogc.execute-api.us-east-1.amazonaws.com/dev/user \
--header 'content-type: application/json' \
--data '{
"email": "anything@gmail.com"
}'
```
### References
- Create serverless template with golang, https://serverless.com/blog/framework-example-golang-lambda-support/
- Deploy Serverless, https://medium.com/devopslinks/aws-lambda-serverless-framework-python-part-1-a-step-by-step-hello-world-4182202aba4a
- Run local aws-sam-cli, https://github.com/awslabs/aws-sam-cli
- Generating fake data using SQL, https://vnegrisolo.github.io/postgresql/generate-fake-data-using-sql
## Additional note
### Database
I use postgres, Here is instalation postgres using docker
```bash
docker pull postgres
# alpine is lite version
docker run -d -p 0.0.0.0:5432:5432 postgres:alpine
```