ctdo.de/main.go

142 lines
3.9 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)
}
func handler() {
//pages
2023-01-29 14:48:27 +00:00
logger("----------------HANDLE Pages----------------")
httpHandleFunc("", "./web/pages/home.html", "text/html")
handleFilesInFolder("", "./web/pages/", true)
2023-01-29 14:48:27 +00:00
logger("----------------HANDLE END----------------")
2023-01-29 14:48:27 +00:00
logger("----------------HANDLE ADMIN LOGIN----------------")
//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")
}
}
2023-01-29 14:48:27 +00:00
logger("----------------HANDLE END----------------")
//styles
2023-01-29 14:48:27 +00:00
logger("----------------HANDLE STYLES----------------")
handleFilesInFolder("style/", "./web/styles/", false)
2023-01-29 14:48:27 +00:00
logger("----------------HANDLE END----------------")
//images
2023-01-29 14:48:27 +00:00
handleImages()
}
func handleImages() {
logger("----------------HANDLE IMAGES----------------")
handleFilesInFolder("image/", "./web/images/", false)
2023-01-29 14:39:38 +00:00
logger("----------------HANDLE END----------------")
}
2023-01-29 13:02:46 +00:00
var alreadyHandledFiles []string = []string{"./web/pages/admin/dashboard.html"}
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())
2023-01-29 13:02:46 +00:00
if !alreadyHandled {
if !file.IsDir() {
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 {
2023-01-29 13:02:46 +00:00
childFolder := file.Name()
handleFilesInFolder(childFolder, folderpath+childFolder, handleWithoutFileSuffix)
}
} else {
2023-01-29 13:02:46 +00:00
logger("handleFilesInFolder -> already handled : " + folderpath + file.Name())
}
}
}
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
}