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

View File

@@ -19,10 +19,7 @@ func NewDiskStorage(ids []string) (Storage, error) {
idPth := path.Join(ids...)
dir := path.Join(pth, idPth)
s := &DiskStorage{dir: dir}
err := s.ensureDir()
if err != nil {
return nil, err
}
return s, nil
}
@@ -32,9 +29,6 @@ func (ds *DiskStorage) ensureDir() 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))
if err != nil {
return nil, err
@@ -63,9 +57,6 @@ func (ds *DiskStorage) Delete(key 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))
if err != nil {
return nil, err