Skip to content

Commit

Permalink
Merge pull request #10 from kareemmahlees:refactor_arch
Browse files Browse the repository at this point in the history
Architectural changes
  • Loading branch information
kareemmahlees authored Apr 21, 2024
2 parents ec4e825 + 81348a2 commit a83c582
Show file tree
Hide file tree
Showing 45 changed files with 2,290 additions and 1,895 deletions.
46 changes: 46 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = ["sqlite3", "-f", ".\\test.db" ]
bin = "tmp\\main.exe"
cmd = "go build -o ./tmp/main.exe ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
48 changes: 23 additions & 25 deletions cmd/mysql.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
package cmd

import (
"fmt"
"github.com/kareemmahlees/meta-x/internal"
"github.com/kareemmahlees/meta-x/internal/db"
"github.com/kareemmahlees/meta-x/lib"

"github.com/go-sql-driver/mysql"
"github.com/gofiber/fiber/v2"
"github.com/kareemmahlees/meta-x/utils"
"github.com/spf13/cobra"
)

var mysqlCommand = &cobra.Command{
Use: "mysql",
Short: "use mysql as the database provider",
RunE: func(cmd *cobra.Command, args []string) error {
var cfg string
var mysqlConfig *utils.MySQLConfig

port, _ := cmd.Flags().GetInt("port")

connUrl, _ := cmd.Flags().GetString("url")
if connUrl != "" {
cfg = connUrl
mysqlConfig = utils.NewMySQLConfig(&connUrl, nil)
} else {
dbUsername, _ := cmd.Flags().GetString("username")
dbHost, _ := cmd.Flags().GetString("host")
dbPort, _ := cmd.Flags().GetInt("dbPort")
dbName, _ := cmd.Flags().GetString("db")

dbPassword, _ := cmd.Flags().GetString("password")
if dbPassword == "" {
fmt.Println("Enter password: ")
fmt.Scanln(&dbPassword)
}

conf := mysql.Config{
User: dbUsername,
Passwd: dbPassword,
DBName: dbName,
Net: "tcp",
Addr: fmt.Sprintf("%s:%d", dbHost, dbPort),
}
cfg = conf.FormatDSN()
mysqlConfig = utils.NewMySQLConfig(nil, &utils.MySQLConnectionParams{
DBUsername: dbUsername,
DBPassword: dbPassword,
DBHost: dbHost,
DBPort: dbPort,
DBName: dbName,
})
}

port, _ := cmd.Flags().GetInt("port")

app := fiber.New(fiber.Config{DisableStartupMessage: true})
conn, err := db.InitDBConn(lib.MYSQL, mysqlConfig)
if err != nil {
return err
}
provider := db.NewMySQLProvider(conn)
server := internal.NewServer(provider, port, make(chan bool, 1))

if err := internal.InitDBAndServer(app, lib.MYSQL, cfg, port, make(chan bool, 1)); err != nil {
if err := server.Serve(); err != nil {
return err
}

return nil
},
}

func init() {

mysqlCommand.Flags().String("username", "", "db username")
mysqlCommand.Flags().String("username", "root", "db username")
mysqlCommand.Flags().String("password", "", "db password")
mysqlCommand.Flags().String("host", "localhost", "db host")
mysqlCommand.Flags().Int("dbPort", 3306, "db port")
mysqlCommand.Flags().String("db", "mysql", "db name")
mysqlCommand.Flags().String("url", "", "connection url/string")

mysqlCommand.MarkFlagsMutuallyExclusive("username", "url")
mysqlCommand.MarkFlagsMutuallyExclusive("password", "url")

rootCmd.AddCommand(mysqlCommand)
}
44 changes: 24 additions & 20 deletions cmd/pg.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
package cmd

import (
"fmt"
"github.com/kareemmahlees/meta-x/internal"
"github.com/kareemmahlees/meta-x/internal/db"
"github.com/kareemmahlees/meta-x/lib"

"github.com/gofiber/fiber/v2"
"github.com/lib/pq"

"github.com/kareemmahlees/meta-x/utils"
"github.com/spf13/cobra"
)

var pgCommand = &cobra.Command{
Use: "pg",
Short: "use postgres as the database provider",
RunE: func(cmd *cobra.Command, args []string) error {
var cfg string
var pgConfig *utils.PgConfig

port, _ := cmd.Flags().GetInt("port")

connUrl, _ := cmd.Flags().GetString("url")
if connUrl != "" {
cfg = connUrl
pgConfig = utils.NewPGConfig(&connUrl, nil)
} else {
dbUsername, _ := cmd.Flags().GetString("username")
dbHost, _ := cmd.Flags().GetString("host")
dbPort, _ := cmd.Flags().GetInt("dbPort")
dbName, _ := cmd.Flags().GetString("db")
dbSslMode, _ := cmd.Flags().GetString("sslmode")

dbPassword, _ := cmd.Flags().GetString("password")
if dbPassword == "" {
fmt.Println("Enter password: ")
fmt.Scanln(&dbPassword)
}

cfg, _ = pq.ParseURL(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", dbUsername, dbPassword, dbHost, dbPort, dbName, dbSslMode))
pgConfig = utils.NewPGConfig(nil, &utils.PgConnectionParams{
DBUsername: dbUsername,
DBPassword: dbPassword,
DBHost: dbHost,
DBPort: dbPort,
DBName: dbName,
DBSslMode: dbSslMode,
})
}
conn, err := db.InitDBConn(lib.PSQL, pgConfig)
if err != nil {
return err
}
provider := db.NewPgProvider(conn)
server := internal.NewServer(provider, port, make(chan bool, 1))

port, _ := cmd.Flags().GetInt("port")

app := fiber.New(fiber.Config{DisableStartupMessage: true})

if err := internal.InitDBAndServer(app, lib.PSQL, cfg, port, make(chan bool, 1)); err != nil {
if err := server.Serve(); err != nil {
return err
}

return nil
},
}

func init() {

pgCommand.Flags().String("username", "", "db username")
pgCommand.Flags().String("username", "postgres", "db username")
pgCommand.Flags().String("password", "", "db password")
pgCommand.Flags().String("host", "localhost", "db host")
pgCommand.Flags().Int("dbPort", 5432, "db port")
pgCommand.Flags().String("db", "postgres", "db name")
pgCommand.Flags().String("url", "", "connection url/string")
pgCommand.Flags().String("sslmode", "disable", "db sslmode")
pgCommand.MarkFlagsMutuallyExclusive("username", "url")
pgCommand.MarkFlagsMutuallyExclusive("password", "url")

rootCmd.AddCommand(pgCommand)
}
15 changes: 12 additions & 3 deletions cmd/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cmd

import (
"github.com/kareemmahlees/meta-x/internal"
"github.com/kareemmahlees/meta-x/internal/db"
"github.com/kareemmahlees/meta-x/lib"
"github.com/kareemmahlees/meta-x/utils"

"github.com/gofiber/fiber/v2"
"github.com/spf13/cobra"
)

Expand All @@ -20,8 +21,16 @@ var sqlite3Command = &cobra.Command{
if err != nil {
return err
}
app := fiber.New(fiber.Config{DisableStartupMessage: true})
if err = internal.InitDBAndServer(app, lib.SQLITE3, filePath, port, make(chan bool, 1)); err != nil {
sqliteConfig := utils.NewSQLiteConfig(filePath)

conn, err := db.InitDBConn(lib.SQLITE3, sqliteConfig)
if err != nil {
return err
}
provider := db.NewSQLiteProvider(conn)

server := internal.NewServer(provider, port, make(chan bool, 1))
if err = server.Serve(); err != nil {
return err
}
return nil
Expand Down
29 changes: 27 additions & 2 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"swagger": "2.0",
"info": {
"description": "A RESTFull and GraphQL API to manage your MySQL DB",
"description": "A RESTFull and GraphQL API to supercharge your database",
"title": "MetaX",
"contact": {
"name": "Kareem Ebrahim",
"email": "[email protected]"
},
"version": "1.0"
"version": "0.1.1"
},
"host": "localhost:5522",
"basePath": "/",
Expand Down Expand Up @@ -331,6 +331,31 @@
}
},
"definitions": {
"handlers.APIInfoResult": {
"type": "object",
"properties": {
"author": {
"type": "string"
},
"contact": {
"type": "string"
},
"repo": {
"type": "string"
},
"year": {
"type": "integer"
}
}
},
"handlers.HealthCheckResult": {
"type": "object",
"properties": {
"date": {
"type": "string"
}
}
},
"models.AddModifyColumnPayload": {
"type": "object",
"required": [
Expand Down
20 changes: 18 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
basePath: /
definitions:
handlers.APIInfoResult:
properties:
author:
type: string
contact:
type: string
repo:
type: string
year:
type: integer
type: object
handlers.HealthCheckResult:
properties:
date:
type: string
type: object
models.AddModifyColumnPayload:
properties:
column_name:
Expand Down Expand Up @@ -93,9 +109,9 @@ info:
contact:
email: [email protected]
name: Kareem Ebrahim
description: A RESTFull and GraphQL API to manage your MySQL DB
description: A RESTFull and GraphQL API to supercharge your database
title: MetaX
version: "1.0"
version: 0.1.1
paths:
/:
get:
Expand Down
Loading

0 comments on commit a83c582

Please sign in to comment.