From 98b6afccde01a7745684cd1746ddb91fd10b9f36 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sun, 25 Sep 2022 12:34:34 -0600 Subject: [PATCH] Fixes for docker --- .gitignore | 3 +-- README.md | 14 ++++++++++++++ build/Dockerfile | 14 ++++++++++++++ build/build.sh | 4 ++++ cmd/{wallartd => wallart-server}/main.go | 20 +++++++++----------- cmd/{wallartd => wallart-server}/pixil.go | 0 go.mod | 5 +---- go.sum | 2 -- 8 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 build/Dockerfile create mode 100755 build/build.sh rename cmd/{wallartd => wallart-server}/main.go (83%) rename cmd/{wallartd => wallart-server}/pixil.go (100%) diff --git a/.gitignore b/.gitignore index 41c6512..e934adf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -wallart.bin -wallart.png +cache/ diff --git a/README.md b/README.md index f88fe22..0c67e28 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,17 @@ This is a server for my network-connected wall art thingy. It is built to accept `.pixil` files, which are generated by https://pixilart.com/. + +The wall art device will fetch `wallart.bin` file from this server. +Other things should use `wallart.png`. + + +Building It +------------- + + ./build/build.sh + +Running it +------------- + + docker run --name=wallart-server -p 8080:8080 -v /srv/wallart-server:/cache wallart-server diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..40e3131 --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1-alpine AS build +WORKDIR /go/src/git.woozle.org/neale/wallart-server +COPY go.* cmd ./ +RUN CGO_ENABLED=0 GOOS=linux go install ./... + +FROM alpine AS runtime +WORKDIR /target +RUN mkdir cache +COPY web web +COPY --from=build /go/bin/wallart-server . + +FROM scratch +COPY --from=runtime /target / +ENTRYPOINT ["/wallart-server"] diff --git a/build/build.sh b/build/build.sh new file mode 100755 index 0000000..a169b05 --- /dev/null +++ b/build/build.sh @@ -0,0 +1,4 @@ +#! /bin/sh + +cd $(dirname $0)/.. +docker build -t wallart-server -f build/Dockerfile . diff --git a/cmd/wallartd/main.go b/cmd/wallart-server/main.go similarity index 83% rename from cmd/wallartd/main.go rename to cmd/wallart-server/main.go index ac6ae5d..b6e30d8 100644 --- a/cmd/wallartd/main.go +++ b/cmd/wallart-server/main.go @@ -8,11 +8,14 @@ import ( "image/png" "net/http" "os" + "path" "strings" "github.com/kettek/apng" ) +var cacheDirectory string + func handleUpload(w http.ResponseWriter, r *http.Request) { inf, header, err := r.FormFile("image") if err != nil { @@ -37,14 +40,14 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { return } - outRaw, err := os.OpenFile("wallart.bin", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + outRaw, err := os.Create(path.Join(cacheDirectory, "wallart.bin")) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer outRaw.Close() - outPng, err := os.Create("wallart.png") + outPng, err := os.Create(path.Join(cacheDirectory, "wallart.png")) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -101,21 +104,16 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "It worked") } -func handleWallartBin(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "wallart.bin") -} -func handleWallartPng(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "wallart.png") -} - func main() { listen := flag.String("listen", ":8080", "listen address") web := flag.String("web", "web", "web directory") + flag.StringVar(&cacheDirectory, "cache", "cache", "cache directory") flag.Parse() - http.HandleFunc("/wallart.bin", handleWallartBin) - http.HandleFunc("/wallart.png", handleWallartPng) + cacheDir := http.Dir(cacheDirectory) http.HandleFunc("/upload", handleUpload) + http.Handle("/wallart.bin", http.FileServer(cacheDir)) + http.Handle("/wallart.png", http.FileServer(cacheDir)) http.Handle("/", http.FileServer(http.Dir(*web))) http.ListenAndServe(*listen, nil) } diff --git a/cmd/wallartd/pixil.go b/cmd/wallart-server/pixil.go similarity index 100% rename from cmd/wallartd/pixil.go rename to cmd/wallart-server/pixil.go diff --git a/go.mod b/go.mod index 35f91a8..4db3c06 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,4 @@ module git.woozle.org/neale/wallartd go 1.18 -require ( - github.com/kettek/apng v0.0.0-20220823221153-ff692776a607 - github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 -) +require github.com/kettek/apng v0.0.0-20220823221153-ff692776a607 diff --git a/go.sum b/go.sum index a6a2b6b..f642237 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,2 @@ github.com/kettek/apng v0.0.0-20220823221153-ff692776a607 h1:8tP9cdXzcGX2AvweVVG/lxbI7BSjWbNNUustwJ9dQVA= github.com/kettek/apng v0.0.0-20220823221153-ff692776a607/go.mod h1:x78/VRQYKuCftMWS0uK5e+F5RJ7S4gSlESRWI0Prl6Q= -github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= -github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=