ctdo.de/http.go

111 lines
2.9 KiB
Go
Raw Normal View History

package main
import (
"io"
"net/http"
2023-01-28 23:00:16 +00:00
"os"
2023-01-28 22:18:28 +00:00
"strings"
"time"
)
2023-01-26 22:47:23 +00:00
func httpHandleFunc(urlPath string, filepath string, contentType string) {
2023-01-28 22:18:28 +00:00
urlPath = strings.ToLower(urlPath)
2023-01-28 21:32:20 +00:00
logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">")
2023-01-26 22:47:23 +00:00
http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) {
2023-01-28 21:32:20 +00:00
logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">")
2023-01-28 21:28:31 +00:00
2023-01-26 22:47:23 +00:00
w.Header().Add("Content-Type", contentType)
2023-01-27 14:30:42 +00:00
io.WriteString(w, htmlReplacer(fileRead(filepath), urlPath))
2023-01-26 22:47:23 +00:00
})
}
func httpHandleFuncWithPOST(urlPath string, filepath string, contentType string) {
2023-01-29 14:39:38 +00:00
Event := new(event)
2023-01-28 21:32:20 +00:00
logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">")
2023-01-26 22:47:23 +00:00
http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
2023-01-29 14:39:38 +00:00
logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">")
2023-01-28 22:43:07 +00:00
r.ParseMultipartForm(10 << 20)
2023-01-28 23:00:16 +00:00
err := r.ParseMultipartForm(200000)
errorPanic(err)
2023-01-28 22:09:31 +00:00
2023-01-28 23:00:16 +00:00
formdata := r.MultipartForm
2023-01-29 11:45:55 +00:00
files := formdata.File["media"]
2023-01-28 23:00:16 +00:00
if filepath == "./web/pages/admin/dashboard.html" {
2023-01-29 14:39:38 +00:00
logger("----------------ADD EVENT----------------")
2023-01-28 23:00:16 +00:00
title := formdata.Value["title"]
description := formdata.Value["description"]
media := formdata.File["media"]
2023-01-29 14:39:38 +00:00
mediaString := ""
2023-01-28 23:00:16 +00:00
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)))
2023-01-29 11:45:55 +00:00
2023-01-29 14:39:38 +00:00
logger("files uploaded successfully: ")
2023-01-29 11:45:55 +00:00
for i, _ := range files {
2023-01-29 14:39:38 +00:00
if len(media) > 0 {
mediaString += ","
}
mediaString += "./web/images/" + files[i].Filename
2023-01-29 11:45:55 +00:00
2023-01-29 14:39:38 +00:00
logger("./web/images/" + files[i].Filename)
}
2023-01-29 11:45:55 +00:00
2023-01-29 14:39:38 +00:00
Event.id = -1
Event.title = title[0]
Event.description = description[0]
Event.media = mediaString
Event.date = date[0]
2023-01-29 11:45:55 +00:00
2023-01-29 14:39:38 +00:00
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!")
2023-01-29 14:48:27 +00:00
handleImages()
2023-01-29 14:39:38 +00:00
} else {
logger("event not added!")
2023-01-29 11:45:55 +00:00
}
2023-01-29 14:39:38 +00:00
} else {
logger("no formdata")
2023-01-28 22:09:31 +00:00
}
2023-01-29 14:39:38 +00:00
logger("----------------ADD END----------------")
2023-01-28 22:09:31 +00:00
}
2023-01-26 22:47:23 +00:00
}
2023-01-26 22:47:23 +00:00
w.Header().Add("Content-Type", contentType)
2023-01-27 14:30:42 +00:00
io.WriteString(w, htmlReplacer(fileRead(filepath), urlPath))
2023-01-26 22:47:23 +00:00
})
}
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
}