diff --git a/services/frostfs-uploader/main.go b/services/frostfs-uploader/main.go index 0f8695c..8e0252e 100644 --- a/services/frostfs-uploader/main.go +++ b/services/frostfs-uploader/main.go @@ -1,99 +1,32 @@ package main import ( - "context" - "fmt" - "io" - "log" - "net/http" - "os" + "log" + "net/http" + "os" - "github.com/gorilla/mux" - "github.com/nspcc-dev/neofs-sdk-go/client" - "github.com/nspcc-dev/neofs-sdk-go/client/object" - "github.com/nspcc-dev/neofs-sdk-go/container" - // ... -) - -var ( - frostfsEndpoint = os.Getenv("FROSTFS_ENDPOINT") // типа "grpcs://localhost:8080" - frostfsPrivKey = os.Getenv("FROSTFS_PRIVKEY") - frostfsContainer = os.Getenv("FROSTFS_CONTAINER_ID") + "github.com/gorilla/mux" + "web3-onlyfans/services/frostfs-uploader/internal/utils" + "web3-onlyfans/services/frostfs-uploader/internal/handlers" ) func main() { - // Инициализация - r := mux.NewRouter() - r.HandleFunc("/upload", handleUpload).Methods("POST") + cfgPath := os.Getenv("FROSTFS_UPLOADER_CONFIG") + if cfgPath == "" { + cfgPath = "./config.yaml" + } + cfg, err := utils.LoadConfig(cfgPath) + if err != nil { + log.Fatalf("failed to load config: %v", err) + } + logger := utils.NewLogger(cfg.LogLevel) - // HTTP-сервер - addr := ":8081" - log.Println("Starting FrostFS uploader on", addr) - log.Fatal(http.ListenAndServe(addr, r)) -} - -func handleUpload(w http.ResponseWriter, r *http.Request) { - // Пример: берем файл из multipart/form-data - file, header, err := r.FormFile("file") - if err != nil { - http.Error(w, "file error: "+err.Error(), http.StatusBadRequest) - return - } - defer file.Close() - - // Читаем в []byte (для простоты, но лучше стримить) - data, err := io.ReadAll(file) - if err != nil { - http.Error(w, "read error: "+err.Error(), http.StatusInternalServerError) - return - } - - // Подключаемся к FrostFS - cli, err := client.New(client.WithDefaultPrivateKeyStr(frostfsPrivKey)) - if err != nil { - http.Error(w, "client init error: "+err.Error(), http.StatusInternalServerError) - return - } - defer cli.Close() - - err = cli.Dial(frostfsEndpoint) - if err != nil { - http.Error(w, "dial error: "+err.Error(), http.StatusInternalServerError) - return - } - - cntr, err := container.IDFromString(frostfsContainer) - if err != nil { - http.Error(w, "invalid container: "+err.Error(), http.StatusInternalServerError) - return - } - - oid, err := uploadFileToFrostFS(r.Context(), cli, cntr, data) - if err != nil { - http.Error(w, "frostfs upload error: "+err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, `{"status":"ok","object_id":"%s","filename":"%s"}`, oid, header.Filename) -} - -func uploadFileToFrostFS(ctx context.Context, cli *client.Client, cntr container.ID, data []byte) (string, error) { - obj := object.New() - obj.SetPayload(data) - - writer, err := cli.ObjectPutInit(ctx, cntr, obj) - if err != nil { - return "", err - } - err = writer.Write(data) - if err != nil { - return "", err - } - - oid, err := writer.Close() - if err != nil { - return "", err - } - return oid.String(), nil + r := mux.NewRouter() + r.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { + handlers.UploadHandler(w, r, cfg, logger) + }).Methods("POST") + + addr := cfg.ListenAddr + logger.Infof("frostfs-uploader on %s", addr) + log.Fatal(http.ListenAndServe(addr, r)) }