apikey
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 32s

This commit is contained in:
matst80
2025-05-15 19:47:49 +02:00
parent 8715c7ea34
commit fa47d75541
2 changed files with 10 additions and 11 deletions

10
main.go
View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
"git.tornberg.me/go-persist/pkg/storage" "git.tornberg.me/go-persist/pkg/storage"
@@ -132,8 +133,10 @@ func esimateMimeType(filename string) string {
return "application/octet-stream" return "application/octet-stream"
} }
var apiKey = os.Getenv("API_KEY")
func main() { func main() {
// Initialize the application
rootDir, err := storage.NewDiskStorage([]string{}) rootDir, err := storage.NewDiskStorage([]string{})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -188,6 +191,11 @@ func main() {
json.NewEncoder(w).Encode(content) json.NewEncoder(w).Encode(content)
} else { } else {
defer r.Body.Close() defer r.Body.Close()
authKey := r.Header.Get("Authorization")
if authKey != apiKey {
w.WriteHeader(http.StatusUnauthorized)
return
}
data, err := io.ReadAll(r.Body) data, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@@ -19,10 +19,7 @@ func NewDiskStorage(ids []string) (Storage, error) {
idPth := path.Join(ids...) idPth := path.Join(ids...)
dir := path.Join(pth, idPth) dir := path.Join(pth, idPth)
s := &DiskStorage{dir: dir} s := &DiskStorage{dir: dir}
err := s.ensureDir()
if err != nil {
return nil, err
}
return s, nil return s, nil
} }
@@ -32,9 +29,6 @@ func (ds *DiskStorage) ensureDir() error {
} }
func (ds *DiskStorage) Get(key string) (io.Reader, error) { func (ds *DiskStorage) Get(key string) (io.Reader, error) {
if err := ds.ensureDir(); err != nil {
return nil, err
}
f, err := os.Open(path.Join(ds.dir, key)) f, err := os.Open(path.Join(ds.dir, key))
if err != nil { if err != nil {
return nil, err return nil, err
@@ -63,9 +57,6 @@ func (ds *DiskStorage) Delete(key string) error {
} }
func (ds *DiskStorage) List(prefix string) ([]string, error) { func (ds *DiskStorage) List(prefix string) ([]string, error) {
if err := ds.ensureDir(); err != nil {
return nil, err
}
data, err := os.ReadDir(path.Join(ds.dir, prefix)) data, err := os.ReadDir(path.Join(ds.dir, prefix))
if err != nil { if err != nil {
return nil, err return nil, err