Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ publish:
## Deploy ondehoje service on heroku
.PHONY: heroku/release
heroku/release:
heroku container:release web --app ondehoje
heroku container:release web --app ondehoje ${image}

## Run ondehoje service
.PHONY: run
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center" style="font-size: 24px; font-family: Arial, sans-serif;">
<h1 style="font-family: 'Playfair Display', serif; font-size: 36px;">Onde Hoje ?</h1>
<p style="font-family: 'Lato', sans-serif; font-size: 16px;">A Single place to share the underground</p>
<p style="font-family: 'Lato', sans-serif; font-size: 16px;">Uncovering the Underground</p>
</p>

Onde Hoje is an innovative application that allows users to share underground events in their city. This app is designed for people who are looking for unique experiences and want to explore the hidden gems of their city.
Expand Down Expand Up @@ -56,7 +56,11 @@ sudo apt-get install postgresql #to install psql

heroku pg:psql -a ondehoje # to access database and execute admin commands
```

## Local Database
Pass: example_password
```bash
psql -h localhost -p 5432 -d example_db -U postgres
```
# Ship New code to production

Although there is a CI for that, it's possible to SHIP🚀 new code to production just using your machine.
Expand All @@ -70,6 +74,12 @@ make heroku/release

```

# Load Test

```
k6 run --vus 10 --duration 30s load-test/create-event.js
```

# Useful Vscode extensions

[OpenAPI Swagger Editor](https://42crunch.com/tutorial-openapi-swagger-extension-vs-code/)
[OpenAPI Swagger Editor](https://42crunch.com/tutorial-openapi-swagger-extension-vs-code/)
22 changes: 22 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ const (
eventPathId = "/event/{id}"
)

func deleteAllEventsHandler(eventRepo event.Repository) http.HandlerFunc {
fn := func(w http.ResponseWriter, r *http.Request) {
slog.Info("Calling deleteAll")
if r.Method != http.MethodDelete {
slog.Error("Method not allowed")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}

slog.Info("Deleting all events")
err := eventRepo.DeleteAll(r.Context())
if err != nil {
slog.Error("Delete failed", "error", err)
http.Error(w, "Delete failed", http.StatusInternalServerError)
return
}
slog.Info("Events deleted successfully")
}
return http.HandlerFunc(fn)
}

func deleteEventHandler(eventRepo event.Repository) http.HandlerFunc {
fn := func(w http.ResponseWriter, r *http.Request) {
slog.Info("Calling deleteEventHandler")
Expand Down Expand Up @@ -222,6 +243,7 @@ func HandlerFactory(db *pgxpool.Pool) http.Handler {
router.HandleFunc(eventPathId, deleteEventHandler(eventSQLRepo)).Methods(http.MethodDelete)
router.HandleFunc(eventPathId, getByIDHandler(eventSQLRepo)).Methods(http.MethodGet)
router.HandleFunc(eventPathId, Update(eventSQLRepo)).Methods(http.MethodPut)
router.HandleFunc(eventPath, deleteAllEventsHandler(eventSQLRepo)).Methods(http.MethodDelete) // this function will not be exposed in the swagger
// documentation for developers
opts := middleware.SwaggerUIOpts{SpecURL: "openapi.yaml"}
sh := middleware.SwaggerUI(opts, nil)
Expand Down
9 changes: 9 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Repository interface {
All(ctx context.Context) ([]Event, error)
GetByID(ctx context.Context, id int64) (*Event, error)
Update(ctx context.Context, id int64, newEvent Event) (*Event, error)
DeleteAll(ctx context.Context) error
}

type SQLRepository struct {
Expand All @@ -41,6 +42,14 @@ func EventSQLRepository(db *pgxpool.Pool) *SQLRepository {
return &SQLRepository{db: db}
}

func (r *SQLRepository) DeleteAll(ctx context.Context) error {
_, err := r.db.Exec(ctx, `DELETE FROM events`)
if err != nil {
return err
}
return nil
}

func (r *SQLRepository) Update(ctx context.Context, id int64, newEvent Event) (*Event, error) {

err := r.db.QueryRow(ctx,
Expand Down
41 changes: 41 additions & 0 deletions load-test/create-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//import k6 and do a request to localhost/events with a POST method passing the event object as a JSON string
import http from 'k6/http';
import { sleep, check } from 'k6';


export function teardown() {
//delte all events created by the load test
const url = 'http://localhost:8000/event';
const params = {
headers: {
'Content-Type': 'application/json',
'accept': '*/*',
},
}
let res = http.del(url, null, params)
check(res, {
'is status 200': (r) => r.status === 200,
});
}

export default function () {
const url = 'http://localhost:8000/event';
const params = {
headers: {
'Content-Type': 'application/json',
'accept': '*/*',
},
}
const payload = JSON.stringify({
"title": "string",
"description": "string",
"location": "string",
"start_time": "2023-05-07T13:22:08.124Z",
"end_time": "2023-05-07T13:22:08.124Z",
"instagram_page": "string"
})
let res = http.post(url, payload, params)
check(res, {
'is status 200': (r) => r.status === 200,
});
}
6 changes: 0 additions & 6 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ paths:
summary: Get all events
tags:
- "Event"
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/EventRequest"
responses:
"200":
description: OK
Expand Down