tsk-5: Add contact (#7)
Closes #5 Reviewed-on: phoenix/textsender-api#7 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
+117
-3
@@ -1,13 +1,127 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
const PORT = 8080
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/version"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DBConnString string
|
||||
ServerPort string
|
||||
ResetDB bool
|
||||
}
|
||||
|
||||
type ConnectionInfo struct {
|
||||
Username string
|
||||
Password string
|
||||
Database string
|
||||
Host string
|
||||
Port int
|
||||
SslMode string
|
||||
}
|
||||
|
||||
const PORT = "8080"
|
||||
const APP_NAME = "textsender-api"
|
||||
|
||||
func PortString() string {
|
||||
portMessage := fmt.Sprintf(":%d", PORT)
|
||||
portMessage := fmt.Sprintf(":%s", PORT)
|
||||
|
||||
return portMessage
|
||||
}
|
||||
|
||||
func (ci ConnectionInfo) Parse() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s", ci.Username, ci.Password, ci.Host, ci.Port, ci.Database, ci.SslMode)
|
||||
}
|
||||
|
||||
func PrintName() {
|
||||
fmt.Println(APP_NAME)
|
||||
fmt.Println(version.String())
|
||||
}
|
||||
|
||||
func Load() *Config {
|
||||
versionFlag := flag.Bool("version", false, "Print version information")
|
||||
resetDb := flag.Bool("reset-db", false, "Reset the database schema and exit")
|
||||
port := flag.String("port", PORT, "Server port")
|
||||
flag.Parse()
|
||||
|
||||
if *versionFlag {
|
||||
fmt.Println(version.String())
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
unpackedConnString := UnpackDBConnString()
|
||||
dbConnString := unpackedConnString.Parse()
|
||||
|
||||
return &Config{
|
||||
DBConnString: dbConnString,
|
||||
ServerPort: *port,
|
||||
ResetDB: *resetDb,
|
||||
}
|
||||
}
|
||||
|
||||
func UnpackDBConnString() (connInfo ConnectionInfo) {
|
||||
username := os.Getenv("DB_USER")
|
||||
password := os.Getenv("DB_PASSWORD")
|
||||
host := os.Getenv("DB_HOST")
|
||||
port := os.Getenv("DB_PORT")
|
||||
database := os.Getenv("DB_NAME")
|
||||
sslMode := os.Getenv("DB_SSLMODE")
|
||||
|
||||
if username != "" {
|
||||
connInfo.Username = username
|
||||
} else {
|
||||
connInfo.Username = "user"
|
||||
}
|
||||
|
||||
if password != "" {
|
||||
connInfo.Password = password
|
||||
} else {
|
||||
connInfo.Password = "password"
|
||||
}
|
||||
|
||||
if host != "" {
|
||||
connInfo.Host = host
|
||||
} else {
|
||||
connInfo.Host = "localhost"
|
||||
}
|
||||
|
||||
if port != "" {
|
||||
num, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
connInfo.Port = num
|
||||
} else {
|
||||
connInfo.Port = 5432
|
||||
}
|
||||
|
||||
if database != "" {
|
||||
connInfo.Database = database
|
||||
} else {
|
||||
connInfo.Database = "textsender_db"
|
||||
}
|
||||
|
||||
if sslMode != "" {
|
||||
connInfo.SslMode = sslMode
|
||||
} else {
|
||||
connInfo.SslMode = "disable"
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) GetDBConnString() string {
|
||||
return c.DBConnString
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user