From 4c36742ec8072e645f0b755cdf5e9582ac2a6887 Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Sat, 11 Feb 2023 18:06:52 +0100
Subject: Rework logging

Logging during a request is at the moment not really needed. Printing
the config to the log was a stupid idea too.
---
 config.go   | 10 +++++++---
 database.go |  2 ++
 handler.go  |  8 --------
 main.go     |  2 +-
 server.go   |  2 +-
 5 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/config.go b/config.go
index 9302e11..645e347 100644
--- a/config.go
+++ b/config.go
@@ -2,12 +2,12 @@
 package main
 
 import (
-	"fmt"
 	"log"
 	"flag"
 	"os"
 	"io/ioutil"
 	"encoding/json"
+	"path/filepath"
 )
 
 type RuntimeConfig struct {
@@ -53,13 +53,17 @@ func GetRuntimeConfig() RuntimeConfig {
 		log.Fatalf("Could not read configuration file %s", config.Path)
 	}
 
-	fmt.Print("Used config: " + string(configData) + "\n")
-
 	// parse content to config structs
 	err = json.Unmarshal(configData, &config)
 	if err != nil {
 		log.Fatalf("Could not parse configuration file %s", config.Path)
 	}
 
+	abs,err := filepath.Abs(config.Path)
+	if err != nil {
+		log.Fatalf("Could not translate %s to absolute path.", config.Path)
+	}
+	log.Printf("Config file: %s\n", abs)
+
 	return config
 }
diff --git a/database.go b/database.go
index 1efd22e..ea11cfa 100644
--- a/database.go
+++ b/database.go
@@ -52,6 +52,8 @@ func setupDatabase() *sql.DB {
 		os.Exit(0)  // TODO this does not belong to a database - write utils file 'shutdown.go'
 	}()
 
+	log.Printf("Connected to database: %s\n", target)
+
 	return db
 }
 
diff --git a/handler.go b/handler.go
index 0899797..5a3883d 100644
--- a/handler.go
+++ b/handler.go
@@ -5,7 +5,6 @@ import (
 	"bytes"
 	"fmt"
 	"regexp"
-	"log"
 	"io/ioutil"
 	"net/http"
 	"path/filepath"
@@ -21,7 +20,6 @@ func indexGet(w http.ResponseWriter, r *http.Request) {
 
 	// get data from database
 	cmd := "SELECT id,title FROM recipes ORDER BY title;"
-	log.Printf("Query: %s", cmd)
 	rows, err := db.Query(cmd)
 	if err != nil {
 		http.Error(w, "Failed to load recipes from database.", 500)
@@ -73,7 +71,6 @@ func recipeGet(w http.ResponseWriter, r *http.Request) {
 
 	// get data from database
 	cmd := fmt.Sprintf("SELECT title,upstream_url,description_markdown FROM recipes WHERE (id='%s');", idStr)
-	log.Printf("Query: %s", cmd)
 	rows, err := db.Query(cmd)
 	if err != nil {
 		http.Error(w, "Database returned error: " + err.Error(), 500)
@@ -163,7 +160,6 @@ func recipeEditGet(w http.ResponseWriter, r *http.Request) {
 
 	// get data from database
 	cmd := fmt.Sprintf("SELECT title,upstream_url,description_markdown FROM recipes WHERE (id='%s');", idStr)
-	log.Printf("Query: %s", cmd)
 	rows, err := db.Query(cmd)
 	if err != nil {
 		http.Error(w, "Got error from database: " + err.Error(), 500)
@@ -271,7 +267,6 @@ func addRecipesGet(w http.ResponseWriter, r *http.Request) {
 
 	filename := "add.html"
 	path := filepath.Join(config.Http.Static, filename)
-	log.Printf("Trying to serve: %s", path)
 	http.ServeFile(w, r, path)
 }
 
@@ -281,7 +276,6 @@ func addRecipesPost(w http.ResponseWriter, r *http.Request) {
 	title := r.FormValue("title")
 
 	cmd := fmt.Sprintf("INSERT INTO recipes (title,upstream_url) VALUES ('%s', '%s')", title, url)
-	log.Println(cmd)
 	res,err := db.Exec(cmd)
 	if err != nil {
 		http.Error(w, "Could not add recipe.", 500)
@@ -292,7 +286,6 @@ func addRecipesPost(w http.ResponseWriter, r *http.Request) {
 		http.Error(w, "Expected exactly one recipe URL.", 400)
 		return
 	} else {
-		log.Println("Added custom recipe.")
 		redirect := fmt.Sprintf("/recipe?id=%d", id)
 		http.Redirect(w, r, redirect, 303)
 		return
@@ -302,6 +295,5 @@ func addRecipesPost(w http.ResponseWriter, r *http.Request) {
 func staticGet(w http.ResponseWriter, r *http.Request, filename string) {
 
 	path := filepath.Join(config.Http.Static, filename)
-	log.Printf("Trying to serve: %s\n", path)
 	http.ServeFile(w, r, path)
 }
diff --git a/main.go b/main.go
index 3afdf49..405ff0a 100644
--- a/main.go
+++ b/main.go
@@ -10,8 +10,8 @@ var config RuntimeConfig
 var db *sql.DB
 
 func main() {
+	log.Printf("Started Ceres recipe server.\n")
 	config = GetRuntimeConfig()
-	log.Printf("Starting ceres with config file '%s'\n", config.Path)
 	db = setupDatabase()
 	runServer()
 }
diff --git a/server.go b/server.go
index f565636..ff581e3 100644
--- a/server.go
+++ b/server.go
@@ -21,6 +21,6 @@ func runServer() {
 
 	setupRoutes()
 	address := config.Http.Host + ":" + config.Http.Port
-	log.Println("Binding to 'http://" + address)
+	log.Println("Serving content at 'http://" + address + "'.")
 	log.Fatal(http.ListenAndServe(address, nil))
 }
-- 
cgit v1.2.3-70-g09d2