Fix various json parsing and sql generation bugs

This commit is contained in:
Vikram Rangnekar
2020-01-19 03:12:51 -05:00
parent 8097ca3b8f
commit a0b8907c3c
16 changed files with 180 additions and 57 deletions

View File

@ -128,10 +128,10 @@ func (al *allowList) upsert(query, vars []byte, uri string) {
var key string
if len(name) == 0 {
key = hash
} else {
if len(name) != 0 {
key = name
} else {
key = hash
}
if i, ok := al.index[key]; !ok {
@ -280,7 +280,7 @@ func (al *allowList) save(item *allowItem) {
for i := range v {
if len(v[i].vars) != 0 && !bytes.Equal(v[i].vars, []byte("{}")) {
vj, err := json.MarshalIndent(v[i].vars, "", "\t")
vj, err := json.MarshalIndent(v[i].vars, "", " ")
if err != nil {
logger.Warn().Err(err).Msg("Failed to write allow list 'vars' to file")
continue

View File

@ -35,6 +35,8 @@ func argMap(ctx context.Context, vars []byte) func(w io.Writer, tag string) (int
fields := jsn.Get(vars, [][]byte{[]byte(tag)})
fmt.Println(">>", tag, string(vars))
if len(fields) == 0 {
return 0, nil
}

View File

@ -324,7 +324,7 @@ Branch : %v
Go version : %v
Licensed under the Apache Public License 2.0
Copyright 2015-2019 Vikram Rangnekar.
Copyright 2020, Vikram Rangnekar.
`,
version,
lastCommitSHA,

View File

@ -63,7 +63,7 @@ func cmdDBCreate(cmd *cobra.Command, args []string) {
}
defer conn.Close(ctx)
sql := fmt.Sprintf("CREATE DATABASE %s", conf.DB.DBName)
sql := fmt.Sprintf(`CREATE DATABASE "%s"`, conf.DB.DBName)
_, err = conn.Exec(ctx, sql)
if err != nil {
@ -83,7 +83,7 @@ func cmdDBDrop(cmd *cobra.Command, args []string) {
}
defer conn.Close(ctx)
sql := fmt.Sprintf(`DROP DATABASE IF EXISTS %s`, conf.DB.DBName)
sql := fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, conf.DB.DBName)
_, err = conn.Exec(ctx, sql)
if err != nil {

View File

@ -8,17 +8,19 @@ func cmdServ(cmd *cobra.Command, args []string) {
var err error
if conf, err = initConf(); err != nil {
errlog.Fatal().Err(err).Msg("failed to read config")
fatalInProd(err, "failed to read config")
}
db, err = initDBPool(conf)
if err != nil {
errlog.Fatal().Err(err).Msg("failed to connect to database")
if conf != nil {
if db, err = initDBPool(conf); err != nil {
fatalInProd(err, "failed to connect to database")
}
initCompiler()
initAllowList(confPath)
initPreparedList()
}
initCompiler()
initAllowList(confPath)
initPreparedList()
initWatcher(confPath)
startHTTP()

View File

@ -108,7 +108,7 @@ func Do(log func(string, ...interface{}), additional ...dir) error {
// Ensure that we use the correct events, as they are not uniform across
// platforms. See https://github.com/fsnotify/fsnotify/issues/74
if !conf.Production && strings.HasSuffix(event.Name, "/allow.list") {
if conf != nil && !conf.Production && strings.HasSuffix(event.Name, "/allow.list") {
continue
}

View File

@ -50,7 +50,7 @@ func initCompilers(c *config) (*qcode.Compiler, *psql.Compiler, error) {
}
func initWatcher(cpath string) {
if !conf.WatchAndReload {
if conf != nil && !conf.WatchAndReload {
return
}
@ -70,18 +70,33 @@ func initWatcher(cpath string) {
}
func startHTTP() {
hp := strings.SplitN(conf.HostPort, ":", 2)
var hostPort string
var appName string
if len(conf.Host) != 0 {
hp[0] = conf.Host
defaultHP := "0.0.0.0:8080"
env := os.Getenv("GO_ENV")
if conf != nil {
appName = conf.AppName
hp := strings.SplitN(conf.HostPort, ":", 2)
if len(hp) == 2 {
if len(conf.Host) != 0 {
hp[0] = conf.Host
}
if len(conf.Port) != 0 {
hp[1] = conf.Port
}
hostPort = fmt.Sprintf("%s:%s", hp[0], hp[1])
}
}
if len(conf.Port) != 0 {
hp[1] = conf.Port
if len(hostPort) == 0 {
hostPort = defaultHP
}
hostPort := fmt.Sprintf("%s:%s", hp[0], hp[1])
srv := &http.Server{
Addr: hostPort,
Handler: routeHandler(),
@ -110,8 +125,8 @@ func startHTTP() {
Str("version", version).
Str("git_branch", gitBranch).
Str("host_post", hostPort).
Str("app_name", conf.AppName).
Str("env", conf.Env).
Str("app_name", appName).
Str("env", env).
Msgf("%s listening", serverName)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
@ -124,7 +139,7 @@ func startHTTP() {
func routeHandler() http.Handler {
var apiH http.Handler
if conf.HTTPGZip {
if conf != nil && conf.HTTPGZip {
gzipH := gziphandler.MustNewGzipLevelHandler(6)
apiH = gzipH(http.HandlerFunc(apiV1))
} else {
@ -132,11 +147,14 @@ func routeHandler() http.Handler {
}
mux := http.NewServeMux()
mux.HandleFunc("/health", health)
mux.Handle("/api/v1/graphql", withAuth(apiH))
if conf.WebUI {
mux.Handle("/", http.FileServer(rice.MustFindBox("../web/build").HTTPBox()))
if conf != nil {
mux.HandleFunc("/health", health)
mux.Handle("/api/v1/graphql", withAuth(apiH))
if conf.WebUI {
mux.Handle("/", http.FileServer(rice.MustFindBox("../web/build").HTTPBox()))
}
}
fn := func(w http.ResponseWriter, r *http.Request) {
@ -170,3 +188,7 @@ func getConfigName() string {
return ge
}
func isDev() bool {
return strings.HasPrefix(os.Getenv("GO_ENV"), "dev")
}

View File

@ -141,3 +141,11 @@ func findStmt(role string, stmts []stmt) *stmt {
}
return nil
}
func fatalInProd(err error, msg string) {
if isDev() {
errlog.Error().Err(err).Msg(msg)
} else {
errlog.Fatal().Err(err).Msg(msg)
}
}