szabodanika / microbin

A secure, configurable file-sharing and URL shortening web app written in Rust.
https://microbin.eu
BSD 3-Clause "New" or "Revised" License
2.65k stars 163 forks source link

Docker environment variables #66

Closed modem7 closed 1 year ago

modem7 commented 1 year ago

Heya @szabodanika,

Great project you have!

I understand that you're not so hot on Docker stuff currently, and there's quite a few variables that aren't available.

Would something like this be suitable for starters (I'm sure others could improve on it)?

#!/bin/bash

set -e

# Website
# https://github.com/szabodanika/microbin

# Version variables
MICROBINVER=$(microbin --version)

# Software versions
echo $MICROBINVER

# Declare Variables
PORT="${PORT:-"8080"}"
THREADS="${THREADS:-"1"}"

# Declare the string array
arrVar=()

# Add to string array if variable is true
if [ "$EDITABLE" == "true" ]
  then
    arrVar+=("--editable")
fi

if [ "$HIDE_FOOTER" == "true" ]
  then
    arrVar+=("--hide-footer")
fi

if [ "$HIGHLIGHTSYNTAX" == "true" ]
  then
    arrVar+=("--highlightsyntax")
fi

if [ "$PRIVATE" == "true" ]
  then
    arrVar+=("--private")
fi

if [ "$HIDE_FOOTER" == "true" ]
  then
    arrVar+=("--hide-footer")
fi

if [ "$HIDE_HEADER" == "true" ]
  then
    arrVar+=("--hide-header")
fi

if [ "$HIDE_LOGO" == "true" ]
  then
    arrVar+=("--hide-logo")
fi

if [ "$NO_LISTING" == "true" ]
  then
    arrVar+=("--no-listing")
fi

if [ "$PURE_HTML" == "true" ]
  then
    arrVar+=("--pure-html")
fi

if [ "$READ_ONLY" == "true" ]
  then
    arrVar+=("--readonly")
fi

if [ "$WIDE" == "true" ]
  then
    arrVar+=("--wide")
fi

#########################
#User Editable Variables#
#########################

if [ -v AUTH_USERNAME ]
  then
    arrVar+=("--auth-username $AUTH_USERNAME")
fi

if [ -v AUTH_PASSWORD ]
  then
    arrVar+=("--auth-password $AUTH_PASSWORD")
fi

if [ -v FOOTER_TEXT ]
  then
    arrVar+=("--footer_text $FOOTER_TEXT")
fi

if [ -v BIND_ADDR ]
  then
    arrVar+=("--bind $BIND_ADDR")
fi

# Save and define variables
printf -v str ' %s' "${arrVar[@]}"  # save to variable str without printing
MICROBIN_VARS="${str:1}"  # to remove the leading space 

# Show defined variables
echo "Microbin starting with following variables: microbin --port $PORT --threads $THREADS $MICROBIN_VARS"

# Start Microbin
microbin --port $PORT --threads $THREADS $MICROBIN_VARS
This would give you the following variables: Variable Value Result of true
EDITABLE true false --editable
HIDE_FOOTER true false --hide-footer
HIGHLIGHTSYNTAX true false --highlightsyntax
PRIVATE true false --private
HIDE_FOOTER true false --hide-footer
HIDE_HEADER true false --hide-header
HIDE_LOGO true false --hide-logo
NO_LISTING true false --no-listing
PURE_HTML true false --pure-html
READ_ONLY true false --readonly
WIDE true false --wide
AUTH_USERNAME user value --auth-username uservalue
AUTH_PASSWORD user value --auth-password uservalue
FOOTER_TEXT user value --footer_text uservalue
BIND_ADDR user value --bind uservalue
Variable Value Default Value
PORT user value 8080
THREADS user value 1

This way, no more needing to do commands or anything "non-standard" in Docker, and can easily be changed/added as new features come along without changing how you do things on the backend.

The best option of course, is in the backend, to import OS Variables and use them as required, but this should be a suitable workaround for now.

I can also tweak the dockerfile to be more in-line with buildkit, with better layer caching.

I'd be happy to raise a PR if you'd like?

modem7 commented 1 year ago

This probably needs editing depending on what https://github.com/szabodanika/microbin/pull/34 has added.

abyss commented 1 year ago

@modem7 I think this issue might be complete as a result of #34 unless there's missing options?

modem7 commented 1 year ago

@modem7 I think this issue might be complete as a result of #34 unless there's missing options?

Looks like it! I'll close for now, and we can revisit if needs be!