2024-04-30 10:53:18 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-04-30 12:11:26 +02:00
|
|
|
// func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// fmt.Printf("Received connection\n")
|
|
|
|
// file, err := os.Open("./index.html")
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("Error happened opening index.html: %v\n", err)
|
|
|
|
// }
|
|
|
|
// defer file.Close()
|
|
|
|
// data, err := io.ReadAll(file)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("Error happened reading data: %v\n", err)
|
|
|
|
// }
|
|
|
|
// fmt.Fprint(w, string(data))
|
|
|
|
// }
|
2024-04-30 10:53:18 +02:00
|
|
|
|
|
|
|
func uploadFile(w http.ResponseWriter, r *http.Request) {
|
2024-04-30 11:25:36 +02:00
|
|
|
fmt.Fprintln(w, "You have uploaded something")
|
|
|
|
|
|
|
|
file, header, err := r.FormFile("file")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error happened receiving file: %v\n", err)
|
|
|
|
}
|
2024-04-30 12:11:26 +02:00
|
|
|
|
2024-04-30 12:19:23 +02:00
|
|
|
_, err = os.Stat("./temp/")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.Mkdir("./temp/", 0700)
|
|
|
|
}
|
|
|
|
|
2024-04-30 11:25:36 +02:00
|
|
|
f, err := os.OpenFile("./temp/"+header.Filename, os.O_WRONLY|os.O_CREATE, 0660)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error happened opening file: %v\n", err)
|
|
|
|
}
|
2024-04-30 12:11:26 +02:00
|
|
|
log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent())
|
2024-04-30 11:25:36 +02:00
|
|
|
io.Copy(f, file)
|
|
|
|
|
|
|
|
defer file.Close()
|
2024-04-30 10:53:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-04-30 12:11:26 +02:00
|
|
|
http.Handle("/", http.FileServer(http.Dir("./assets")))
|
2024-04-30 10:53:18 +02:00
|
|
|
http.HandleFunc("/api/upload", uploadFile)
|
|
|
|
|
2024-04-30 11:25:36 +02:00
|
|
|
fmt.Println("Listening on port 8080...")
|
|
|
|
|
2024-04-30 10:53:18 +02:00
|
|
|
err := http.ListenAndServe(":8080", nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|