Skip to content

Commit

Permalink
refactor: making code more idiomatic based on best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
daithihearn committed Nov 18, 2023
1 parent c86aec3 commit 90c5c3c
Show file tree
Hide file tree
Showing 35 changed files with 685 additions and 612 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ docs: #@ Generate docs
swag init -g cmd/api/main.go
.PHONY:docs
test: fmt vet #@ Run tests
go test -v -coverprofile=coverage.out ./...
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
.PHONY:test
fmt: #@ Format the code
Expand Down
23 changes: 21 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package main

import (
"context"
_ "electricity-prices/docs"
"electricity-prices/pkg/api"
"electricity-prices/pkg/db"
"os"
"os/signal"
"syscall"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand All @@ -26,12 +28,26 @@ func init() {
}

func main() {
// Catch SIGINT or SIGTERM and gracefully close the database connection.
// Create a cancellable context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Set up signal handling
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
db.CloseMongoConnection()
cancel() // Cancel the context upon receiving the signal

// Create a new context for the graceful shutdown procedure
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()

// Gracefully close the database connection
if err := db.CloseMongoConnection(shutdownCtx); err != nil {
// Handle error (e.g., log it)
os.Exit(1)
}
os.Exit(0)
}()

Expand Down Expand Up @@ -66,4 +82,7 @@ func main() {
if err != nil {
return
}

// Wait for the cancellation of the context (due to signal handling)
<-ctx.Done()
}
28 changes: 24 additions & 4 deletions cmd/sync/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"context"
"electricity-prices/pkg/db"
"electricity-prices/pkg/service"
"electricity-prices/pkg/sync"
"github.com/joho/godotenv"
"os"
"os/signal"
"syscall"
"time"
)

func init() {
Expand All @@ -15,15 +17,33 @@ func init() {
}

func main() {
// Catch SIGINT or SIGTERM and gracefully close the database connection.
// Create a cancellable context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Set up signal handling
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
db.CloseMongoConnection()
cancel() // Cancel the context upon receiving the signal

// Create a new context for the graceful shutdown procedure
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()

// Gracefully close the database connection
if err := db.CloseMongoConnection(shutdownCtx); err != nil {
// Handle error (e.g., log it)
os.Exit(1)
}
os.Exit(0)
}()

// Sync with the API.
service.SyncWithAPI()
sync.SyncWithAPI(ctx)
cancel()

// Wait for the cancellation of the context (due to signal handling)
<-ctx.Done()
}
64 changes: 32 additions & 32 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.AlexaResponse"
"$ref": "#/definitions/alexa.AlexaResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
Expand All @@ -72,19 +72,19 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.AlexaResponse"
"$ref": "#/definitions/alexa.AlexaResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
Expand Down Expand Up @@ -114,20 +114,20 @@ const docTemplate = `{
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Price"
"$ref": "#/definitions/price.Price"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
Expand Down Expand Up @@ -157,20 +157,20 @@ const docTemplate = `{
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.DailyAverage"
"$ref": "#/definitions/price.DailyAverage"
}
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
Expand Down Expand Up @@ -198,27 +198,27 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.DailyPriceInfo"
"$ref": "#/definitions/price.DailyPriceInfo"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/model.ErrorResponse"
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
}
}
},
"definitions": {
"model.AlexaResponse": {
"alexa.AlexaResponse": {
"type": "object",
"properties": {
"mainText": {
Expand All @@ -238,7 +238,15 @@ const docTemplate = `{
}
}
},
"model.DailyAverage": {
"api.ErrorResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"price.DailyAverage": {
"type": "object",
"properties": {
"average": {
Expand All @@ -249,45 +257,45 @@ const docTemplate = `{
}
}
},
"model.DailyPriceInfo": {
"price.DailyPriceInfo": {
"type": "object",
"properties": {
"cheapestPeriods": {
"type": "array",
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Price"
"$ref": "#/definitions/price.Price"
}
}
},
"dayAverage": {
"type": "number"
},
"dayRating": {
"$ref": "#/definitions/model.DayRating"
"$ref": "#/definitions/price.DayRating"
},
"expensivePeriods": {
"type": "array",
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Price"
"$ref": "#/definitions/price.Price"
}
}
},
"prices": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Price"
"$ref": "#/definitions/price.Price"
}
},
"thirtyDayAverage": {
"type": "number"
}
}
},
"model.DayRating": {
"price.DayRating": {
"type": "string",
"enum": [
""
Expand All @@ -296,15 +304,7 @@ const docTemplate = `{
"Nil"
]
},
"model.ErrorResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"model.Price": {
"price.Price": {
"type": "object",
"properties": {
"dateTime": {
Expand Down
Loading

0 comments on commit 90c5c3c

Please sign in to comment.