Skip to content

Commit

Permalink
Merge pull request #2 from PSNAppz/develop
Browse files Browse the repository at this point in the history
Release v1.2
  • Loading branch information
PSNAppz authored Feb 20, 2023
2 parents 7155052 + 6864b2c commit bca535f
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ override.tf.json

# Ignore CLI configuration files
.terraformrc
terraform.rc
terraform.rc
tfplan
40 changes: 40 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM golang:1.19.5

COPY go.mod go.sum /go/src/github.com/PSNAppz/Fold-ELK/
COPY .env.sample .env
WORKDIR /go/src/github.com/PSNAppz/Fold-ELK
ENV $(cat /path/to/.env | xargs)
RUN go mod download

COPY . /go/src/github.com/PSNAppz/Fold-ELK
Expand Down
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ Data is seeded using faker, which is used to populate the PostgresSQL database.
- Docker
- Docker Compose
- Go
- Terraform (For deploying to AWS)

### Setting up the docker environment
### Setting up and building the docker environment

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

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

- Make sure the ELK stack is up and running using the above steps
- Make sure the ELK stack is up and running using the above steps (except the API service)
- Run `go build -o bin/application cmd/api/main.go` to build the API binary
- Run `export PGURL="postgres://fold-elk:password@localhost:5432/fold_elk?sslmode=disable"`
- Migrate the database using `migrate -database $PGURL -path db/migrations/ up `
- Run `./bin/application` to run the API and start listening on port 8080

### Seed the database (Optional)
A seeder binary is provided to seed the database with fake data.
#### Run the existing seeder binary

- Run `./bin/seeder` to seed the database with dummy data
#### Modifying & Build the seeder binary
- Locate the `main.go` file in the `cmd/seed` directory and modify if needed
- Run `go build -o bin/seeder cmd/seed/main.go` to build the seeder binary

## Running the tests

As of now, the tests are only for the User API. The tests can be run using the below steps:

- Make sure the ELK stack is up and running using the above steps
- Go inside the `tests` directory `cd tests`
- Run `go test`

## Deployment

### Deploying to AWS
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.

- Make sure you have Terraform installed
- Run `terraform init` to initialize the Terraform project
- Run `terraform plan` to see the changes that will be made
- Run `terraform apply` to apply the changes


### Pre Submission Checklist
- [x] Detailed steps are included that allow us to spin up the services and the data pipeline.
- [x] [Loom video URL](https://www.loom.com/share/9b76a3cf38cf4a48b40936adae8e74e9)
Binary file added bin/seeder
Binary file not shown.
91 changes: 91 additions & 0 deletions cmd/seed/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"math/rand"
"os"
"strings"

"github.com/PSNAppz/Fold-ELK/db"
"github.com/PSNAppz/Fold-ELK/models"
"github.com/bxcodec/faker/v3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func main() {
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
dbConfig := db.Config{
// hard-coding db connection info since we only have to seed on the dev machine
Host: "localhost",
Port: 5432,
Username: "fold-elk",
Password: "password",
DbName: "fold_elk",
}

dbInstance, err := db.Init(dbConfig)
if err != nil {
logger.Err(err).Msg("Connection failed")
os.Exit(1)
}
logger.Info().Msg("Connected to Database")

// Seed dummy users
logger.Info().Msg("Seeding Users to Database")
for i := 0; i < 15; i++ {
user := &models.User{
Name: faker.Name(),
}
err = dbInstance.CreateUser(user)
if err != nil {
log.Err(err).Msg("failed to save record")
}
}
logger.Info().Msg("Completed Seeding Users to Database")

// Create Hashtags
logger.Info().Msg("Seeding Hashtags to Database")
for i := 0; i < 15; i++ {
ht := &models.Hashtag{
Name: faker.Word(),
}
err = dbInstance.CreateHashtag(ht)
if err != nil {
log.Err(err).Msg("failed to save record")
}
}
logger.Info().Msg("Completed Seeding Hashtags to Database")

// Seed dummy projects
logger.Info().Msg("Seeding Projects to Database")
for i := 0; i < 10; i++ {
name := faker.Sentence()
project := &models.Project{
Name: name,
Slug: strings.ReplaceAll(strings.ToLower(name), " ", "-"),
Description: faker.Paragraph(),
}

// get random user
user, err := dbInstance.GetUserById(rand.Intn(15) + 1)
if err != nil {
log.Err(err).Msg("failed to get user")
}

// get random hashtags
hashtagIds := make([]models.Hashtag, 0)
for i := 0; i < 3; i++ {
hashtagIds, err = dbInstance.GetHashtags()
if err != nil {
log.Err(err).Msg("failed to get hashtags")
}
}
// create project
err = dbInstance.CreateProject(project, &user, &hashtagIds)
if err != nil {
log.Err(err).Msg("failed to save record")
}
}
logger.Info().Msg("Completed Seeding Projects to Database")

}
2 changes: 1 addition & 1 deletion db/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (db Database) UpdateUser(userId int, user models.User) error {
}

user.ID = userId
logQuery := "INSERT INTO users_logs(user_id, operation) VALUES ($1, $2, $3)"
logQuery := "INSERT INTO users_logs(user_id, operation) VALUES ($1, $2)"
_, err = db.Conn.Exec(logQuery, user.ID, updateOp)
if err != nil {
db.Logger.Err(err).Msg("could not log operation for logstash")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/PSNAppz/Fold-ELK
go 1.19

require (
github.com/bxcodec/faker/v3 v3.8.1
github.com/elastic/go-elasticsearch/v8 v8.6.0
github.com/gin-gonic/gin v1.8.2
github.com/lib/pq v1.10.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/bxcodec/faker/v3 v3.8.1 h1:qO/Xq19V6uHt2xujwpaetgKhraGCapqY2CRWGD/SqcM=
github.com/bxcodec/faker/v3 v3.8.1/go.mod h1:DdSDccxF5msjFo5aO4vrobRQ8nIApg8kq3QWPEQD6+o=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
13 changes: 13 additions & 0 deletions setup_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

sudo apt update -y
sudo apt install docker.io -y
sudo apt install git -y
sudo service docker start
sudo systemctl enable docker

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
curl -SL https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
sudo chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

Loading

0 comments on commit bca535f

Please sign in to comment.