Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
codeinuit committed Aug 10, 2023
1 parent b6e0b10 commit 115e637
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
)

// handlers is the main struct for handling endpoints
type handlers struct {
log logger.Logger
db database.Database
Expand Down
7 changes: 7 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/gin-gonic/gin"
)

// FizzBuzz represents the main structure
type FizzBuzz struct {
engine *gin.Engine
srv *http.Server
Expand All @@ -27,6 +28,7 @@ type FizzBuzz struct {
quit chan os.Signal
}

// setupRouter init the main structure and the http router as well
func setupRouter() (fb *FizzBuzz, err error) {
l := logrus.NewLogrusLogger()
_, isDebug := os.LookupEnv("DEBUG")
Expand All @@ -44,6 +46,8 @@ func setupRouter() (fb *FizzBuzz, err error) {
return fb, nil
}

// Run will run main program along the http server.
// It should be run as goroutine and stopped using Stop function
func (fb *FizzBuzz) Run(port string) {
fb.srv = &http.Server{
Addr: fmt.Sprintf(":%s", port),
Expand All @@ -57,6 +61,7 @@ func (fb *FizzBuzz) Run(port string) {
}
}

// Stop is used to stop the main program and its http server properly
func (fb FizzBuzz) Stop() {
fb.log.Warn("stop signal catched, closing server")

Expand All @@ -71,6 +76,8 @@ func (fb FizzBuzz) Stop() {
fb.log.Info("server closed, exiting")
}

// initRoutes is used to init the base routes and attach them
// to the router
func initRoutes(fb *FizzBuzz, db database.Database) {
h := handlers{
log: fb.log,
Expand Down
6 changes: 6 additions & 0 deletions pkg/database/mysql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type MySQLDatabase struct {
db *gorm.DB
}

// InitDatabase is used to return the MySQL database instance
func InitDatabase() (*MySQLDatabase, error) {
dbHost := os.Getenv(MYSQL_HOST)
dbDatabase := os.Getenv(MYSQL_DB)
Expand All @@ -45,12 +46,15 @@ func InitDatabase() (*MySQLDatabase, error) {
}, err
}

// CountUsage is used to return the number of usages of FizzBuzz from the DB
func (db *MySQLDatabase) CountUsage() (models.Stats, error) {
var result []models.Stats

err := db.db.Model(&models.Stats{}).Find(&result).Error

// @TODO: use max() instead
// reason of the comment : encountered a library bug with the driver
// getting every entries is usually a bad practice
var mostUsed models.Stats
for _, stat := range result {
if stat.Use > mostUsed.Use {
Expand All @@ -61,6 +65,8 @@ func (db *MySQLDatabase) CountUsage() (models.Stats, error) {
return mostUsed, err
}

// UsageUpdate is used to increment the number of usages for FizzBuzz endpoint
// to the database
func (db *MySQLDatabase) UsageUpdate(m models.Stats) error {
return db.db.Transaction(func(tx *gorm.DB) error {
var result models.Stats
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"gorm.io/gorm"
)

// Stats is the model to store FizzBuzz calls and
// the numbers of usages
type Stats struct {
gorm.Model

Expand Down

0 comments on commit 115e637

Please sign in to comment.