From 322fcc3979da2d572bbcbec77b85c669d83e9a38 Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Sat, 12 Jan 2019 23:21:50 +0100 Subject: [PATCH] Docker support --- Makefile | 12 +++++++++-- docker/Dockerfile | 16 ++++++++++++++ docker/main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 docker/Dockerfile create mode 100644 docker/main.go diff --git a/Makefile b/Makefile index c8708ae..0f44be4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -run: +run: ${MAKE} -j2 backgroundrun backgroundts build: tsc res/static/res/typescript/lib/*.ts --outFile res/static/res/script/pixellib.js \ @@ -12,5 +12,13 @@ deps: backgroundrun: go run main.go backgroundts: - tsc --watch --project res/static/res/typescript/home + tsc --watch --project res/static/res/typescript/home --project res/static/res/typescript/textupload + +docker: + go build -o docker/pixeldrain-web docker/main.go + chmod +x docker/pixeldrain-web + docker build --tag pixeldrain-web -f docker/Dockerfile . + rm docker/pixeldrain-web + +.PHONY: docker diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..b417fec --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,16 @@ +FROM ubuntu + +RUN mkdir -p \ + /bin \ + /opt/pixeldrain/res + +COPY docker/pixeldrain-web /bin/pixeldrain-web +COPY res /opt/pixeldrain/res + +EXPOSE 8081 + +ENV api_url_external="" \ + api_url_internal="" \ + debug_mode="" + +ENTRYPOINT [ "/bin/pixeldrain-web" ] diff --git a/docker/main.go b/docker/main.go new file mode 100644 index 0000000..fc309bc --- /dev/null +++ b/docker/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "net/http" + "os" + + "fornaxian.com/pixeldrain-web/init/conf" + "fornaxian.com/pixeldrain-web/webcontroller" + + "github.com/Fornaxian/log" + "github.com/julienschmidt/httprouter" +) + +// This is just a launcher for the web server. During testing the app would +// be directly embedded by another Go project. And when deployed it will run +// independently. +func main() { + log.Info("Starting web UI server in docker mode (PID %v)", os.Getpid()) + + var webconf = &conf.PixelWebConfig{ + StaticResourceDir: "/opt/pixeldrain/res/static", + TemplateDir: "/opt/pixeldrain/res/template", + } + + if webconf.APIURLExternal, _ = os.LookupEnv("api_url_external"); webconf.APIURLExternal == "" { + webconf.APIURLExternal = "/api" + } + if webconf.APIURLInternal, _ = os.LookupEnv("api_url_internal"); webconf.APIURLInternal == "" { + panic("internal API URL is required. Please set api_url_internal") + } + + // Check debug mode. When debug mode is enabled debug logging will be + // enabled, templates will be parsed on every request and analytics tracking + // will be disabled + switch d, _ := os.LookupEnv("debug_mode"); d { + case "1", "yes", "y", "true": + webconf.DebugMode = true + log.SetLogLevel(log.LevelDebug) + default: + webconf.DebugMode = false + log.SetLogLevel(log.LevelInfo) + } + + // Web path prefix + prefix, _ := os.LookupEnv("api_path_prefix") + + // Init the http router + r := httprouter.New() + webcontroller.New(r, prefix, webconf) + + err := http.ListenAndServe(":8081", r) + if err != nil { + log.Error("Can't listen and serve Pixeldrain Web: %v", err) + } +}