Compare commits
No commits in common. "318fa30d3c1f1f3341bb9fe4c0c56d8e43a35d1b" and "a8a19b32bdc9f6aefd745172b703932f0bdbbdfd" have entirely different histories.
318fa30d3c
...
a8a19b32bd
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +0,0 @@
|
|||||||
build
|
|
||||||
uploads
|
|
||||||
8
Makefile
8
Makefile
@ -1,8 +0,0 @@
|
|||||||
BINARY = termcloud
|
|
||||||
|
|
||||||
build: clean
|
|
||||||
go build -o build/${BINARY} cmd/${BINARY}/main.go
|
|
||||||
|
|
||||||
clean:
|
|
||||||
go clean ./...
|
|
||||||
rm -rf build
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.keircn.com/keiran/termcloud/internal/handlers"
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/labstack/echo/v4/middleware"
|
|
||||||
"golang.org/x/time/rate"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
e := echo.New()
|
|
||||||
|
|
||||||
e.Use(middleware.BodyLimit("100M"))
|
|
||||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
||||||
AllowOrigins: []string{"*"},
|
|
||||||
AllowMethods: []string{echo.GET, echo.POST},
|
|
||||||
}))
|
|
||||||
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
|
||||||
Level: 5,
|
|
||||||
}))
|
|
||||||
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(20))))
|
|
||||||
e.Static("/uploads", "uploads")
|
|
||||||
|
|
||||||
e.GET("/", handlers.RootHandler)
|
|
||||||
e.POST("/upload", handlers.UploadHandler)
|
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":8080"))
|
|
||||||
}
|
|
||||||
19
go.mod
19
go.mod
@ -1,20 +1,3 @@
|
|||||||
module git.keircn.com/keiran/termcloud
|
module git.keircn.com/keiran/termcord
|
||||||
|
|
||||||
go 1.24.5
|
go 1.24.5
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/labstack/echo/v4 v4.13.4
|
|
||||||
golang.org/x/time v0.11.0
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/labstack/gommon v0.4.2 // indirect
|
|
||||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
|
||||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
|
||||||
golang.org/x/crypto v0.38.0 // indirect
|
|
||||||
golang.org/x/net v0.40.0 // indirect
|
|
||||||
golang.org/x/sys v0.33.0 // indirect
|
|
||||||
golang.org/x/text v0.25.0 // indirect
|
|
||||||
)
|
|
||||||
|
|||||||
31
go.sum
31
go.sum
@ -1,31 +0,0 @@
|
|||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA=
|
|
||||||
github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ=
|
|
||||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
|
||||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
|
||||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
|
||||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
|
||||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
|
||||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
|
||||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
|
||||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
|
||||||
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
|
|
||||||
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
|
|
||||||
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
|
|
||||||
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
|
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
|
||||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
|
||||||
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
|
||||||
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
|
||||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
|
||||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
@ -1,76 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
type FileInfo struct {
|
|
||||||
FileName string `json:"fileName"`
|
|
||||||
FileSize int64 `json:"fileSize"`
|
|
||||||
FileType string `json:"fileType"`
|
|
||||||
FileURL string `json:"fileURL"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func RootHandler(c echo.Context) error {
|
|
||||||
return c.JSON(200, map[string]string{
|
|
||||||
"status": "😺",
|
|
||||||
"docs": "https://illfillthisoutlater.com",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadHandler e.POST("/upload")
|
|
||||||
func UploadHandler(c echo.Context) error {
|
|
||||||
file, err := c.FormFile("file")
|
|
||||||
if err != nil {
|
|
||||||
c.Logger().Errorf("Error retrieving file from request: %v", err)
|
|
||||||
return c.JSON(400, map[string]string{"error": "Failed to retrieve file"})
|
|
||||||
}
|
|
||||||
|
|
||||||
if file == nil {
|
|
||||||
c.Logger().Error("No file provided in the request")
|
|
||||||
return c.JSON(400, map[string]string{"error": "No file provided"})
|
|
||||||
}
|
|
||||||
|
|
||||||
src, err := file.Open()
|
|
||||||
if err != nil {
|
|
||||||
c.Logger().Errorf("Error opening uploaded file: %v", err)
|
|
||||||
return c.JSON(500, map[string]string{"error": "Failed to open file"})
|
|
||||||
}
|
|
||||||
defer src.Close()
|
|
||||||
|
|
||||||
fileName := file.Filename
|
|
||||||
fileSize := file.Size
|
|
||||||
fileType := file.Header.Get("Content-Type")
|
|
||||||
|
|
||||||
c.Logger().Infof("Received file: %s, Size: %d bytes, Type: %s", fileName, fileSize, fileType)
|
|
||||||
|
|
||||||
err = os.MkdirAll("uploads", 0o755)
|
|
||||||
if err != nil {
|
|
||||||
c.Logger().Errorf("Error creating uploads directory: %v", err)
|
|
||||||
return c.JSON(500, map[string]string{"error": "Failed to create upload directory"})
|
|
||||||
}
|
|
||||||
|
|
||||||
dst, err := os.Create("uploads/" + fileName)
|
|
||||||
if err != nil {
|
|
||||||
c.Logger().Errorf("Error creating file on server: %v", err)
|
|
||||||
return c.JSON(500, map[string]string{"error": "Failed to save file"})
|
|
||||||
}
|
|
||||||
defer dst.Close()
|
|
||||||
|
|
||||||
if _, err := io.Copy(dst, src); err != nil {
|
|
||||||
c.Logger().Errorf("Error saving uploaded file: %v", err)
|
|
||||||
return c.JSON(500, map[string]string{"error": "Failed to save file"})
|
|
||||||
}
|
|
||||||
|
|
||||||
fileURL := "http://localhost:8080/uploads/" + fileName
|
|
||||||
|
|
||||||
return c.JSON(200, FileInfo{
|
|
||||||
FileName: fileName,
|
|
||||||
FileSize: fileSize,
|
|
||||||
FileType: fileType,
|
|
||||||
FileURL: fileURL,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user