Skip to content

Commit

Permalink
fix: ⚡ refactor a flujo de trabajo
Browse files Browse the repository at this point in the history
se agregan nueva forma de administrar secrets, automatizacion a traves de Makefile, migraciones para bases de datos
  • Loading branch information
Euler-B committed Feb 24, 2024
1 parent 33163bf commit 216b3c5
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 64 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HOST_SERVER=
PORT_SERVER=
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
DB_NAME=
MARIADB_URL_0=
MARIADB_URL_1=
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ datadir/
index.html
# El archivo index.html solo uso para debbug de mis metodos de seguridad en la api

.env
.idea/

37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
include .env

run:
go run main.go

database-up:
docker run \
-d \
--name mariaDB \
-p 3306:3306 \
-e MARIADB_ROOT_PASSWORD=${DB_PASSWORD} \
-e MARIADB_DATABASE=${DB_NAME} \
mariadb:10.7.4
database-down:
docker rm -f mariaDB


start-db:
make database-up
echo Espere unos segundos mientras se levanta la instancia
sleep 15
make migrate-db


migrate-db:
migrate -path database/migrations/ -database ${MARIADB_URL} up

migrate-db-rollback:
migrate -path database/migrations/ -database ${MARIADB_URL} down

tests:
go test -v ./...

# TO_DO:
# 1.- refactoizar star-db
# 2.- refactorizar sentencias migrate down

2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func New(ctx context.Context, s *settings.Settings) (*sqlx.DB, error) {
connectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?parseTime=true",
"%s:%s@tcp(%s:%s)/%s?parseTime=true",
s.DB.User,
s.DB.Password,
s.DB.Host,
Expand Down
1 change: 1 addition & 0 deletions database/migrations/00_users.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS USERS;
7 changes: 7 additions & 0 deletions database/migrations/00_users.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
create table USERS(
id int not null auto_increment,
email varchar(255) not null,
name varchar(255) not null,
password varchar(255) not null,
primary key(id)
);
1 change: 1 addition & 0 deletions database/migrations/01_products.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS PRODUCTS;
10 changes: 10 additions & 0 deletions database/migrations/01_products.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
create table PRODUCTS(
id int not null auto_increment,
name varchar(255) not null,
description varchar(255) not null,
price float not null,
created_by int not null,
primary key (id),
foreign key (created_by) references USERS(id)

);
1 change: 1 addition & 0 deletions database/migrations/02_roles.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS ROLES;
5 changes: 5 additions & 0 deletions database/migrations/02_roles.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table ROLES(
id int not null auto_increment,
name varchar(255) not null,
primary key(id)
);
1 change: 1 addition & 0 deletions database/migrations/03_userRoles.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS USERS_ROLES;
8 changes: 8 additions & 0 deletions database/migrations/03_userRoles.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table USERS_ROLES(
id int not null auto_increment,
user_id int not null,
role_id int not null,
primary key (id),
foreign key (user_id) references USERS(id),
foreign key (role_id) references ROLES(id)
);
1 change: 1 addition & 0 deletions database/migrations/04_insertRoles.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ROLES DROP (id, name);
5 changes: 5 additions & 0 deletions database/migrations/04_insertRoles.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
INSERT INTO ROLES (id, name)
VALUES
(1, 'admin'),
(2, 'seller'),
(3, 'customer');
42 changes: 0 additions & 42 deletions database/squema.sql

This file was deleted.

5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.21.6
require (
github.com/go-playground/validator/v10 v10.18.0
github.com/go-sql-driver/mysql v1.7.1
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/jmoiron/sqlx v1.3.5
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.11.4
github.com/stretchr/testify v1.8.4
go.uber.org/fx v1.20.1
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -18,7 +19,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -36,4 +36,5 @@ require (
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ func main() {
func setLifeCycle(lc fx.Lifecycle, a *api.API, s *settings.Settings, e *echo.Echo) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error { // el context usado aqui es el propio de la libreria fx
dbAddress := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?parseTime=true",
s.DB.User,
s.DB.Password,
s.DB.Host,
s.DB.Port,
s.DB.Name,
)
println(dbAddress)
address := fmt.Sprintf("%s:%s", s.Host, s.Port)
println(address)
go a.Start(e, address)

return nil
Expand Down
49 changes: 30 additions & 19 deletions settings/settings.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
package settings

import (
_ "embed"
"log"
"os"

"gopkg.in/yaml.v3"
"github.com/joho/godotenv"
)

//go:embed settings.yml
var settingsFile []byte

type DatabaseConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Name string `yaml:"name"`
Host string `string:"host"`
Port string `string:"port"`
User string `string:"user"`
Password string `string:"password"`
Name string `string:"name"`
}

type Settings struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
DB DatabaseConfig `yaml:"database"`
Host string `string:"host"`
Port string `int64:"port"`
DB DatabaseConfig `string:"database"`
}

func New() (*Settings, error) {
var s Settings

err := yaml.Unmarshal(settingsFile, &s)
func init() {
err := godotenv.Load(".env")
if err != nil {
return nil, err
log.Fatal("Error loading .env")
}
}

func New() *Settings {
dbConf := DatabaseConfig{
Host: os.Getenv("D_HOST"),
Port: os.Getenv("DB_PORT"),
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
Name: os.Getenv("DB_NAME"),
}
s := Settings{
Host: os.Getenv("HOST_SERVER"),
Port: os.Getenv("PORT_SERVER"),
DB: dbConf,
}
return &s, nil
return &s
}

0 comments on commit 216b3c5

Please sign in to comment.