add 10 seconds timeout
This commit is contained in:
parent
99e2cd23f3
commit
b214921dd0
1 changed files with 21 additions and 2 deletions
23
go/main.go
23
go/main.go
|
@ -4,9 +4,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "html/template"
|
_ "html/template"
|
||||||
"io"
|
"io"
|
||||||
"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,7 +68,9 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erfolgsmeldung mit dem Link ausgeben
|
lastUploadTime = time.Now() // Setzen Sie die Zeit des letzten Uploads
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
// Schreiben Sie den Link als HTML in die Antwort
|
// Schreiben Sie den Link als HTML in die Antwort
|
||||||
|
|
Loading…
Reference in a new issue