From 52331b2fa0a4c9ebd2aea61cda202d90d05b5e0a Mon Sep 17 00:00:00 2001 From: xoy Date: Sat, 28 Jan 2023 21:43:07 +0100 Subject: [PATCH] =?UTF-8?q?errorhandling=20und=20logging=20eingebunden,=20?= =?UTF-8?q?code=20=C3=BCberarbeitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/http.yml | 1 + database.go | 9 +---- file.go | 14 +++++-- func.go | 100 +++++++++++++++++++++++++++++++----------------- http.go | 8 ++-- main.go | 15 ++------ 6 files changed, 84 insertions(+), 63 deletions(-) create mode 100644 config/http.yml diff --git a/config/http.yml b/config/http.yml new file mode 100644 index 0000000..90829f7 --- /dev/null +++ b/config/http.yml @@ -0,0 +1 @@ +address: :80 \ No newline at end of file diff --git a/database.go b/database.go index aec9354..863c4f8 100644 --- a/database.go +++ b/database.go @@ -8,10 +8,7 @@ import ( func dbConnect(username string, password string, address string, port string, database string) *sql.DB { db, err := sql.Open("mysql", username+":"+password+"@tcp("+address+":"+port+")/"+database) - - if err != nil { - panic(err.Error()) - } + errorPanic(err) return db } @@ -22,9 +19,7 @@ func dbClose(database *sql.DB) { func dbQuerry(database *sql.DB, sqlCode string) *sql.Rows { results, err := database.Query(sqlCode) - if err != nil { - panic(err.Error()) - } + errorPanic(err) return results } diff --git a/file.go b/file.go index 6648025..08d237d 100644 --- a/file.go +++ b/file.go @@ -2,15 +2,21 @@ package main import ( "io/ioutil" - "log" + "os" ) func fileRead(src string) string { content, err := ioutil.ReadFile(src) - if err != nil { - log.Fatal(err.Error()) - } + errorPanic(err) return string(content) } + +func fileAddLine(input string, filepath string) { + file, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + errorPanic(err) + + _, err = file.WriteString(input + "\n") + errorPanic(err) +} diff --git a/func.go b/func.go index 2c5b254..9de5c20 100644 --- a/func.go +++ b/func.go @@ -2,35 +2,44 @@ package main import ( "io" - "math/rand" "net/http" "strconv" "strings" "time" ) +func logger(input string) { + println(input) + fileAddLine(input, "./log/"+time.Now().Format("2006-02-01")+".log") +} + func handler() { - //Pages + logger("Pages:") + + //pages httpHandleFunc("", "./web/pages/home.html", "text/html") httpHandleFunc("home", "./web/pages/home.html", "text/html") httpHandleFunc("treff", "./web/pages/treff.html", "text/html") httpHandleFunc("events", "./web/pages/events.html", "text/html") httpHandleFunc("about", "./web/pages/about.html", "text/html") + //contact pages httpHandleFunc("kontakt", "./web/pages/kontakt.html", "text/html") httpHandleFunc("kontakt/adresse", "./web/pages/kontakt/adresse.html", "text/html") httpHandleFunc("kontakt/irc", "./web/pages/kontakt/irc.html", "text/html") httpHandleFunc("kontakt/mail", "./web/pages/kontakt/mail.html", "text/html") httpHandleFunc("kontakt/tel", "./web/pages/kontakt/tel.html", "text/html") + //pages httpHandleFunc("verein", "./web/pages/verein.html", "text/html") httpHandleFunc("support", "./web/pages/support.html", "text/html") - httpHandleFunc("impressum", "./web/pages/impressum.html", "text/html") httpHandleFunc("datenschutz", "./web/pages/datenschutz.html", "text/html") + //admin pages keys := getAdminKeys() + //this check is necessary! if keys != nil { for _, key := range keys { httpHandleFunc("admin/"+key, "./web/pages/admin/dashboard.html", "text/html") @@ -38,13 +47,13 @@ func handler() { } } - //Styles + //styles httpHandleFunc("style/main.css", "./web/styles/main.css", "text/css") httpHandleFunc("style/kontakt.css", "./web/styles/kontakt.css", "text/css") httpHandleFunc("style/home.css", "./web/styles/home.css", "text/css") httpHandleFunc("style/events.css", "./web/styles/events.css", "text/css") - //Images + //images httpHandleFunc("image/logo_ctdo.svg", "./web/images/logo_ctdo.svg", "image/svg+xml") httpHandleFunc("image/header.jpg", "./web/images/header.jpg", "image/jpeg") httpHandleFunc("image/adresse_knopf.webp", "./web/images/adresse_knopf.webp", "image/webp") @@ -84,10 +93,9 @@ func getRoomState() status { } defer r.Body.Close() - body, _err := io.ReadAll(r.Body) - if _err != nil { - panic(_err.Error()) - } + var body []byte + body, err = io.ReadAll(r.Body) + errorPanic(err) bodyString := string(body) @@ -96,21 +104,18 @@ func getRoomState() status { bodyString = strings.ReplaceAll(bodyString, "{", "") bodyString = strings.ReplaceAll(bodyString, "}", "") - _temp := strings.Split(bodyString, ",") + Temp := strings.Split(bodyString, ",") - for _, element := range _temp { - __temp := strings.Split(element, ":") - temp = append(temp, __temp[1]) + for _, element := range Temp { + TEmp := strings.Split(element, ":") + temp = append(temp, TEmp[1]) } roomState := new(status) roomState.state = temp[0] == "true" - var __err error - roomState.power, __err = strconv.ParseInt(temp[2], 0, 64) - if __err != nil { - panic(__err.Error()) - } + roomState.power, err = strconv.ParseInt(temp[2], 0, 64) + errorPanic(err) return *roomState } @@ -211,14 +216,10 @@ func getNextTopic() topic { if newDate[0] == "Thu" || newDate[0] == "Tue" { dayA, errA := strconv.Atoi(newDate[2]) - if errA != nil { - panic(errA.Error()) - } + errorPanic(errA) dayB, errB := strconv.Atoi(newDate[2]) - if errB != nil { - panic(errB.Error()) - } + errorPanic(errB) if ifFloatRange(float64(dayA)/7, 0, 1, false, true) || (ifFloatRange(float64(dayB)/7, 2, 3, false, true) && newDate[0] == "Tue") { output.date = date.AddDate(0, 0, 1*i).Format("02.01.2006") @@ -246,34 +247,61 @@ func readDatabaseYML() database { switch i { case 0: output.username = row[1] - break case 1: output.password = row[1] - break case 2: output.address = row[1] - break case 3: output.port = row[1] - break case 4: output.database = row[1] - break + default: + logger("func.go:259 -> switch-case is out of range") } } return *output } -func generateRandomString(length int) string { - chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - chars += strings.ToLower(chars) +func readHttpYML() string { + file := fileRead("./config/http.yml") - output := "" + rows := [][]string{} - for i := 0; i < length; i++ { - output += string(chars[rand.Intn(len(chars)-1)]) + for _, row := range strings.Split(file, "\n") { + rows = append(rows, strings.Split(row, ": ")) } - return output + for i, row := range rows { + switch i { + case 0: + return row[1] + default: + logger("func.go:280 -> switch-case is out of range") + } + } + + return "" +} + +/* + func generateRandomString(length int) string { + chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + chars += strings.ToLower(chars) + + output := "" + + for i := 0; i < length; i++ { + output += string(chars[rand.Intn(len(chars)-1)]) + } + + return output + } +*/ + +func errorPanic(err error) { + if err != nil { + logger(err.Error()) + panic("----------------ERROR----------------") + } } diff --git a/http.go b/http.go index b72c771..02bcc65 100644 --- a/http.go +++ b/http.go @@ -6,8 +6,7 @@ import ( ) func httpHandleFunc(urlPath string, filepath string, contentType string) { - s := new(submit) - s.data = "null" + logger("-->" + urlPath + " " + filepath + " " + contentType) http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", contentType) @@ -20,9 +19,8 @@ func httpHandleFuncWithPOST(urlPath string, filepath string, contentType string) s.data = "null" http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { - if err := r.ParseForm(); err != nil { - panic(err.Error()) - } + err := r.ParseForm() + errorPanic(err) } w.Header().Add("Content-Type", contentType) diff --git a/main.go b/main.go index 0ab12e4..4b3fe88 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,16 @@ package main import ( - "errors" - "fmt" "net/http" - "os" ) func main() { - addr := ":80" + addr := readHttpYML() + + logger("Connection open with address " + addr) handler() err := http.ListenAndServe(addr, nil) - - if errors.Is(err, http.ErrServerClosed) { - fmt.Printf("server closed\n") - } else if err != nil { - fmt.Printf("error starting server: %s\n", err) - os.Exit(1) - } + errorPanic(err) }