Make all slog errors uniform (lowercase)

This commit is contained in:
Maximilian 2023-09-03 15:56:35 -05:00
parent ed712a5344
commit e87f14bcbf
6 changed files with 22 additions and 22 deletions

View File

@ -33,13 +33,13 @@ func LoadConfig() Configuration {
flag.Parse() flag.Parse()
file, err := os.Open(*c) file, err := os.Open(*c)
if err != nil { if err != nil {
panic("Unable to open JSON config file: " + err.Error()) panic("unable to open JSON config file: " + err.Error())
} }
defer func(file *os.File) { defer func(file *os.File) {
err := file.Close() err := file.Close()
if err != nil { if err != nil {
slog.Error("Unable to close JSON config file: ", err) slog.Error("unable to close JSON config file: ", err)
} }
}(file) }(file)
@ -47,7 +47,7 @@ func LoadConfig() Configuration {
Config := Configuration{} Config := Configuration{}
err = decoder.Decode(&Config) err = decoder.Decode(&Config)
if err != nil { if err != nil {
panic("Unable to decode JSON config file: " + err.Error()) panic("unable to decode JSON config file: " + err.Error())
} }
return Config return Config

View File

@ -24,7 +24,7 @@ func Connect(app *app.App) *sql.DB {
panic(err) panic(err)
} }
slog.Info("Connected to database successfully on " + app.Config.Db.Ip + ":" + app.Config.Db.Port + " using database " + app.Config.Db.Name) slog.Info("connected to database successfully on " + app.Config.Db.Ip + ":" + app.Config.Db.Port + " using database " + app.Config.Db.Name)
return db return db
} }

View File

@ -39,23 +39,23 @@ func createTable(app *app.App, tableName string) error {
var tableExists bool var tableExists bool
err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ $1 AND pg_catalog.pg_table_is_visible(c.oid))", "^"+tableName+"$").Scan(&tableExists) err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ $1 AND pg_catalog.pg_table_is_visible(c.oid))", "^"+tableName+"$").Scan(&tableExists)
if err != nil { if err != nil {
slog.Error("Error checking if table exists: " + tableName) slog.Error("error checking if table exists: " + tableName)
return err return err
} }
if tableExists { if tableExists {
slog.Info("Table already exists: " + tableName) slog.Info("table already exists: " + tableName)
return nil return nil
} else { } else {
sanitizedTableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\" (\"Id\" serial primary key)", tableName) sanitizedTableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\" (\"Id\" serial primary key)", tableName)
_, err := app.Db.Query(sanitizedTableQuery) _, err := app.Db.Query(sanitizedTableQuery)
if err != nil { if err != nil {
slog.Error("Error creating table: " + tableName) slog.Error("error creating table: " + tableName)
return err return err
} }
slog.Info("Table created successfully: " + tableName) slog.Info("table created successfully: " + tableName)
return nil return nil
} }
} }
@ -65,17 +65,17 @@ func createColumn(app *app.App, tableName, columnName, columnType string) error
var columnExists bool var columnExists bool
err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)", tableName, columnName).Scan(&columnExists) err := app.Db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)", tableName, columnName).Scan(&columnExists)
if err != nil { if err != nil {
slog.Error("Error checking if column exists: " + columnName + " in table: " + tableName) slog.Error("error checking if column exists: " + columnName + " in table: " + tableName)
return err return err
} }
if columnExists { if columnExists {
slog.Info("Column already exists: " + columnName + " in table: " + tableName) slog.Info("column already exists: " + columnName + " in table: " + tableName)
return nil return nil
} else { } else {
postgresType, err := getPostgresType(columnType) postgresType, err := getPostgresType(columnType)
if err != nil { if err != nil {
slog.Error("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType) slog.Error("error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err return err
} }
@ -84,11 +84,11 @@ func createColumn(app *app.App, tableName, columnName, columnType string) error
_, err = app.Db.Query(query) _, err = app.Db.Query(query)
if err != nil { if err != nil {
slog.Error("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType) slog.Error("error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err return err
} }
slog.Info("Column created successfully:", columnName) slog.Info("column created successfully:", columnName)
return nil return nil
} }

12
main.go
View File

@ -34,14 +34,14 @@ func main() {
if _, err := os.Stat("logs"); os.IsNotExist(err) { if _, err := os.Stat("logs"); os.IsNotExist(err) {
err := os.Mkdir("logs", 0755) err := os.Mkdir("logs", 0755)
if err != nil { if err != nil {
panic("Failed to create log directory: " + err.Error()) panic("failed to create log directory: " + err.Error())
} }
} }
// Create log file and set output // Create log file and set output
file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil { if err != nil {
panic("Error creating log file: " + err.Error()) panic("error creating log file: " + err.Error())
} }
logger := slog.New(slog.NewTextHandler(file, nil)) logger := slog.New(slog.NewTextHandler(file, nil))
@ -70,10 +70,10 @@ func main() {
// Start server // Start server
server := &http.Server{Addr: appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port} server := &http.Server{Addr: appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port}
go func() { go func() {
slog.Info("Starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port) slog.Info("starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port)
err := server.ListenAndServe() err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) { if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("Could not listen on %s: %v\n", appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, err) slog.Error("could not listen on %s: %v\n", appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, err)
os.Exit(1) os.Exit(1)
} }
}() }()
@ -85,11 +85,11 @@ func main() {
go app.RunScheduledTasks(&appLoaded, 100, stop) go app.RunScheduledTasks(&appLoaded, 100, stop)
<-interrupt <-interrupt
slog.Info("Interrupt signal received. Shutting down server...") slog.Info("interrupt signal received. Shutting down server...")
err = server.Shutdown(context.Background()) err = server.Shutdown(context.Background())
if err != nil { if err != nil {
slog.Error("Could not gracefully shutdown the server: %v\n", err) slog.Error("could not gracefully shutdown the server: %v\n", err)
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -23,7 +23,7 @@ func Get(app *app.App) {
} }
staticHandler := http.FileServer(http.FS(staticFS)) staticHandler := http.FileServer(http.FS(staticFS))
http.Handle("/static/", http.StripPrefix("/static/", staticHandler)) http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
slog.Info("Serving static files from embedded file system /static") slog.Info("serving static files from embedded file system /static")
// Pages // Pages
http.HandleFunc("/", getController.ShowHome) http.HandleFunc("/", getController.ShowHome)

View File

@ -13,7 +13,7 @@ func GenerateCsrfToken(w http.ResponseWriter, _ *http.Request) (string, error) {
buff := make([]byte, int(math.Ceil(float64(64)/2))) buff := make([]byte, int(math.Ceil(float64(64)/2)))
_, err := rand.Read(buff) _, err := rand.Read(buff)
if err != nil { if err != nil {
slog.Error("Error creating random buffer for csrf token value" + err.Error()) slog.Error("error creating random buffer for csrf token value" + err.Error())
return "", err return "", err
} }
str := hex.EncodeToString(buff) str := hex.EncodeToString(buff)
@ -37,7 +37,7 @@ func GenerateCsrfToken(w http.ResponseWriter, _ *http.Request) (string, error) {
func VerifyCsrfToken(r *http.Request) (bool, error) { func VerifyCsrfToken(r *http.Request) (bool, error) {
cookie, err := r.Cookie("csrf_token") cookie, err := r.Cookie("csrf_token")
if err != nil { if err != nil {
slog.Info("Unable to get csrf_token cookie" + err.Error()) slog.Info("unable to get csrf_token cookie" + err.Error())
return false, err return false, err
} }