Skip to content

Commit

Permalink
Init database
Browse files Browse the repository at this point in the history
  • Loading branch information
voltgizerz committed Jun 8, 2024
1 parent 3c25d76 commit e01becd
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"context"
"os"
"os/signal"
"runtime"
"sync"
"syscall"

"github.com/voltgizerz/POS-restaurant/config"
"github.com/voltgizerz/POS-restaurant/database"
"github.com/voltgizerz/POS-restaurant/internal/app/repository"
"github.com/voltgizerz/POS-restaurant/pkg/jeager"
"github.com/voltgizerz/POS-restaurant/pkg/logger"
)
Expand All @@ -18,7 +21,6 @@ func main() {
logger.Init()

cfg := config.NewConfig()

defer handlePanic()

closer, err := jeager.NewJeager(cfg.App.Name)
Expand All @@ -27,6 +29,17 @@ func main() {
}
defer closer.Close()

ctx := context.Background()

// Init database
db := database.InitDatabase(ctx, cfg.Database)

repositoryOpts := repository.RepositoryOpts{
Database: db,
}

_ = repository.NewUserRepository(repositoryOpts)

logger.LogStdOut.Info("Application is now running. Press CTRL-C to exit.")

// Wait for a termination signal
Expand Down
36 changes: 36 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package database

import (
"context"

"github.com/jmoiron/sqlx"
"github.com/opentracing/opentracing-go"
"github.com/voltgizerz/POS-restaurant/config"
"github.com/voltgizerz/POS-restaurant/pkg/logger"
)

type DatabaseOpts struct {
MasterDB *sqlx.DB
}

// InitDatabase initializes and returns a new Database instance
func InitDatabase(ctx context.Context, cfg config.Database) *DatabaseOpts {
span, ctx := opentracing.StartSpanFromContext(ctx, "database.InitDatabase")
defer span.Finish()

return &DatabaseOpts{
MasterDB: connectMySQL(ctx, cfg),
}
}

func connectMySQL(ctx context.Context, cfg config.Database) *sqlx.DB {
span, _ := opentracing.StartSpanFromContext(ctx, "database.connectMySQL")
defer span.Finish()

db, err := sqlx.Connect("mysql", cfg.Host)
if err != nil {
logger.LogStdErr.Fatalf("[connectMySQL] failed connect err: %s", err.Error())
}

return db
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
7 changes: 7 additions & 0 deletions internal/app/interfaces/user.interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package interfaces

import (
"context"

"github.com/voltgizerz/POS-restaurant/internal/app/entity"
)

type IUserRepository interface {
GetUser(ctx context.Context, int64 int64) (*entity.User, error)
}
9 changes: 9 additions & 0 deletions internal/app/repository/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package repository

import (
"github.com/voltgizerz/POS-restaurant/database"
)

type RepositoryOpts struct {
Database *database.DatabaseOpts
}
40 changes: 40 additions & 0 deletions internal/app/repository/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package repository

import (
"context"

"github.com/jmoiron/sqlx"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"github.com/voltgizerz/POS-restaurant/internal/app/entity"
"github.com/voltgizerz/POS-restaurant/internal/app/interfaces"
"github.com/voltgizerz/POS-restaurant/pkg/logger"
)

type UserRepository struct {
MasterDB *sqlx.DB
}

func NewUserRepository(opts RepositoryOpts) interfaces.IUserRepository {
return &UserRepository{
MasterDB: opts.Database.MasterDB,
}
}

func (r *UserRepository) GetUser(ctx context.Context, userID int64) (*entity.User, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "repo.UserRepository.GetUser")
defer span.Finish()

user := entity.User{}
err := r.MasterDB.Get(&user, "SELECT * FROM users WHERE id=$1", userID)
if err != nil {
logger.LogStdErr.WithFields(logrus.Fields{
"user_id": userID,
"error": err,
}).Error("[UserRepository] error on GetUser")

return nil, err
}

return &user, nil
}

0 comments on commit e01becd

Please sign in to comment.