package cmd import ( "embed" "fmt" "io" "io/fs" "log" "net/http" "os" ) //go:embed assets var assetsFolder embed.FS func uploadFile(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Successful upload!") file, header, err := r.FormFile("file") if err != nil { log.Printf("Error happened receiving file: %v\n", err) } defer file.Close() _, err = os.Stat("./temp/") if os.IsNotExist(err) { os.Mkdir("./temp/", 0700) } 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) } log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent()) io.Copy(f, file) } func server() { serverRoot, err := fs.Sub(assetsFolder, "assets") if err != nil { log.Fatalf("Error occurred setting HTML root: %v\n", err) } http.Handle("/", http.FileServer(http.FS(serverRoot))) http.HandleFunc("/api/upload", uploadFile) fmt.Println("Listening on port 8080...") err = http.ListenAndServe(":8080", nil) if err != nil { log.Fatal(err) } }