diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d097a2f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +# syntax=docker/dockerfile:1 -*-python-*- +FROM golang:1.20-alpine as builder + +RUN apk update +RUN apk upgrade +RUN apk add --no-cache git make bash binutils + +RUN git --version + +WORKDIR /work + +COPY go.mod . +RUN go mod download +COPY . . +RUN make && strip upd + +FROM alpine:3.17 +LABEL maintainer="Uploads Author " + +RUN install -o 1001 -g 1001 -d /data + +WORKDIR /app +COPY --from=builder /work/upd /app/upd + +ENV UPD_LISTEN=:8080 +ENV UPD_STORAGEDIR=/data +ENV UPD_DBFILE=/data/bbolt.db +ENV UPD_DEBUG=1 + +USER 1001:1001 +EXPOSE 8080 +VOLUME /data +CMD ["/app/upd"] diff --git a/Makefile b/Makefile index 755d4b6..ad3af87 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,76 @@ -all: - make -C upd - make -C upctl + +# Copyright © 2023 Thomas von Dein + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +# +# no need to modify anything below +tool = upd +version = $(shell egrep "= .v" cfg/config.go | cut -d'=' -f2 | cut -d'"' -f 2) +archs = android darwin freebsd linux netbsd openbsd windows +PREFIX = /usr/local +UID = root +GID = 0 +BRANCH = $(shell git branch --show-current) +COMMIT = $(shell git rev-parse --short=8 HEAD) +BUILD = $(shell date +%Y.%m.%d.%H%M%S) +VERSION := $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version)) +HAVE_POD := $(shell pod2text -h 2>/dev/null) + +all: buildlocal + +buildlocal: + go build -ldflags "-X 'github.com/tlinden/up/upd/cfg.VERSION=$(VERSION)'" + +buildimage: clean + docker-compose --verbose build + +release: + ./mkrel.sh $(tool) $(version) + gh release create $(version) --generate-notes releases/* + +install: buildlocal + install -d -o $(UID) -g $(GID) $(PREFIX)/bin + install -o $(UID) -g $(GID) -m 555 $(tool) $(PREFIX)/sbin/ clean: - make -C upd clean - make -C upctl clean + rm -rf $(tool) releases coverage.out + +test: + go test -v ./... +# bash t/test.sh + +singletest: + @echo "Call like this: ''make singletest TEST=TestX1 MOD=lib" + go test -run $(TEST) github.com/tlinden/upd/$(MOD) + +cover-report: + go test ./... -cover -coverprofile=coverage.out + go tool cover -html=coverage.out + +show-versions: buildlocal + @echo "### upd version:" + @./upd --version + + @echo + @echo "### go module versions:" + @go list -m all + + @echo + @echo "### go version used for building:" + @grep -m 1 go go.mod + +goupdate: + go get -t -u=patch ./... diff --git a/upd/api/auth.go b/api/auth.go similarity index 100% rename from upd/api/auth.go rename to api/auth.go diff --git a/upd/api/cleaner.go b/api/cleaner.go similarity index 100% rename from upd/api/cleaner.go rename to api/cleaner.go diff --git a/upd/api/common.go b/api/common.go similarity index 100% rename from upd/api/common.go rename to api/common.go diff --git a/upd/api/common_test.go b/api/common_test.go similarity index 100% rename from upd/api/common_test.go rename to api/common_test.go diff --git a/upd/api/db.go b/api/db.go similarity index 100% rename from upd/api/db.go rename to api/db.go diff --git a/upd/api/fileio.go b/api/fileio.go similarity index 100% rename from upd/api/fileio.go rename to api/fileio.go diff --git a/upd/api/handlers.go b/api/handlers.go similarity index 100% rename from upd/api/handlers.go rename to api/handlers.go diff --git a/upd/api/server.go b/api/server.go similarity index 100% rename from upd/api/server.go rename to api/server.go diff --git a/upd/api/timestamp.go b/api/timestamp.go similarity index 100% rename from upd/api/timestamp.go rename to api/timestamp.go diff --git a/upd/cfg/config.go b/cfg/config.go similarity index 100% rename from upd/cfg/config.go rename to cfg/config.go diff --git a/upd/cmd/root.go b/cmd/root.go similarity index 100% rename from upd/cmd/root.go rename to cmd/root.go diff --git a/docker/docker-compose.yml b/docker-compose.yml similarity index 100% rename from docker/docker-compose.yml rename to docker-compose.yml diff --git a/docker/Dockerfile b/docker/Dockerfile deleted file mode 100644 index eaea402..0000000 --- a/docker/Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -# syntax=docker/dockerfile:1 -*-python-*- -FROM golang:1.18-alpine -ARG VERSION -LABEL upd_version=0.1 - -ENV HOME="/" \ - OS_ARCH="amd64" \ - OS_FLAVOUR="alpine" \ - OS_NAME="linux" - -RUN echo $VERSION -RUN install -o 1001 -g 1001 -d /data - -WORKDIR /build -RUN mkdir -p api cfg cmd -COPY upd/api/* ./api/ -COPY upd/cfg/* ./cfg/ -COPY upd/cmd/* ./cmd/ -COPY upd/*.* ./ -RUN go mod tidy -#RUN echo $VERSION -# doesn't work, see -# https://blog.alexellis.io/inject-build-time-vars-golang/ -RUN go build -ldflags "-X upd/cfg.VERSION=$VERSION" -RUN echo $VERSION > version -RUN ls -l - - -WORKDIR /app -RUN cp /build/upd /app/upd -#RUN rm -rf /build - -ENV UPD_LISTEN=:8080 -ENV UPD_STORAGEDIR=/data -ENV UPD_DBFILE=/data/bbolt.db -ENV UPD_DEBUG=1 - -USER 1001:1001 -EXPOSE 8080 -VOLUME /data -CMD ["/app/upd"] diff --git a/docker/Makefile b/docker/Makefile deleted file mode 100644 index de893cd..0000000 --- a/docker/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -upd = ../upd/upd -version = $(shell egrep "= .v" ../upd/cfg/config.go | cut -d'=' -f2 | cut -d'"' -f 2) -BRANCH = $(shell git branch --show-current) -COMMIT = $(shell git rev-parse --short=8 HEAD) -BUILD = $(shell date +%Y.%m.%d.%H%M%S) -VERSION := $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version)) - -v: - @echo $(VERSION) - -build: - cp -a ../updlocal upd - make -C upd clean - docker-compose build --build-arg VERSION=$(VERSION) diff --git a/upd/go.mod b/go.mod similarity index 100% rename from upd/go.mod rename to go.mod diff --git a/upd/go.sum b/go.sum similarity index 100% rename from upd/go.sum rename to go.sum diff --git a/upd/main.go b/main.go similarity index 100% rename from upd/main.go rename to main.go diff --git a/upd/nokeys.hcl b/nokeys.hcl similarity index 100% rename from upd/nokeys.hcl rename to nokeys.hcl diff --git a/upd/upd.hcl b/upd.hcl similarity index 100% rename from upd/upd.hcl rename to upd.hcl diff --git a/upd/Makefile b/upd/Makefile deleted file mode 100644 index 1894611..0000000 --- a/upd/Makefile +++ /dev/null @@ -1,74 +0,0 @@ - -# Copyright © 2023 Thomas von Dein - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -# -# no need to modify anything below -tool = upd -version = $(shell egrep "= .v" cfg/config.go | cut -d'=' -f2 | cut -d'"' -f 2) -archs = android darwin freebsd linux netbsd openbsd windows -PREFIX = /usr/local -UID = root -GID = 0 -BRANCH = $(shell git branch --show-current) -COMMIT = $(shell git rev-parse --short=8 HEAD) -BUILD = $(shell date +%Y.%m.%d.%H%M%S) -VERSION := $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version)) -HAVE_POD := $(shell pod2text -h 2>/dev/null) - -all: buildlocal - - -buildlocal: - go build -ldflags "-X 'github.com/tlinden/up/upd/cfg.VERSION=$(VERSION)'" - -release: - ./mkrel.sh $(tool) $(version) - gh release create $(version) --generate-notes releases/* - -install: buildlocal - install -d -o $(UID) -g $(GID) $(PREFIX)/bin - install -o $(UID) -g $(GID) -m 555 $(tool) $(PREFIX)/sbin/ - -clean: - rm -rf $(tool) releases coverage.out - -test: - go test -v ./... -# bash t/test.sh - -singletest: - @echo "Call like this: ''make singletest TEST=TestX1 MOD=lib" - go test -run $(TEST) github.com/tlinden/upd/$(MOD) - -cover-report: - go test ./... -cover -coverprofile=coverage.out - go tool cover -html=coverage.out - -show-versions: buildlocal - @echo "### upd version:" - @./upd --version - - @echo - @echo "### go module versions:" - @go list -m all - - @echo - @echo "### go version used for building:" - @grep -m 1 go go.mod - -goupdate: - go get -t -u=patch ./... diff --git a/upd/README.md b/upd/README.md deleted file mode 100644 index e6527c9..0000000 --- a/upd/README.md +++ /dev/null @@ -1,20 +0,0 @@ -## Test the server - -### single file upload - -curl -X POST localhost:8080/api/putfile -F "upload[]=@/home/scip/2023-02-06_10-51.png" -H "Content-Type: multipart/form-data" - -get with: - -curl -o x http://localhost:8080/api/getfile/05988587-cc1c-4590-9dc0-564b8a912686/2023-02-06_10-51.png - -### multiple files upload - -curl -X POST localhost:8080/api/putfile -F "upload[]=@/home/scip/2023-02-06_10-51.png" -F "upload[]=@/home/scip/pgstat.png" -H "Content-Type: multipart/form-data" - -## TODO - -- return HTTP errors as JSON as well -- add auto expire customization, e.g. expire after 1 day -- add cleanup storage per api call (single id and everything) -- add oauth2