-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from metrico/feature/code-refector
Refactor
- Loading branch information
Showing
16 changed files
with
537 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package root | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"net/http" | ||
"quackpipe/model" | ||
"quackpipe/service/db" | ||
"quackpipe/utils" | ||
) | ||
|
||
func QueryOperation(flagInformation *model.CommandLineFlags, query string, r *http.Request, defaultPath string, defaultFormat string, defaultParams string) (string, error) { | ||
// auth to hash based temp file storage | ||
username, password, ok := r.BasicAuth() | ||
hashdb := "" | ||
if ok && len(password) > 0 { | ||
hash := sha256.Sum256([]byte(username + password)) | ||
hashdb = fmt.Sprintf("%s/%x.db", defaultPath, hash) | ||
} | ||
rows, duration, err := db.Quack(*flagInformation, query, false, defaultParams, hashdb) | ||
if err != nil { | ||
return "", err | ||
} else { | ||
result, err := utils.ConversationOfRows(rows, defaultFormat, duration) | ||
if err != nil { | ||
return "", err | ||
} | ||
return result, nil | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package handlers | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"quackpipe/controller/root" | ||
"quackpipe/model" | ||
"quackpipe/utils" | ||
) | ||
|
||
//go:embed play.html | ||
var staticPlay string | ||
|
||
type Handler struct { | ||
FlagInformation *model.CommandLineFlags | ||
} | ||
|
||
func (u *Handler) Handlers(w http.ResponseWriter, r *http.Request) { | ||
var bodyBytes []byte | ||
var query string | ||
var err error | ||
defaultFormat := *u.FlagInformation.Format | ||
defaultParams := *u.FlagInformation.Params | ||
defaultPath := *u.FlagInformation.DBPath | ||
// handle query parameter | ||
if r.URL.Query().Get("query") != "" { | ||
query = r.URL.Query().Get("query") | ||
} else if r.Body != nil { | ||
bodyBytes, err = io.ReadAll(r.Body) | ||
if err != nil { | ||
fmt.Printf("Body reading error: %v", err) | ||
return | ||
} | ||
defer r.Body.Close() | ||
query = string(bodyBytes) | ||
} | ||
|
||
switch r.Header.Get("Accept") { | ||
case "application/json": | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
case "application/xml": | ||
w.Header().Set("Content-Type", "application/xml; charset=utf-8") | ||
case "text/css": | ||
w.Header().Set("Content-Type", "text/css; charset=utf-8") | ||
default: | ||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
} | ||
// format handling | ||
if r.URL.Query().Get("default_format") != "" { | ||
defaultFormat = r.URL.Query().Get("default_format") | ||
} | ||
// param handling | ||
if r.URL.Query().Get("default_params") != "" { | ||
defaultParams = r.URL.Query().Get("default_params") | ||
} | ||
|
||
// extract FORMAT from query and override the current `default_format` | ||
cleanQuery, format := utils.ExtractAndRemoveFormat(query) | ||
if len(format) > 0 { | ||
query = cleanQuery | ||
defaultFormat = format | ||
} | ||
if len(query) == 0 { | ||
_, _ = w.Write([]byte(staticPlay)) | ||
|
||
} else { | ||
result, err := root.QueryOperation(u.FlagInformation, query, r, defaultPath, defaultFormat, defaultParams) | ||
if err != nil { | ||
_, _ = w.Write([]byte(err.Error())) | ||
} else { | ||
_, _ = w.Write([]byte(result)) | ||
} | ||
} | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"quackpipe/model" | ||
"quackpipe/router" | ||
"quackpipe/utils" | ||
) | ||
|
||
// initFlags initializes the command line flags | ||
func initFlags() *model.CommandLineFlags { | ||
|
||
appFlags := &model.CommandLineFlags{} | ||
appFlags.Host = flag.String("host", "0.0.0.0", "API host. Default 0.0.0.0") | ||
appFlags.Port = flag.String("port", "8123", "API port. Default 8123") | ||
appFlags.Format = flag.String("format", "JSONCompact", "API port. Default JSONCompact") | ||
appFlags.Params = flag.String("params", "", "DuckDB optional parameters. Default to none.") | ||
appFlags.DBPath = flag.String("dbpath", "/tmp/", "DuckDB DB storage path. Default to /tmp/") | ||
appFlags.Stdin = flag.Bool("stdin", false, "STDIN query. Default false") | ||
appFlags.Alias = flag.Bool("alias", true, "Built-in CH Aliases. Default true") | ||
flag.Parse() | ||
|
||
return appFlags | ||
} | ||
|
||
var appFlags *model.CommandLineFlags | ||
|
||
func main() { | ||
appFlags = initFlags() | ||
if *appFlags.Stdin { | ||
rows, duration, format, err := utils.ReadFromScanner(*appFlags) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
results, err := utils.ConversationOfRows(rows, format, duration) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} else { | ||
fmt.Println(results) | ||
} | ||
|
||
} else { | ||
r := router.NewRouter(appFlags) | ||
fmt.Printf("QuackPipe API Running: %s:%s\n", *appFlags.Host, *appFlags.Port) | ||
if err := http.ListenAndServe(*appFlags.Host+":"+*appFlags.Port, r); err != nil { | ||
panic(err) | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package model | ||
|
||
// params for Flags | ||
type CommandLineFlags struct { | ||
Host *string `json:"host"` | ||
Port *string `json:"port"` | ||
Stdin *bool `json:"stdin"` | ||
Alias *bool `json:"alias"` | ||
Format *string `json:"format"` | ||
Params *string `json:"params"` | ||
DBPath *string `json:"dbpath"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package model | ||
|
||
// Metadata is the metadata for a column | ||
type Metadata struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
} | ||
|
||
// Statistics is the statistics for a query | ||
type Statistics struct { | ||
Elapsed float64 `json:"elapsed"` | ||
RowsRead int `json:"rows_read"` | ||
BytesRead int `json:"bytes_read"` | ||
} | ||
|
||
// OutputJSON is the JSON output for a query | ||
type OutputJSON struct { | ||
Meta []Metadata `json:"meta"` | ||
Data [][]interface{} `json:"data"` | ||
Rows int `json:"rows"` | ||
RowsBeforeLimitAtLeast int `json:"rows_before_limit_at_least"` | ||
Statistics Statistics `json:"statistics"` | ||
} |
Oops, something went wrong.