Fixes for docker

This commit is contained in:
Neale Pickett 2022-09-25 12:34:34 -06:00
parent 09e8798cb1
commit 98b6afccde
8 changed files with 43 additions and 19 deletions

3
.gitignore vendored
View File

@ -1,2 +1 @@
wallart.bin
wallart.png
cache/

View File

@ -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

14
build/Dockerfile Normal file
View File

@ -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"]

4
build/build.sh Executable file
View File

@ -0,0 +1,4 @@
#! /bin/sh
cd $(dirname $0)/..
docker build -t wallart-server -f build/Dockerfile .

View File

@ -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)
}

5
go.mod
View File

@ -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

2
go.sum
View File

@ -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=