Skip to content

Commit bca535f

Browse files
authored
Merge pull request #2 from PSNAppz/develop
Release v1.2
2 parents 7155052 + 6864b2c commit bca535f

File tree

13 files changed

+375
-38
lines changed

13 files changed

+375
-38
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ override.tf.json
4949

5050
# Ignore CLI configuration files
5151
.terraformrc
52-
terraform.rc
52+
terraform.rc
53+
tfplan

.terraform.lock.hcl

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
FROM golang:1.19.5
22

33
COPY go.mod go.sum /go/src/github.com/PSNAppz/Fold-ELK/
4+
COPY .env.sample .env
45
WORKDIR /go/src/github.com/PSNAppz/Fold-ELK
6+
ENV $(cat /path/to/.env | xargs)
57
RUN go mod download
68

79
COPY . /go/src/github.com/PSNAppz/Fold-ELK

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ Data is seeded using faker, which is used to populate the PostgresSQL database.
2121
- Docker
2222
- Docker Compose
2323
- Go
24+
- Terraform (For deploying to AWS)
2425

25-
### Setting up the docker environment
26+
### Setting up and building the docker environment
2627

2728
- Clone the repo
2829
- Run `mv .env.example .env` to create the environment file
@@ -36,6 +37,40 @@ Data is seeded using faker, which is used to populate the PostgresSQL database.
3637

3738
The CRUD API can be run locally using the below steps:
3839

39-
- Make sure the ELK stack is up and running using the above steps
40+
- Make sure the ELK stack is up and running using the above steps (except the API service)
4041
- Run `go build -o bin/application cmd/api/main.go` to build the API binary
42+
- Run `export PGURL="postgres://fold-elk:password@localhost:5432/fold_elk?sslmode=disable"`
43+
- Migrate the database using `migrate -database $PGURL -path db/migrations/ up `
4144
- Run `./bin/application` to run the API and start listening on port 8080
45+
46+
### Seed the database (Optional)
47+
A seeder binary is provided to seed the database with fake data.
48+
#### Run the existing seeder binary
49+
50+
- Run `./bin/seeder` to seed the database with dummy data
51+
#### Modifying & Build the seeder binary
52+
- Locate the `main.go` file in the `cmd/seed` directory and modify if needed
53+
- Run `go build -o bin/seeder cmd/seed/main.go` to build the seeder binary
54+
55+
## Running the tests
56+
57+
As of now, the tests are only for the User API. The tests can be run using the below steps:
58+
59+
- Make sure the ELK stack is up and running using the above steps
60+
- Go inside the `tests` directory `cd tests`
61+
- Run `go test`
62+
63+
## Deployment
64+
65+
### Deploying to AWS
66+
The project can be deployed to AWS using Terraform. The Terraform scripts can be located in the project root directory. The script will provision an EC2 instance, install docker and docker-compose and then deploy the ELK stack and the API service.
67+
68+
- Make sure you have Terraform installed
69+
- Run `terraform init` to initialize the Terraform project
70+
- Run `terraform plan` to see the changes that will be made
71+
- Run `terraform apply` to apply the changes
72+
73+
74+
### Pre Submission Checklist
75+
- [x] Detailed steps are included that allow us to spin up the services and the data pipeline.
76+
- [x] [Loom video URL](https://www.loom.com/share/9b76a3cf38cf4a48b40936adae8e74e9)

bin/seeder

6.24 MB
Binary file not shown.

cmd/seed/main.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"os"
6+
"strings"
7+
8+
"github.com/PSNAppz/Fold-ELK/db"
9+
"github.com/PSNAppz/Fold-ELK/models"
10+
"github.com/bxcodec/faker/v3"
11+
"github.com/rs/zerolog"
12+
"github.com/rs/zerolog/log"
13+
)
14+
15+
func main() {
16+
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
17+
dbConfig := db.Config{
18+
// hard-coding db connection info since we only have to seed on the dev machine
19+
Host: "localhost",
20+
Port: 5432,
21+
Username: "fold-elk",
22+
Password: "password",
23+
DbName: "fold_elk",
24+
}
25+
26+
dbInstance, err := db.Init(dbConfig)
27+
if err != nil {
28+
logger.Err(err).Msg("Connection failed")
29+
os.Exit(1)
30+
}
31+
logger.Info().Msg("Connected to Database")
32+
33+
// Seed dummy users
34+
logger.Info().Msg("Seeding Users to Database")
35+
for i := 0; i < 15; i++ {
36+
user := &models.User{
37+
Name: faker.Name(),
38+
}
39+
err = dbInstance.CreateUser(user)
40+
if err != nil {
41+
log.Err(err).Msg("failed to save record")
42+
}
43+
}
44+
logger.Info().Msg("Completed Seeding Users to Database")
45+
46+
// Create Hashtags
47+
logger.Info().Msg("Seeding Hashtags to Database")
48+
for i := 0; i < 15; i++ {
49+
ht := &models.Hashtag{
50+
Name: faker.Word(),
51+
}
52+
err = dbInstance.CreateHashtag(ht)
53+
if err != nil {
54+
log.Err(err).Msg("failed to save record")
55+
}
56+
}
57+
logger.Info().Msg("Completed Seeding Hashtags to Database")
58+
59+
// Seed dummy projects
60+
logger.Info().Msg("Seeding Projects to Database")
61+
for i := 0; i < 10; i++ {
62+
name := faker.Sentence()
63+
project := &models.Project{
64+
Name: name,
65+
Slug: strings.ReplaceAll(strings.ToLower(name), " ", "-"),
66+
Description: faker.Paragraph(),
67+
}
68+
69+
// get random user
70+
user, err := dbInstance.GetUserById(rand.Intn(15) + 1)
71+
if err != nil {
72+
log.Err(err).Msg("failed to get user")
73+
}
74+
75+
// get random hashtags
76+
hashtagIds := make([]models.Hashtag, 0)
77+
for i := 0; i < 3; i++ {
78+
hashtagIds, err = dbInstance.GetHashtags()
79+
if err != nil {
80+
log.Err(err).Msg("failed to get hashtags")
81+
}
82+
}
83+
// create project
84+
err = dbInstance.CreateProject(project, &user, &hashtagIds)
85+
if err != nil {
86+
log.Err(err).Msg("failed to save record")
87+
}
88+
}
89+
logger.Info().Msg("Completed Seeding Projects to Database")
90+
91+
}

