Skip to content

Commit 04cd333

Browse files
committed
refactored some parts
1 parent 6b3d6ed commit 04cd333

File tree

6 files changed

+77
-37
lines changed

6 files changed

+77
-37
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ databases {
117117
118118
```
119119

120-
Supported SQL Engines
121-
=====================
122-
- `sqlite3`
123-
- `mysql`
124-
- `postgresql`
125-
- `cockroachdb`
126-
- `sqlserver`
127-
128120
Supported Validation Rules
129121
==========================
130122
- Simple Validations methods with no args: [here](https://godoc.org/github.com/asaskevich/govalidator#TagMap)
@@ -142,6 +134,36 @@ Usage
142134
- You install `sqler` using the right binary for your `os` from the releases page.
143135
- Let's say that you downloaded `sqler_darwin_amd64`
144136
- Let's rename it to `sqler`, and copy it to `/usr/local/bin`
137+
- Now just run `sqler -h`, you will the next
138+
```bash
139+
____ ___ _
140+
/ ___| / _ \| | ___ _ __
141+
\___ \| | | | | / _ \ '__|
142+
___) | |_| | |__| __/ |
143+
|____/ \__\_\_____\___|_|
144+
145+
turn your SQL queries into safe valid RESTful apis.
146+
147+
148+
-config string
149+
the config file(s) that contains your endpoints configs, it accepts comma seprated list of glob style pattern (default "./config.example.hcl")
150+
-driver string
151+
the sql driver to be used (default "mysql")
152+
-dsn string
153+
the data source name for the selected engine (default "root:root@tcp(127.0.0.1)/test?multiStatements=true")
154+
-resp string
155+
the rest api listen address (default ":3678")
156+
-rest string
157+
the rest api listen address (default ":8025")
158+
-workers int
159+
the maximum workers count (default 4)
160+
```
161+
162+
- you need specify which driver you need from the following:
163+
- `mysql` its dsn: `usrname:password@tcp(server:port)/dbname?option1=value1&...`
164+
- `postgres` its dsn: `postgresql://username:password@server:port/dbname?option1=value1`
165+
- `sqlite3` its dsn: `/path/to/db.sqlite?option1=value1`
166+
- you can specifiy multiple files for `-config`, i.e `-config="/my/config/dir/*.hcl,/my/config/dir2/*.hcl"`
145167
146168
License
147169
========
File renamed without changes.

init.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,31 @@ func init() {
2929

3030
runtime.GOMAXPROCS(*flagWorkers)
3131

32-
if _, err := sqlx.Connect(*flagDBDriver, *flagDBDSN); err != nil {
33-
fmt.Println(color.RedString("[%s] - connection error - (%s)", *flagDBDriver, err.Error()))
34-
os.Exit(0)
32+
{
33+
tstconn, err := sqlx.Connect(*flagDBDriver, *flagDBDSN)
34+
if err != nil {
35+
fmt.Println(color.RedString("[%s] %s - connection error - (%s)", *flagDBDriver, *flagDBDSN, err.Error()))
36+
os.Exit(0)
37+
}
38+
tstconn.Close()
3539
}
3640

37-
manager, err := NewManager(*flagAPIFile)
38-
if err != nil {
39-
fmt.Println(color.RedString("(%s)", err.Error()))
40-
os.Exit(0)
41+
{
42+
manager, err := NewManager(*flagAPIFile)
43+
if err != nil {
44+
fmt.Println(color.RedString("(%s)", err.Error()))
45+
os.Exit(0)
46+
}
47+
macrosManager = manager
4148
}
4249

43-
macrosManager = manager
50+
{
51+
var err error
4452

45-
snow, err = snowflake.NewNode(1)
46-
if err != nil {
47-
fmt.Println(color.RedString("(%s)", err.Error()))
48-
os.Exit(0)
53+
snow, err = snowflake.NewNode(1)
54+
if err != nil {
55+
fmt.Println(color.RedString("(%s)", err.Error()))
56+
os.Exit(0)
57+
}
4958
}
5059
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
func main() {
1414
fmt.Println(color.MagentaString(sqlerBrand))
1515
fmt.Printf("⇨ sqler server version: %s \n", color.GreenString(sqlerVersion))
16+
fmt.Printf("⇨ sqler used driver is %s \n", color.GreenString(*flagDBDriver))
1617
fmt.Printf("⇨ sqler used dsn is %s \n", color.GreenString(*flagDBDSN))
1718
fmt.Printf("⇨ sqler workers count: %s \n", color.GreenString(strconv.Itoa(*flagWorkers)))
1819
fmt.Printf("⇨ sqler resp server available at: %s \n", color.GreenString(*flagRESPListenAddr))

manager.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package main
55

66
import (
7+
"fmt"
78
"io/ioutil"
89
"path/filepath"
10+
"strings"
911
"text/template"
1012

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

26-
files, _ := filepath.Glob(configpath)
28+
for _, p := range strings.Split(configpath, ",") {
29+
files, _ := filepath.Glob(p)
2730

28-
for _, file := range files {
29-
data, err := ioutil.ReadFile(file)
30-
if err != nil {
31-
return nil, err
31+
if len(files) < 1 {
32+
return nil, fmt.Errorf("invalid path (%s)", p)
3233
}
3334

34-
var config map[string]*Macro
35-
if err := hcl.Unmarshal(data, &config); err != nil {
36-
return nil, err
37-
}
38-
39-
for k, v := range config {
40-
manager.macros[k] = v
41-
_, err := manager.compiled.New(k).Parse(v.Exec)
35+
for _, file := range files {
36+
data, err := ioutil.ReadFile(file)
4237
if err != nil {
4338
return nil, err
4439
}
45-
v.compiled = manager.compiled
46-
v.name = k
40+
41+
var config map[string]*Macro
42+
if err := hcl.Unmarshal(data, &config); err != nil {
43+
return nil, err
44+
}
45+
46+
for k, v := range config {
47+
manager.macros[k] = v
48+
_, err := manager.compiled.New(k).Parse(v.Exec)
49+
if err != nil {
50+
return nil, err
51+
}
52+
v.compiled = manager.compiled
53+
v.name = k
54+
}
4755
}
4856
}
4957

vars.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
)
1212

1313
var (
14-
flagDBDriver = flag.String("engine", "mysql", "the sql engine/driver to be used")
14+
flagDBDriver = flag.String("driver", "mysql", "the sql driver to be used")
1515
flagDBDSN = flag.String("dsn", "root:root@tcp(127.0.0.1)/test?multiStatements=true", "the data source name for the selected engine")
16-
flagAPIFile = flag.String("api", "./api.example.hcl", "the validators used before processing the sql, it accepts a glob style pattern")
16+
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")
1717
flagRESTListenAddr = flag.String("rest", ":8025", "the rest api listen address")
1818
flagRESPListenAddr = flag.String("resp", ":3678", "the rest api listen address")
1919
flagWorkers = flag.Int("workers", runtime.NumCPU(), "the maximum workers count")

0 commit comments

Comments
 (0)