ctdo.de/http.go

75 lines
2.2 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"
)
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-28 21:32:20 +00:00
logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">")
2023-01-26 22:47:23 +00:00
s := new(submit)
s.data = "null"
http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
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" {
title := formdata.Value["title"]
description := formdata.Value["description"]
media := formdata.File["media"]
date := formdata.Value["date"]
if title[0] != "" && description[0] != "" && media != nil && date[0] != "" {
2023-01-28 22:09:31 +00:00
logger("----------------POST----------------")
2023-01-28 23:00:16 +00:00
logger("title: " + title[0])
logger("descrtiption: " + description[0])
logger("media: " + string(len(media)))
2023-01-29 11:45:55 +00:00
logger("files uploaded successfully : ")
for i, _ := range files {
file, err := files[i].Open()
errorPanic(err)
out, err := os.Create("./web/images/" + files[i].Filename)
2023-01-29 11:55:21 +00:00
errorPanic(err, "unable to create the file '"+"./web/images/"+files[i].Filename+"' for writing. Check your write access privilege")
2023-01-29 11:45:55 +00:00
_, err = io.Copy(out, file)
errorPanic(err)
2023-01-29 11:55:21 +00:00
logger("./web/images/" + files[i].Filename)
2023-01-29 11:45:55 +00:00
}
2023-01-28 22:09:31 +00:00
}
2023-01-29 11:45:55 +00:00
logger("date: " + date[0])
logger("----------------POST END----------------")
2023-01-28 22:09:31 +00:00
}
2023-01-26 22:47:23 +00:00
}
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
})
}