package main import ( "io" "net/http" "os" "strings" "time" ) func httpHandleFunc(urlPath string, filepath string, contentType string) { urlPath = strings.ToLower(urlPath) logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">") http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) { logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">") w.Header().Add("Content-Type", contentType) io.WriteString(w, htmlReplacer(fileRead(filepath), urlPath)) }) } func httpHandleFuncWithPOST(urlPath string, filepath string, contentType string) { Event := new(event) logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">") http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">") r.ParseMultipartForm(10 << 20) err := r.ParseMultipartForm(200000) errorPanic(err) formdata := r.MultipartForm files := formdata.File["media"] if filepath == "./web/pages/admin/dashboard.html" { logger("----------------ADD EVENT----------------") title := formdata.Value["title"] description := formdata.Value["description"] media := formdata.File["media"] mediaString := "" date := formdata.Value["date"] if title[0] != "" && description[0] != "" && media != nil && date[0] != "" { logger("title: " + title[0]) logger("descrtiption: " + description[0]) logger("media: " + string(len(media))) logger("files uploaded successfully: ") for i, _ := range files { if len(media) > 0 { mediaString += "," } mediaString += "./web/images/" + files[i].Filename logger("./web/images/" + files[i].Filename) } Event.id = -1 Event.title = title[0] Event.description = description[0] Event.media = mediaString Event.date = date[0] logger("date: " + date[0]) if addEvent(*Event) { for i, _ := range files { file, err := files[i].Open() errorPanic(err) out, err := os.Create("./web/images/" + files[i].Filename) errorPanic(err, "unable to create the file -> '"+"./web/images/"+files[i].Filename+"' : check your write access privilege") _, err = io.Copy(out, file) errorPanic(err) } logger("event added!") } else { logger("event not added!") } } else { logger("no formdata") } logger("----------------ADD END----------------") } } w.Header().Add("Content-Type", contentType) io.WriteString(w, htmlReplacer(fileRead(filepath), urlPath)) }) } func httpGetBody(url string) []byte { c := &http.Client{Timeout: 10 * time.Second} r, err := c.Get(url) errorPanic(err) defer r.Body.Close() var body []byte body, err = io.ReadAll(r.Body) errorPanic(err) return body }