Update main.go
This commit is contained in:
parent
49b84ee9fb
commit
4357542f09
1 changed files with 23 additions and 90 deletions
|
@ -1,99 +1,32 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"log"
|
||||||
"fmt"
|
"net/http"
|
||||||
"io"
|
"os"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/client"
|
"web3-onlyfans/services/frostfs-uploader/internal/utils"
|
||||||
"github.com/nspcc-dev/neofs-sdk-go/client/object"
|
"web3-onlyfans/services/frostfs-uploader/internal/handlers"
|
||||||
"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")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Инициализация
|
cfgPath := os.Getenv("FROSTFS_UPLOADER_CONFIG")
|
||||||
r := mux.NewRouter()
|
if cfgPath == "" {
|
||||||
r.HandleFunc("/upload", handleUpload).Methods("POST")
|
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-сервер
|
r := mux.NewRouter()
|
||||||
addr := ":8081"
|
r.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("Starting FrostFS uploader on", addr)
|
handlers.UploadHandler(w, r, cfg, logger)
|
||||||
log.Fatal(http.ListenAndServe(addr, r))
|
}).Methods("POST")
|
||||||
}
|
|
||||||
|
addr := cfg.ListenAddr
|
||||||
func handleUpload(w http.ResponseWriter, r *http.Request) {
|
logger.Infof("frostfs-uploader on %s", addr)
|
||||||
// Пример: берем файл из multipart/form-data
|
log.Fatal(http.ListenAndServe(addr, r))
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue