From 02840b07de0aed90f3e931a6bb0bdb1f0b3929d8 Mon Sep 17 00:00:00 2001 From: xoy Date: Thu, 26 Jan 2023 23:13:44 +0100 Subject: [PATCH] =?UTF-8?q?anpassung=20der=20funktions-=20und=20variablenn?= =?UTF-8?q?amen,=20erstellung=20vor=C3=BCbergehender=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++++- file.go | 2 +- func.go | 58 ++++++++++++++++++++++++++++++++++--------------------- html.go | 30 +++++++--------------------- http.go | 29 +++++++--------------------- main.go | 20 ++++++++++++++++++- 6 files changed, 75 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index ec46004..5d11a85 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# Readme \ No newline at end of file +# Readme + +git clone https://git.ctdo.de/xoy/ctdo.de.git +cd ./ctdo.de/ +go run . \ No newline at end of file diff --git a/file.go b/file.go index c5bc322..32fcd94 100644 --- a/file.go +++ b/file.go @@ -5,7 +5,7 @@ import ( "log" ) -func file_read(src string) string { +func fileRead(src string) string { content, err := ioutil.ReadFile(src) if err != nil { diff --git a/func.go b/func.go index 816e6d1..3a82467 100644 --- a/func.go +++ b/func.go @@ -10,35 +10,35 @@ import ( func handler() { //Pages - http_HandleFunc("home", "./web/pages/home.html", true, "text/html") - http_HandleFunc("treff", "./web/pages/treff.html", false, "text/html") - http_HandleFunc("events", "./web/pages/events.html", false, "text/html") - http_HandleFunc("about", "./web/pages/about.html", false, "text/html") + httpHandleFunc("home", "./web/pages/home.html", true, "text/html") + httpHandleFunc("treff", "./web/pages/treff.html", false, "text/html") + httpHandleFunc("events", "./web/pages/events.html", false, "text/html") + httpHandleFunc("about", "./web/pages/about.html", false, "text/html") - http_HandleFunc("kontakt", "./web/pages/kontakt.html", false, "text/html") - http_HandleFunc("kontakt/adresse", "./web/pages/kontakt/adresse.html", false, "text/html") - http_HandleFunc("kontakt/irc", "./web/pages/kontakt/irc.html", false, "text/html") - http_HandleFunc("kontakt/mail", "./web/pages/kontakt/mail.html", false, "text/html") - http_HandleFunc("kontakt/tel", "./web/pages/kontakt/tel.html", false, "text/html") + httpHandleFunc("kontakt", "./web/pages/kontakt.html", false, "text/html") + httpHandleFunc("kontakt/adresse", "./web/pages/kontakt/adresse.html", false, "text/html") + httpHandleFunc("kontakt/irc", "./web/pages/kontakt/irc.html", false, "text/html") + httpHandleFunc("kontakt/mail", "./web/pages/kontakt/mail.html", false, "text/html") + httpHandleFunc("kontakt/tel", "./web/pages/kontakt/tel.html", false, "text/html") - http_HandleFunc("verein", "./web/pages/verein.html", false, "text/html") - http_HandleFunc("support", "./web/pages/support.html", false, "text/html") + httpHandleFunc("verein", "./web/pages/verein.html", false, "text/html") + httpHandleFunc("support", "./web/pages/support.html", false, "text/html") - http_HandleFunc("impressum", "./web/pages/impressum.html", false, "text/html") - http_HandleFunc("datenschutz", "./web/pages/datenschutz.html", false, "text/html") + httpHandleFunc("impressum", "./web/pages/impressum.html", false, "text/html") + httpHandleFunc("datenschutz", "./web/pages/datenschutz.html", false, "text/html") //Styles - http_HandleFunc("style/main.css", "./web/styles/main.css", false, "text/css") - http_HandleFunc("style/kontakt.css", "./web/styles/kontakt.css", false, "text/css") - http_HandleFunc("style/home.css", "./web/styles/home.css", false, "text/css") + httpHandleFunc("style/main.css", "./web/styles/main.css", false, "text/css") + httpHandleFunc("style/kontakt.css", "./web/styles/kontakt.css", false, "text/css") + httpHandleFunc("style/home.css", "./web/styles/home.css", false, "text/css") //Images - http_HandleFunc("image/logo_ctdo.svg", "./web/images/logo_ctdo.svg", false, "image/svg+xml") - http_HandleFunc("image/header.jpg", "./web/images/header.jpg", false, "image/jpeg") - http_HandleFunc("image/adresse_knopf.webp", "./web/images/adresse_knopf.webp", false, "image/webp") - http_HandleFunc("image/chat_knopf.webp", "./web/images/chat_knopf.webp", false, "image/webp") - http_HandleFunc("image/mail_knopf.webp", "./web/images/mail_knopf.webp", false, "image/webp") - http_HandleFunc("image/tel_knopf.webp", "./web/images/tel_knopf.webp", false, "image/webp") + httpHandleFunc("image/logo_ctdo.svg", "./web/images/logo_ctdo.svg", false, "image/svg+xml") + httpHandleFunc("image/header.jpg", "./web/images/header.jpg", false, "image/jpeg") + httpHandleFunc("image/adresse_knopf.webp", "./web/images/adresse_knopf.webp", false, "image/webp") + httpHandleFunc("image/chat_knopf.webp", "./web/images/chat_knopf.webp", false, "image/webp") + httpHandleFunc("image/mail_knopf.webp", "./web/images/mail_knopf.webp", false, "image/webp") + httpHandleFunc("image/tel_knopf.webp", "./web/images/tel_knopf.webp", false, "image/webp") } func getPages() [][]string { @@ -102,3 +102,17 @@ func getRoomState() status { return *roomState } + +func htmlReplacer(input string) string { + output := strings.ReplaceAll(input, "!NAV", htmlNav(getPages())) + + if getRoomState().state { + output = strings.ReplaceAll(output, "!RAUMSTATUS", "

