Compare commits
4 commits
96341b8367
...
5946f9d96a
Author | SHA1 | Date | |
---|---|---|---|
5946f9d96a | |||
df941103ba | |||
ebc18d2b92 | |||
50cc6fc530 |
5 changed files with 66 additions and 10 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,4 +2,5 @@
|
|||
*.ez
|
||||
/build
|
||||
erl_crash.dump
|
||||
/priv
|
||||
/priv
|
||||
**/geofeed.csv
|
||||
|
|
49
Dockerfile
Normal file
49
Dockerfile
Normal file
|
@ -0,0 +1,49 @@
|
|||
# Build args
|
||||
ARG PORT=8080
|
||||
ARG GLEAM_VERSION=v1.11.1
|
||||
|
||||
# Build stage - compile the application
|
||||
FROM ghcr.io/gleam-lang/gleam:${GLEAM_VERSION}-erlang-alpine AS builder
|
||||
|
||||
# Add project code
|
||||
COPY ./common /build/common
|
||||
COPY ./client /build/client
|
||||
COPY ./server /build/server
|
||||
|
||||
# Install dependencies for all projects
|
||||
RUN cd /build/common && gleam deps download
|
||||
RUN cd /build/client && gleam deps download
|
||||
RUN cd /build/server && gleam deps download
|
||||
|
||||
# Compile the client code and output to server's static directory
|
||||
RUN cd /build/client \
|
||||
&& gleam add --dev lustre_dev_tools \
|
||||
&& gleam run -m lustre/dev build app --minify --outdir=/build/server/priv/static
|
||||
|
||||
# Compile the server code
|
||||
RUN cd /build/server \
|
||||
&& gleam export erlang-shipment
|
||||
|
||||
# Runtime stage - slim image with only what's needed to run
|
||||
FROM ghcr.io/gleam-lang/gleam:${GLEAM_VERSION}-erlang-alpine
|
||||
|
||||
# Copy the compiled server code from the builder stage
|
||||
COPY --from=builder /build/server/build/erlang-shipment /app
|
||||
|
||||
# Set up the entrypoint
|
||||
WORKDIR /app
|
||||
RUN echo '#!/bin/sh\nexec ./entrypoint.sh "$@"' > /app/start.sh \
|
||||
&& chmod +x /app/start.sh
|
||||
|
||||
# Add volume for accessing geofeed from host
|
||||
VOLUME /data
|
||||
|
||||
# Set environment variables
|
||||
ENV GEOFEED_PATH=/data/geofeed.csv
|
||||
ENV PORT=${PORT}
|
||||
|
||||
# Expose the port the server will run on
|
||||
EXPOSE ${PORT}
|
||||
|
||||
# Run the server
|
||||
CMD ["/app/start.sh", "run"]
|
|
@ -12,7 +12,7 @@ wisp = ">= 1.8.0 and < 2.0.0"
|
|||
mist = ">= 5.0.2 and < 6.0.0"
|
||||
lustre = ">= 5.2.1 and < 6.0.0"
|
||||
simplifile = ">= 2.3.0 and < 3.0.0"
|
||||
argv = ">= 1.0.2 and < 2.0.0"
|
||||
envoy = ">= 1.0.2 and < 2.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||
{ name = "directories", version = "1.2.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "D13090CFCDF6759B87217E8DDD73A75903A700148A82C1D33799F333E249BF9E" },
|
||||
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
|
||||
{ name = "exception", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "329D269D5C2A314F7364BD2711372B6F2C58FA6F39981572E5CA68624D291F8C" },
|
||||
|
@ -31,7 +30,7 @@ packages = [
|
|||
]
|
||||
|
||||
[requirements]
|
||||
argv = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
envoy = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
gleam_erlang = { version = ">= 1.2.0 and < 2.0.0" }
|
||||
gleam_http = { version = ">= 4.1.0 and < 5.0.0" }
|
||||
gleam_json = { version = ">= 3.0.2 and < 4.0.0" }
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import argv
|
||||
import envoy
|
||||
import gleam/dynamic/decode
|
||||
import gleam/erlang/process
|
||||
import gleam/http.{Get, Post}
|
||||
import gleam/int
|
||||
import gleam/json
|
||||
import gleam/result
|
||||
import lustre/attribute
|
||||
import lustre/element
|
||||
import lustre/element/html
|
||||
|
@ -16,15 +17,21 @@ import wisp/wisp_mist
|
|||
|
||||
pub fn main() {
|
||||
wisp.configure_logger()
|
||||
let secret_key_base = wisp.random_string(64)
|
||||
let assert Ok(priv_directory) = wisp.priv_directory("server")
|
||||
let static_directory = priv_directory <> "/static"
|
||||
let assert [port_str, geofeed_path, ..] = argv.load().arguments
|
||||
let assert Ok(port) = int.parse(port_str)
|
||||
let geofeed_path = envoy.get("GEOFEED_PATH") |> result.unwrap("geofeed.csv")
|
||||
let secret_key_base =
|
||||
envoy.get("SECRET_KEY") |> result.unwrap(wisp.random_string(64))
|
||||
let bind_address = envoy.get("BIND_ADDRESS") |> result.unwrap("0.0.0.0")
|
||||
let port =
|
||||
envoy.get("PORT")
|
||||
|> result.try(int.parse)
|
||||
|> result.unwrap(8080)
|
||||
let assert Ok(_) =
|
||||
handle_request(static_directory, geofeed_path, _)
|
||||
handle_request(_, static_directory, geofeed_path)
|
||||
|> wisp_mist.handler(secret_key_base)
|
||||
|> mist.new
|
||||
|> mist.bind(bind_address)
|
||||
|> mist.port(port)
|
||||
|> mist.start
|
||||
process.sleep_forever()
|
||||
|
@ -44,9 +51,9 @@ fn app_middleware(
|
|||
}
|
||||
|
||||
fn handle_request(
|
||||
req: Request,
|
||||
static_directory: String,
|
||||
geofeed_path: String,
|
||||
req: Request,
|
||||
) -> Response {
|
||||
use req <- app_middleware(req, static_directory)
|
||||
case req.method, wisp.path_segments(req) {
|
||||
|
|
Loading…
Reference in a new issue