feat(): add basic authentication
This commit is contained in:
parent
48528f3a56
commit
b8ad1dd9b9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
files/
|
files/
|
||||||
|
.key
|
||||||
|
@ -7,4 +7,5 @@ services:
|
|||||||
- "58080:8080"
|
- "58080:8080"
|
||||||
volumes:
|
volumes:
|
||||||
- ./files:/app/files
|
- ./files:/app/files
|
||||||
|
- ./.key:/app/.key
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
10
main.go
10
main.go
@ -42,6 +42,11 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !checkAuth(w, r) {
|
||||||
|
http.Error(w, "You're not authorized.", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, maxFileSize)
|
r.Body = http.MaxBytesReader(w, r.Body, maxFileSize)
|
||||||
|
|
||||||
file, _, err := r.FormFile("file")
|
file, _, err := r.FormFile("file")
|
||||||
@ -77,3 +82,8 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, "http://%s/%d\n", url, time)
|
fmt.Fprintf(w, "http://%s/%d\n", url, time)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
authKey, _ := os.ReadFile(".key")
|
||||||
|
return r.Header.Get("X-Auth")+"\n" == string(authKey)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user