Raumstatus: offen

") + } else { + output = strings.ReplaceAll(output, "!RAUMSTATUS", "

Raumstatus: geschlossen

") + } + + output = strings.ReplaceAll(output, "!FOOTERNAV", htmlNav(getFooterPages())) + + return output +} diff --git a/html.go b/html.go index f57bc4d..2abe61f 100644 --- a/html.go +++ b/html.go @@ -1,44 +1,28 @@ package main -import "strings" - -func html_Element(tag string, innerHTML string, args string) string { +func htmlElement(tag string, innerHTML string, args string) string { return "<" + tag + " " + args + ">" + innerHTML + "" } -func html_LinkElement(innerHTML string, destination string, blank bool, args string) string { +func htmlLinkElement(innerHTML string, destination string, blank bool, args string) string { b := "" if blank { b = "target=\"_blank\"" } - return html_Element("a", innerHTML, " href=\""+destination+"\" "+b) + return htmlElement("a", innerHTML, " href=\""+destination+"\" "+b) } -func html_InputElement(name string, _type string, value string, args string) string { +func htmlInputElement(name string, _type string, value string, args string) string { return "" } -func html_Nav(pages [][]string) string { +func htmlNav(pages [][]string) string { output := "" for _, page := range pages { - output += html_Element("li", html_LinkElement(page[0], page[1], false, ""), "") + output += htmlElement("li", htmlLinkElement(page[0], page[1], false, ""), "") } - return html_Element("nav", html_Element("ul", output, ""), "") -} - -func html_replacer(input string) string { - output := strings.ReplaceAll(input, "!NAV", html_Nav(getPages())) - - if getRoomState().state { - output = strings.ReplaceAll(output, "!RAUMSTATUS", "

Raumstatus: offen

") - } else { - output = strings.ReplaceAll(output, "!RAUMSTATUS", "

Raumstatus: geschlossen

") - } - - output = strings.ReplaceAll(output, "!FOOTERNAV", html_Nav(getFooterPages())) - - return output + return htmlElement("nav", htmlElement("ul", output, ""), "") } diff --git a/http.go b/http.go index 74c8cdf..b87456e 100644 --- a/http.go +++ b/http.go @@ -1,23 +1,21 @@ package main import ( - "errors" "fmt" "io" "net/http" - "os" ) -func http_HandleFunc(url_path string, filepath string, is_mainpage bool, content_type string) { - if is_mainpage { +func httpHandleFunc(urlPath string, filepath string, isMainpage bool, contentType string) { + if isMainpage { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", content_type) - io.WriteString(w, html_replacer(file_read(filepath))) + w.Header().Add("Content-Type", contentType) + io.WriteString(w, htmlReplacer(fileRead(filepath))) }) } else { s := new(submit) s.data = "null" - http.HandleFunc("/"+url_path, func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { if err := r.ParseForm(); err != nil { fmt.Fprintf(w, "ParseForm() err: %v", err) @@ -25,22 +23,9 @@ func http_HandleFunc(url_path string, filepath string, is_mainpage bool, content } } - w.Header().Add("Content-Type", content_type) + w.Header().Add("Content-Type", contentType) - io.WriteString(w, html_replacer(file_read(filepath))) + io.WriteString(w, htmlReplacer(fileRead(filepath))) }) } } - -func http_start(address string) { // address could be = ":8080" - handler() - - err := http.ListenAndServe(address, 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) - } -} diff --git a/main.go b/main.go index 0faaad6..0ab12e4 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,23 @@ package main +import ( + "errors" + "fmt" + "net/http" + "os" +) + func main() { - http_start(":80") + addr := ":80" + + 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) + } }