Files
astra-file-storage/main.go
2023-08-24 22:43:18 +03:00

68 lines
1.1 KiB
Go

package main
import (
"astra-file-storage/config"
uuid2 "github.com/google/uuid"
"github.com/labstack/echo/v4"
"io"
"net/http"
"os"
"github.com/labstack/echo/v4/middleware"
)
type FileSuccess struct {
FileName string `json:"file_name"`
}
func health(c echo.Context) error {
type Resp struct {
Status string `json:"status"`
}
return c.JSON(http.StatusOK, Resp{Status: "OK"})
}
func upload(c echo.Context) error {
file, err := c.FormFile("file")
if err != nil {
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
uuid := uuid2.New().String()
fileName := uuid + "-" + file.Filename
dst, err := os.Create(config.Env.Server.SharePath + "/" + fileName)
if err != nil {
return err
}
defer dst.Close()
// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
return c.JSON(http.StatusOK, FileSuccess{
FileName: fileName,
})
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/", config.Env.Server.SharePath)
e.POST("/upload", upload)
e.GET("/health", health)
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
}