ctdo.de/main.go

169 lines
5.3 KiB
Go
Raw Normal View History

package main
import (
"io/ioutil"
"net/http"
"strings"
)
2023-01-28 21:22:13 +00:00
/*
Timezone = UTC
*/
func main() {
addr := readHttpYML()
2023-01-28 21:22:13 +00:00
logger("----------------START----------------")
logger("timezone = UTC")
logger("connection open with address " + addr)
handler()
err := http.ListenAndServe(addr, nil)
errorPanic(err)
2023-01-28 21:22:13 +00:00
defer logger("----------------END----------------")
}
func handler() {
logger("Pages:")
//pages
httpHandleFunc("", "./web/pages/home.html", "text/html")
handleFilesInFolder("", "./web/pages/", true)
/*
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()
if len(keys) > 0 {
for _, key := range keys {
httpHandleFunc("admin/"+key, "./web/pages/admin/dashboard.html", "text/html")
httpHandleFuncWithPOST("admin/"+key+"/addEvent", "./web/pages/admin/dashboard.html", "text/html")
}
}
//styles
handleFilesInFolder("style/", "./web/styles/", false)
/*
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")
httpHandleFunc("style/dashboard.css", "./web/styles/dashboard.css", "text/css")
*/
//images
handleFilesInFolder("image/", "./web/images/", false)
/*
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")
httpHandleFunc("image/chat_knopf.webp", "./web/images/chat_knopf.webp", "image/webp")
httpHandleFunc("image/mail_knopf.webp", "./web/images/mail_knopf.webp", "image/webp")
httpHandleFunc("image/tel_knopf.webp", "./web/images/tel_knopf.webp", "image/webp")
*/
}
var alreadyHandledFiles []string = []string{}
func handleFilesInFolder(urlPrefix string, folderpath string, handleWithoutFileSuffix bool) {
if folderpath[len(folderpath)-1] != '/' {
folderpath += "/"
}
files, err := ioutil.ReadDir(folderpath)
errorPanic(err)
for _, file := range files {
alreadyHandled := stringListContains(alreadyHandledFiles, folderpath+file.Name())
if !file.IsDir() {
if !alreadyHandled {
f := strings.Split(file.Name(), ".")
ContentType := f[len(f)-1]
filename := ""
if handleWithoutFileSuffix {
for index, item := range f {
if index < len(f)-1 {
filename += item
}
}
} else {
filename = file.Name()
}
switch ContentType {
case "png":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "image/png")
case "webp":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "image/webp")
case "svg":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "image/svg+xml")
case "jpeg":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "image/jpeg")
case "jpg":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "image/jpeg")
case "gif":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "image/gif")
case "css":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "text/css")
case "html":
httpHandleFunc(urlPrefix+filename, folderpath+file.Name(), "text/html")
default:
logger("handleFilesInFolder -> content-type unknown : " + folderpath + file.Name())
}
alreadyHandledFiles = append(alreadyHandledFiles, folderpath+file.Name())
} else {
logger("handleFilesInFolder -> already handled : " + folderpath + file.Name())
}
} else {
childFolder := file.Name()
handleFilesInFolder(childFolder, folderpath+childFolder, handleWithoutFileSuffix)
}
}
}
func getPages() [][]string {
output := [][]string{}
output = append(output, []string{"home", "/home"})
output = append(output, []string{"zeiten & location", "/treff"})
output = append(output, []string{"events", "/events"})
output = append(output, []string{"über uns", "/about"})
output = append(output, []string{"kontakt", "/kontakt"})
output = append(output, []string{"verein", "/verein"})
output = append(output, []string{"unterstützung", "/support"})
return output
}
func getFooterPages() [][]string {
output := [][]string{}
output = append(output, []string{"impressum", "/impressum"})
output = append(output, []string{"datenschutzerklärung", "/datenschutz"})
return output
}