add 10 seconds timeout
This commit is contained in:
parent
99e2cd23f3
commit
b214921dd0
1 changed files with 21 additions and 2 deletions
19
go/main.go
19
go/main.go
|
@ -7,6 +7,14 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
lastUploadTime time.Time
|
||||||
|
mu sync.Mutex
|
||||||
|
uploadInterval = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -25,6 +33,15 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
if time.Since(lastUploadTime) < uploadInterval {
|
||||||
|
http.Error(w, "Nur alle 10 Sekunden erlaubt", http.StatusTooManyRequests)
|
||||||
|
log.Printf("Bildupload zu häufig. Nur alle 10 Sekunden erlaubt.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
file, handler, err := r.FormFile("image")
|
file, handler, err := r.FormFile("image")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,6 +68,8 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastUploadTime = time.Now() // Setzen Sie die Zeit des letzten Uploads
|
||||||
|
|
||||||
// Erfolgsmeldung mit dem Link ausgeben
|
// Erfolgsmeldung mit dem Link ausgeben
|
||||||
link := fmt.Sprintf("Bild erfolgreich hochgeladen. Sie können es hier anzeigen: <a href='/view/%s'>Anzeigen</a>", handler.Filename)
|
link := fmt.Sprintf("Bild erfolgreich hochgeladen. Sie können es hier anzeigen: <a href='/view/%s'>Anzeigen</a>", handler.Filename)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue