Permalink
Browse files

refactored some parts

  • Loading branch information...
alash3al committed Jan 10, 2019
1 parent 6b3d6ed commit 04cd333e16a7e54f8a7b0cb9e08d8f96c53709ff
Showing with 77 additions and 37 deletions.
  1. +30 −8 README.md
  2. 0 api.example.hcl → config.example.hcl
  3. +21 −12 init.go
  4. +1 −0 main.go
  5. +23 −15 manager.go
  6. +2 −2 vars.go
@@ -117,14 +117,6 @@ databases {
```

Supported SQL Engines
=====================
- `sqlite3`
- `mysql`
- `postgresql`
- `cockroachdb`
- `sqlserver`

Supported Validation Rules
==========================
- Simple Validations methods with no args: [here](https://godoc.org/github.com/asaskevich/govalidator#TagMap)
@@ -142,6 +134,36 @@ Usage
- You install `sqler` using the right binary for your `os` from the releases page.
- Let's say that you downloaded `sqler_darwin_amd64`
- Let's rename it to `sqler`, and copy it to `/usr/local/bin`
- Now just run `sqler -h`, you will the next
```bash
____ ___ _
/ ___| / _ \| | ___ _ __
\___ \| | | | | / _ \ '__|
___) | |_| | |__| __/ |
|____/ \__\_\_____\___|_|
turn your SQL queries into safe valid RESTful apis.
-config string
the config file(s) that contains your endpoints configs, it accepts comma seprated list of glob style pattern (default "./config.example.hcl")
-driver string
the sql driver to be used (default "mysql")
-dsn string
the data source name for the selected engine (default "root:root@tcp(127.0.0.1)/test?multiStatements=true")
-resp string
the rest api listen address (default ":3678")
-rest string
the rest api listen address (default ":8025")
-workers int
the maximum workers count (default 4)
```
- you need specify which driver you need from the following:
- `mysql` its dsn: `usrname:password@tcp(server:port)/dbname?option1=value1&...`
- `postgres` its dsn: `postgresql://username:password@server:port/dbname?option1=value1`
- `sqlite3` its dsn: `/path/to/db.sqlite?option1=value1`
- you can specifiy multiple files for `-config`, i.e `-config="/my/config/dir/*.hcl,/my/config/dir2/*.hcl"`
License
========
File renamed without changes.
33 init.go
@@ -29,22 +29,31 @@ func init() {

runtime.GOMAXPROCS(*flagWorkers)

if _, err := sqlx.Connect(*flagDBDriver, *flagDBDSN); err != nil {
fmt.Println(color.RedString("[%s] - connection error - (%s)", *flagDBDriver, err.Error()))
os.Exit(0)
{
tstconn, err := sqlx.Connect(*flagDBDriver, *flagDBDSN)
if err != nil {
fmt.Println(color.RedString("[%s] %s - connection error - (%s)", *flagDBDriver, *flagDBDSN, err.Error()))
os.Exit(0)
}
tstconn.Close()
}

manager, err := NewManager(*flagAPIFile)
if err != nil {
fmt.Println(color.RedString("(%s)", err.Error()))
os.Exit(0)
{
manager, err := NewManager(*flagAPIFile)
if err != nil {
fmt.Println(color.RedString("(%s)", err.Error()))
os.Exit(0)
}
macrosManager = manager
}

macrosManager = manager
{
var err error

snow, err = snowflake.NewNode(1)
if err != nil {
fmt.Println(color.RedString("(%s)", err.Error()))
os.Exit(0)
snow, err = snowflake.NewNode(1)
if err != nil {
fmt.Println(color.RedString("(%s)", err.Error()))
os.Exit(0)
}
}
}
@@ -13,6 +13,7 @@ import (
func main() {
fmt.Println(color.MagentaString(sqlerBrand))
fmt.Printf("⇨ sqler server version: %s \n", color.GreenString(sqlerVersion))
fmt.Printf("⇨ sqler used driver is %s \n", color.GreenString(*flagDBDriver))
fmt.Printf("⇨ sqler used dsn is %s \n", color.GreenString(*flagDBDSN))
fmt.Printf("⇨ sqler workers count: %s \n", color.GreenString(strconv.Itoa(*flagWorkers)))
fmt.Printf("⇨ sqler resp server available at: %s \n", color.GreenString(*flagRESPListenAddr))
@@ -4,8 +4,10 @@
package main

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"text/template"

"github.com/hashicorp/hcl"
@@ -23,27 +25,33 @@ func NewManager(configpath string) (*Manager, error) {
manager.macros = make(map[string]*Macro)
manager.compiled = template.New("main")

files, _ := filepath.Glob(configpath)
for _, p := range strings.Split(configpath, ",") {
files, _ := filepath.Glob(p)

for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
if len(files) < 1 {
return nil, fmt.Errorf("invalid path (%s)", p)
}

var config map[string]*Macro
if err := hcl.Unmarshal(data, &config); err != nil {
return nil, err
}

for k, v := range config {
manager.macros[k] = v
_, err := manager.compiled.New(k).Parse(v.Exec)
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
v.compiled = manager.compiled
v.name = k

var config map[string]*Macro
if err := hcl.Unmarshal(data, &config); err != nil {
return nil, err
}

for k, v := range config {
manager.macros[k] = v
_, err := manager.compiled.New(k).Parse(v.Exec)
if err != nil {
return nil, err
}
v.compiled = manager.compiled
v.name = k
}
}
}

@@ -11,9 +11,9 @@ import (
)

var (
flagDBDriver = flag.String("engine", "mysql", "the sql engine/driver to be used")
flagDBDriver = flag.String("driver", "mysql", "the sql driver to be used")
flagDBDSN = flag.String("dsn", "root:root@tcp(127.0.0.1)/test?multiStatements=true", "the data source name for the selected engine")
flagAPIFile = flag.String("api", "./api.example.hcl", "the validators used before processing the sql, it accepts a glob style pattern")
flagAPIFile = flag.String("config", "./config.example.hcl", "the config file(s) that contains your endpoints configs, it accepts comma seprated list of glob style pattern")
flagRESTListenAddr = flag.String("rest", ":8025", "the rest api listen address")
flagRESPListenAddr = flag.String("resp", ":3678", "the rest api listen address")
flagWorkers = flag.Int("workers", runtime.NumCPU(), "the maximum workers count")

0 comments on commit 04cd333

Please sign in to comment.