Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
updated config settings
Browse files Browse the repository at this point in the history
  • Loading branch information
paraswaykole committed Jan 9, 2023
1 parent 63407d1 commit f4d59ee
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 73 deletions.
2 changes: 1 addition & 1 deletion cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func handleCmd(cmdText string) {
}

func printHelp() {
fmt.Println("To add a new database use the IDE interface running at https://localhost:" + config.GetServerPort())
fmt.Println("To add a new database use the IDE interface running at https://localhost:" + config.GetConfig().Port)
fmt.Println("To connect to existing db type '\\base db-nick-name'.")
fmt.Println("Once connected to db, type your query and press enter to get query results.")
fmt.Println("To end the program, type 'exit'.")
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var longDescription = `Slashbase is a modern in-browser database IDE & CLI for your dev/data workflows.
Use Slashbase to connect to your database, browse data and schema, write,
run and save queries, create charts, right from your browser.
Connects to Slashbase IDE at https://app.slashbase.com`
Connects to Slashbase IDE at https://local.slashbase.com`

func display(input string) string {
if cliApp.CurrentDB == nil {
Expand Down Expand Up @@ -49,7 +49,7 @@ var rootCmd = &cobra.Command{
DisableDefaultCmd: true,
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Access Slashbase IDE at http://localhost:" + config.GetServerPort())
fmt.Println("Access Slashbase IDE at http://localhost:" + config.GetConfig().Port)
fmt.Println("Type 'help' for more info on cli.")
setup.SetupApp()
queryengines.Init()
Expand Down
87 changes: 83 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package config

import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"

"github.com/joho/godotenv"
"github.com/slashbaseide/slashbase/internal/utils"
)

var config AppConfig
Expand All @@ -14,6 +21,11 @@ func Init(buildName, version string) {
if err != nil {
log.Fatal("Error loading development.env file")
}
} else if buildName == BUILD_PRODUCTION {
err := godotenv.Load(GetAppEnvFilePath())
if err != nil {
log.Fatal("Error loading .env file")
}
}
config = newConfig(buildName, version)
}
Expand All @@ -26,9 +38,76 @@ func GetConfig() *AppConfig {
return &config
}

func GetServerPort() string {
if config.Port == "" {
return DEFAULT_SERVER_PORT
func GetAppEnvFilePath() string {
var filePath string
if runtime.GOOS == "windows" {
// Get the %LOCALAPPDATA% path
localAppData := os.Getenv("LOCALAPPDATA")
// Set the file name and path
filePath = filepath.Join(localAppData, app_name, app_env_file)
} else if runtime.GOOS == "darwin" {
// Get the user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
filePath = filepath.Join(homeDir, "Library", "Application Support", app_name, app_env_file)
} else if runtime.GOOS == "linux" {
filePath = filepath.Join("/usr/local", app_name, app_env_file)
} else {
panic(errors.New("not implemented"))
}
err := os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
panic(err)
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
err = createEnvFile(filePath)
if err != nil {
panic(err)
}
}
return filePath
}

func GetAppDatabaseFilePath() string {
if !IsLive() {
return app_db_file
}
var filePath string
if runtime.GOOS == "windows" {
// Get the %LOCALAPPDATA% path
localAppData := os.Getenv("LOCALAPPDATA")
// Set the file name and path
filePath = filepath.Join(localAppData, app_name, app_db_file)
} else if runtime.GOOS == "darwin" {
// Get the user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
filePath = filepath.Join(homeDir, "Library", "Application Support", app_name, app_db_file)
} else if runtime.GOOS == "linux" {
filePath = filepath.Join("/usr/local", app_name, app_db_file)
} else {
panic(errors.New("not implemented"))
}
err := os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
panic(err)
}
return filePath
}

func createEnvFile(filePath string) error {
hex, err := utils.RandomHex(32)
if err != nil {
return err
}
envFileData := fmt.Sprintf(`CRYPTED_DATA_SECRET=%s`, hex)
err = ioutil.WriteFile(filePath, []byte(envFileData), 0700)
if err != nil {
return err
}
return config.Port
return nil
}
54 changes: 3 additions & 51 deletions internal/config/constants.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,14 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
)

const (
PAGINATION_COUNT = 20

app_name = "slashbase"
app_db_file = "app.db"
app_name = "slashbase"
app_db_file = "app.db"
app_env_file = ".env"

BUILD_PRODUCTION = "production"
BUILD_DEVELOPMENT = "development"

DEFAULT_SERVER_PORT = "22022"
)

func GetAppDatabaseFilePath() string {
if !IsLive() {
return app_db_file
}
var filePath string
if runtime.GOOS == "windows" {
// Get the %LOCALAPPDATA% path
localAppData := os.Getenv("LOCALAPPDATA")
// Set the file name and path
filePath = filepath.Join(localAppData, app_name, app_db_file)
} else if runtime.GOOS == "darwin" {
// Get the user's home directory
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
filePath = filepath.Join(homeDir, "Library", "Application Support", app_name, app_db_file)
} else if runtime.GOOS == "linux" {
filePath = filepath.Join("/usr/local", app_name, app_db_file)
} else {
panic(errors.New("not implemented"))
}
err := os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
panic(err)
}
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath, _ := filepath.EvalSymlinks(ex)
exPath = filepath.Dir(exPath)
exPath = exPath + "/" + app_db_file
if _, err := os.Stat(exPath); !os.IsNotExist(err) {
if err := os.Rename(exPath, filePath); err != nil {
fmt.Println(err)
}
}
return filePath
}
15 changes: 2 additions & 13 deletions internal/config/model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package config

import (
"log"
"os"

"github.com/slashbaseide/slashbase/internal/utils"
)

type AppConfig struct {
Expand All @@ -15,18 +12,10 @@ type AppConfig struct {
}

func newConfig(buildName, version string) AppConfig {
cryptedDataSecret := os.Getenv("CRYPTED_DATA_SECRET")
if cryptedDataSecret == "" {
hex, err := utils.RandomHex(32)
if err != nil {
log.Fatal("env CRYPTED_DATA_SECRET not found")
}
cryptedDataSecret = hex
}
return AppConfig{
Version: version,
BuildName: buildName,
Port: os.Getenv("PORT"),
CryptedDataSecret: cryptedDataSecret,
Port: DEFAULT_SERVER_PORT,
CryptedDataSecret: os.Getenv("CRYPTED_DATA_SECRET"),
}
}
4 changes: 2 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func Init() {
gin.SetMode(gin.ReleaseMode)
}
if config.IsLive() {
osx.OpenDefault("http://localhost:" + config.GetServerPort())
osx.OpenDefault("http://localhost:" + config.GetConfig().Port)
}
router := NewRouter()
go router.Run(":" + config.GetServerPort())
go router.Run(":" + config.GetConfig().Port)
}

0 comments on commit f4d59ee

Please sign in to comment.