db/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (db Database) UpdateUser(userId int, user models.User) error {
3333
}
3434

3535
user.ID = userId
36-
logQuery := "INSERT INTO users_logs(user_id, operation) VALUES ($1, $2, $3)"
36+
logQuery := "INSERT INTO users_logs(user_id, operation) VALUES ($1, $2)"
3737
_, err = db.Conn.Exec(logQuery, user.ID, updateOp)
3838
if err != nil {
3939
db.Logger.Err(err).Msg("could not log operation for logstash")

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/PSNAppz/Fold-ELK
33
go 1.19
44

55
require (
6+
github.com/bxcodec/faker/v3 v3.8.1
67
github.com/elastic/go-elasticsearch/v8 v8.6.0
78
github.com/gin-gonic/gin v1.8.2
89
github.com/lib/pq v1.10.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/bxcodec/faker/v3 v3.8.1 h1:qO/Xq19V6uHt2xujwpaetgKhraGCapqY2CRWGD/SqcM=
2+
github.com/bxcodec/faker/v3 v3.8.1/go.mod h1:DdSDccxF5msjFo5aO4vrobRQ8nIApg8kq3QWPEQD6+o=
13
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
24
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

setup_server.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
sudo apt update -y
4+
sudo apt install docker.io -y
5+
sudo apt install git -y
6+
sudo service docker start
7+
sudo systemctl enable docker
8+
9+
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
10+
mkdir -p $DOCKER_CONFIG/cli-plugins
11+
curl -SL https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
12+
sudo chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
13+

0 commit comments

Comments
 (0)