package main import ( "io/ioutil" "net/http" "strconv" "strings" ) /* Timezone = UTC */ func main() { addr := readHttpYML() logger("----------------START----------------") logger("timezone = UTC") logger("connection open with address " + addr) handler() err := http.ListenAndServe(addr, nil) errorPanic(err) } func handler() { //reihenfolge ist wichtig! //admin pages logger("----------------HANDLE ADMIN LOGIN----------------") 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") } } logger("----------------HANDLE END----------------") //events handleEvents() //pages logger("----------------HANDLE PAGES----------------") httpHandleFunc("", "./web/pages/home.html", "text/html") handleFilesInFolder("", "./web/pages/", true) logger("----------------HANDLE END----------------") //styles logger("----------------HANDLE STYLES----------------") handleFilesInFolder("style/", "./web/styles/", false) logger("----------------HANDLE END----------------") //images handleImages() } func handleEvents() { if getEventCount() > 0 { logger("----------------HANDLE EVENTS----------------") events := getEvents() for _, e := range events { httpHandleFunc("event/"+strconv.Itoa(e.id), "./web/pages/event.html", "text/html") } logger("----------------HANDLE END----------------") } } func handleImages() { logger("----------------HANDLE IMAGES----------------") handleFilesInFolder("image/", "./web/images/", false) logger("----------------HANDLE END----------------") } var alreadyHandledFiles []string = []string{"./web/pages/admin/dashboard.html", "./web/pages/event.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()) 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 { childFolder := file.Name() handleFilesInFolder(childFolder, folderpath+childFolder, handleWithoutFileSuffix) } } else { 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 }