errorhandling und logging eingebunden, code überarbeitet
This commit is contained in:
parent
c3d246c81c
commit
52331b2fa0
|
@ -0,0 +1 @@
|
|||
address: :80
|
|
@ -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
|
||||
}
|
||||
|
|
14
file.go
14
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)
|
||||
}
|
||||
|
|
88
func.go
88
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,26 +247,45 @@ 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 {
|
||||
func readHttpYML() string {
|
||||
file := fileRead("./config/http.yml")
|
||||
|
||||
rows := [][]string{}
|
||||
|
||||
for _, row := range strings.Split(file, "\n") {
|
||||
rows = append(rows, strings.Split(row, ": "))
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
|
@ -276,4 +296,12 @@ func generateRandomString(length int) string {
|
|||
}
|
||||
|
||||
return output
|
||||
}
|
||||
*/
|
||||
|
||||
func errorPanic(err error) {
|
||||
if err != nil {
|
||||
logger(err.Error())
|
||||
panic("----------------ERROR----------------")
|
||||
}
|
||||
}
|
||||
|
|
8
http.go
8
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)
|
||||
|
|
15
main.go
15
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue