diff --git a/.editorconfig b/.editorconfig index 8f0c87a..29e5a75 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,24 @@ -[*.cr] -charset = utf-8 -end_of_line = lf -insert_final_newline = true +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] + +# Change these settings to your own preference indent_style = space indent_size = 2 + +# We recommend you to keep these unchanged +end_of_line = lf +charset = utf-8 trim_trailing_whitespace = true +insert_final_newline = true + +[Dockerfile] +indent_style = space +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false diff --git a/.env.production b/.env.production new file mode 100755 index 0000000..74d9768 --- /dev/null +++ b/.env.production @@ -0,0 +1,25 @@ +# Rename to .env and fill in values + +# Where to redirect for the main route. +FALLBACK_URL="https://www.clearlink.com/contact" + +# Redis accessible URL +REDIS_URL="redis://localhost:6379/0" + +# Secret token to verify new app key requests. +SECRET_TOKEN="9c7847a350d9773f96de3af44089bb4df91500345353d01abd3078d11b0c4bd2" + +# The length of the generated unique short code in bytes. See Random::Secure#hex +# SHORT_ID_SIZE=3 + +# The short domain this app is hosted at. +SHORT_URL="https://mpng.in" + +# Optional PORT specification +# PORT=7001 + +# Optional name of CSV report +# LINK_REPORT_CSV_NAME=MPNGIN_Link_Report + +# Uncomment when building for release/production. +# KEMAL_ENV=production diff --git a/Dockerfile b/Dockerfile index ff425d0..e2222fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,41 +1,37 @@ -FROM crystallang/crystal:1.2.2 AS builder +FROM crystallang/crystal:1.9.2 as builder -WORKDIR /app +ARG APP_ENV=production +ENV KEMAL_ENV=${APP_ENV} -COPY . . +WORKDIR /build -ARG APP_ENV=production +COPY shard.yml shard.lock ./ +RUN echo "KEMAL_ENV=${APP_ENV}" >> .env && \ + shards install --frozen --production -RUN shards install --production && \ - echo "KEMAL_ENV=${APP_ENV}" >> .env && \ - KEMAL_ENV=${APP_ENV} crystal build --release src/mpngin.cr; +COPY . . +RUN shards build --production --release --no-debug --static --progress -FROM debian:stable-slim +FROM debian:buster-slim as production +ARG APP_ENV=production ARG PORT=7001 -ENV LANG C.UTF-8 -ENV DEBIAN_FRONTEND noninteractive - -EXPOSE ${PORT} +ENV KEMAL_ENV=${APP_ENV} WORKDIR /app -COPY --from=builder /app/mpngin . -COPY --from=builder /app/.env . +STOPSIGNAL SIGQUIT + +COPY --from=builder /build/bin/mpngin . +COPY .env.production .env -RUN apt-get update && \ - apt-get upgrade -y && \ - apt-get install -y libssl1.1 libevent-2.1-7 && \ - apt-get auto-remove -y && \ - apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ - useradd -m app-user && \ +RUN useradd -m app-user && \ chown -R app-user /home/app-user && \ echo "PORT=${PORT}" >> .env && \ - chown -R app-user .; + echo "KEMAL_ENV=${KEMAL_ENV}" >> .env && \ + chown -R app-user /app; USER app-user -STOPSIGNAL SIGQUIT - -ENTRYPOINT [ "./mpngin" ] -CMD [ "./mpngin" ] \ No newline at end of file +ENTRYPOINT ["./mpngin"] +CMD ["./mpngin"] diff --git a/README.md b/README.md index 9460036..af5763b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A simple and fast URL shortener with built in stats. Requires Redis and assumes ## Installation Copy `env.example` to `.env` and set the values. For the -`SECRET_TOKEN`, try `Random::Secure.hex(32)`. +`SECRET_TOKEN` you can generate one with `$ crystal eval "puts Random::Secure.hex(32)"`. NOTE: If building in release mode for production, ensure `KEMAL_ENV` is uncommented in `.env` and set to `production`. @@ -21,14 +21,14 @@ For production, make a release build with `KEMAL_ENV` uncommented in `.env` and ```sh # Assumes .env file is correctly filled out, including KEMAL_ENV=production -crystal build --release src/mpngin.cr +shards build --production --release --static --progress ``` The `.env` must accompany the built binary at the same filesystem level. Example production tree: ``` mpngin -|_ mpgin (binary) +|_ mpngin (binary) |_ .env ``` diff --git a/env.sample b/env.sample index 5ad6985..f34fccd 100644 --- a/env.sample +++ b/env.sample @@ -7,6 +7,7 @@ FALLBACK_URL= REDIS_URL=redis://127.0.0.1:6379/0 # Secret token to verify new app key requests. +# You can generate one with `$ crystal eval "puts Random::Secure.hex(32)"` SECRET_TOKEN= # The short domain this app is hosted at.