ctdo.de/http.go

75 lines
2.1 KiB
Go

package main
import (
"io"
"net/http"
"os"
"strings"
)
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) {
logger(readHttpYML() + "/" + urlPath + " <--> " + filepath + " <" + contentType + ">")
s := new(submit)
s.data = "null"
http.HandleFunc("/"+urlPath, func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseMultipartForm(10 << 20)
err := r.ParseMultipartForm(200000)
errorPanic(err)
formdata := r.MultipartForm
files := formdata.File["multiplefiles"] // grab the filenames
logger("files uploaded successfully : ")
for i, _ := range files { // loop through the files one by one
file, err := files[i].Open()
errorPanic(err)
out, err := os.Create("./web/images/" + files[i].Filename)
errorPanic(err)
_, err = io.Copy(out, file)
errorPanic(err)
logger(files[i].Filename)
}
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] != "" {
logger("----------------POST----------------")
logger("title: " + title[0])
logger("descrtiption: " + description[0])
logger("media: " + string(len(media)))
logger("date: " + date[0])
logger("----------------POST END----------------")
}
}
}
logger(r.Method + " request -> " + readHttpYML() + "/" + urlPath + " <" + contentType + ">")
w.Header().Add("Content-Type", contentType)
io.WriteString(w, htmlReplacer(fileRead(filepath), urlPath))
})
}