commit 12ef5f122e739865896e08307a0c79dbba345340 Author: rodley82 Date: Sun Sep 11 00:20:34 2022 -0300 Initial commit basic version taking only files sent on file file param diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b758540 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +catchy-cat +extra diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3b6750c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +FROM golang:1.18.5 + +# Defining a non privileged user to run the app +# which has the benefit that one can define the UID/GID of this new user +# so volume mapping of a local directory to the container with the same +# ids works with no ownership/permission issues +ENV PROJECT_NAME=catchy-cat +ENV PROJECT_USER=${PROJECT_NAME} +ENV PROJECT_USER_UID=1000 +ENV PROJECT_USER_GID=1000 +ENV PROJECT_USER_HOME=/home/${PROJECT_USER} +ENV PROJECT_USER_APP_HOME=/go/src/github.com/rodley82/${PROJECT_NAME} + +RUN groupadd -r --gid ${PROJECT_USER_GID} ${PROJECT_USER} +RUN useradd --system --no-log-init --uid ${PROJECT_USER_UID} --gid ${PROJECT_USER_GID} --home-dir ${PROJECT_USER_HOME} --create-home --shell /bin/bash ${PROJECT_USER} + +RUN mkdir -p ${PROJECT_USER_APP_HOME} && chown -R ${PROJECT_USER}:${PROJECT_USER} ${PROJECT_USER_APP_HOME} + +# TODO Just for dev +# RUN apt update && apt install -y tmux + +USER ${PROJECT_USER} + +ENV GO111MODULE on + +# TODO Just for dev +# Only for development so we can later use reflex -r '\.go$' make all +# RUN go install github.com/cespare/reflex@latest && \ +# go install github.com/go-delve/delve/cmd/dlv@latest && \ +# go install github.com/tc-hib/go-winres@latest + +WORKDIR ${PROJECT_USER_APP_HOME} +COPY --chown=${PROJECT_USER}:${PROJECT_USER} go.mod ./ +COPY --chown=${PROJECT_USER}:${PROJECT_USER} go.sum ./ + +RUN go mod download + +COPY --chown=${PROJECT_USER}:${PROJECT_USER} *.go ./ + +RUN go build -o ${PROJECT_USER_APP_HOME}/${PROJECT_NAME} + +# Now tell Docker what command to run when the container starts +CMD ["sh", "-c", "${PROJECT_USER_APP_HOME}/${PROJECT_NAME}"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..2fd87aa --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +![catchy-cat-logo](public/catchy-cat.png) + +# catchy-cat + +A web process ready to catch any file that is thrown at it. + +Based [on this gist](https://gist.github.com/UniIsland/3346170) + + +# Testing + +``` +curl -v -F file=@./public/catchy-cat.png http://localhost:3000/ +``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f9b5c52 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + catchy-cat: + image: catchy-cat + build: . + # command: ./catchy-cat + environment: + - PORT=3000 + - DESTINATION_PATH="./" + ports: + - "3000:3000" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..89286bf --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module github.com/rodley82/catchy-cat + +go 1.18 + +require ( + github.com/andybalholm/brotli v1.0.4 // indirect + github.com/gofiber/fiber/v2 v2.37.1 // indirect + github.com/klauspost/compress v1.15.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.40.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c872193 --- /dev/null +++ b/go.sum @@ -0,0 +1,26 @@ +github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= +github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/gofiber/fiber/v2 v2.37.1 h1:QK2032gjv0ulegpv/qlTEBoXQD3eFFzCHXcNN12UZCs= +github.com/gofiber/fiber/v2 v2.37.1/go.mod h1:j3UslgQeJQP3mNhBxHnLLE8TPqA1Fd/lrl4gD25rRUY= +github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U= +github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +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/fasthttp v1.40.0 h1:CRq/00MfruPGFLTQKY8b+8SfdK60TxNztjRMnH0t1Yc= +github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..311ef1a --- /dev/null +++ b/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "log" + "github.com/gofiber/fiber/v2" +) + +var DestinationPath string + +func main() { + log.Println("Starting Catchy Cat") + DestinationPath = os.Getenv("DESTINATION_PATH") + if DestinationPath == "" { + DestinationPath = "./" + } + log.Println("DestinationPath:", DestinationPath) + + log.Println("Starting Fiber...") + app := fiber.New() + + app.Get("/", func(c *fiber.Ctx) error { + return c.SendString("Catchy-Cat, Listening!") + }) + + app.Post("/", HandlePostRequest) + port := os.Getenv("PORT") + + app.Listen(fmt.Sprintf(":%s", port)) +} + +func HandlePostRequest(c *fiber.Ctx) error { + file, err := c.FormFile("file") + if err != nil { + return c.Status(fiber.StatusBadRequest).SendString("File Not found in file arg") + } + buffer, err := file.Open() + form, err:=c.MultipartForm() + log.Printf("%d files present in the multipart form", len(form.File)) + if err != nil { + return c.Status(fiber.StatusBadRequest).SendString("Could not open file") + } + + defer buffer.Close() + + f, err := os.Create(fmt.Sprintf("%s/%s", DestinationPath, file.Filename)) + if err != nil { + return c.Status(fiber.StatusBadRequest).SendString("Could not Create file") + } + + data, err := ioutil.ReadAll(buffer) + + if err != nil { + return c.Status(fiber.StatusBadRequest).SendString("Failed reading buffer") + } + + _, err = f.Write(data) + + if err != nil { + return c.Status(fiber.StatusBadRequest).SendString("Failed writing file") + } + + return c.SendString("Catchy-Cat, File Caught!") +} diff --git a/public/catchy-cat.png b/public/catchy-cat.png new file mode 100644 index 0000000..1c34aac Binary files /dev/null and b/public/catchy-cat.png differ