Skip to content

Commit

Permalink
Update nix derivation with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoDaniels authored Jan 8, 2024
1 parent 83b6a62 commit f23878c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Create [AWS CloudFront Lambda@Edge](https://docs.aws.amazon.com/lambda/latest/dg

This package uses an [Elm headless worker](https://package.elm-lang.org/packages/elm/core/latest/Platform#worker) under
the hood requiring [ports](https://guide.elm-lang.org/interop/ports) and a small JavaScript snippet to be bundled and
deployed into AWS CloudFront Lambda@Edge.
deployed into AWS CloudFront Lambda@Edge. Check the [examples folder](/examples).

Elm part:

Expand Down Expand Up @@ -75,9 +75,9 @@ makeLambda.buildElmAWSCloudFront {
elmSrc = ./elm-srcs.nix;
elmRegistryDat = ./registry.dat;
lambdas = [
{ module = "MyModuleTwo"; }
{ module = ./src/MyModuleTwo.elm; }
{
module = "MyModuleOne";
module = ./src/MyModuleOne.elm;
flags = [ ''token:"token-goes-here"'' ''url:"url-goes-here"'' ];
}
];
Expand Down
22 changes: 12 additions & 10 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
stdenv.mkDerivation {
name = "elm-aws-cloudfront";
src = src;
buildInputs =
[ pkgs.elmPackages.elm pkgs.esbuild ];
buildInputs = [ pkgs.elmPackages.elm pkgs.esbuild ];

buildPhase = pkgs.elmPackages.fetchElmDeps {
elmPackages = import elmSrc;
Expand All @@ -14,10 +13,11 @@

installPhase = let
buildFlags = flags: "{flags: {${lib.concatStringsSep ", " flags}}}";
moduleName = module: lib.removeSuffix ".elm" (baseNameOf module);
jsHandler = lambda:
pkgs.writeText "${lambda.module}.js" ''
pkgs.writeText "${moduleName lambda.module}.js" ''
const {Elm} = require('./elm.tmp');
const app = Elm.${lambda.module}.init(${
const app = Elm.${moduleName lambda.module}.init(${
if lambda ? "flags" then buildFlags lambda.flags else ""
});
exports.handler = (event, context, callback) => {
Expand All @@ -32,14 +32,16 @@
in "${lib.concatStrings (map (lambda: ''
mkdir -p $out
echo "creating js handler for ${lambda.module}"
cp ${jsHandler lambda} $out/${lambda.module}.tmp.js
echo "creating js handler for ${baseNameOf lambda.module}"
cp ${jsHandler lambda} $out/${moduleName lambda.module}.tmp.js
echo "compiling ${lambda.module}.elm"
${pkgs.elmPackages.elm}/bin/elm make src/${lambda.module}.elm --optimize --output $out/elm.tmp.js
echo "compiling ${baseNameOf lambda.module}"
${pkgs.elmPackages.elm}/bin/elm make ${lambda.module} --optimize --output $out/elm.tmp.js
echo "bundle ${lambda.module}"
${pkgs.esbuild}/bin/esbuild --bundle --minify --pure:A2 --pure:A3 --pure:A4 --pure:A5 --pure:A6 --pure:A7 --pure:A8 --pure:A9 --pure:F2 --pure:F3 --pure:F3 --pure:F4 --pure:F5 --pure:F6 --pure:F7 --pure:F8 --pure:F9 --platform=node --outfile=$out/${lambda.module}.js $out/${lambda.module}.tmp.js
echo "bundle ${moduleName lambda.module}"
${pkgs.esbuild}/bin/esbuild --bundle --minify --pure:A2 --pure:A3 --pure:A4 --pure:A5 --pure:A6 --pure:A7 --pure:A8 --pure:A9 --pure:F2 --pure:F3 --pure:F3 --pure:F4 --pure:F5 --pure:F6 --pure:F7 --pure:F8 --pure:F9 --platform=node --outfile=$out/${
moduleName lambda.module
}.js $out/${moduleName lambda.module}.tmp.js
echo "cleanup temporary files"
rm $out/*.tmp.js
Expand Down
26 changes: 26 additions & 0 deletions examples/MyModule.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
port module MyModule exposing (main)

import CloudFront exposing (Model, Msg, cloudFront)
import CloudFront.Header exposing (withHeader)
import CloudFront.Lambda exposing (originResponse, toResponse)
import Json.Decode as Decode
import Json.Encode as Encode


port inputEvent : (Decode.Value -> msg) -> Sub msg


port outputEvent : Encode.Value -> Cmd msg


main : Program () (Model ()) Msg
main =
( inputEvent, outputEvent )
|> (originResponse
(\{ response, request } _ ->
response
|> withHeader { key = "cache-control", value = "public, max-age=31536000" }
|> toResponse
)
|> cloudFront
)
27 changes: 27 additions & 0 deletions examples/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let
cloudfront = pkgs.callPackage (pkgs.stdenv.mkDerivation {
name = "elm-aws-cloudfront";
src = fetchGit {
url = "https://github.com/MarcoDaniels/elm-aws-cloudfront.git";
ref = "refs/tags/nix-1.0.0";
};
installPhase = ''
mkdir -p $out
cp $src/default.nix $out
'';
}) { };

in cloudfront.buildElmAWSCloudFront {
src = ../../.;
elmSrc = ../../nix/elm-srcs.nix;
elmRegistryDat = ../../nix/registry.dat;
lambdas = [
{ module = ./src/WebsiteRequest.elm; }
{ module = ./src/WebsiteResponse.elm; }
{
module = ./src/AssetRequest.elm;
flags = [ ''token:"token"'' ''domain:"domain"'' ];
}
{ module = ./src/AssetResponse.elm; }
];
}
10 changes: 10 additions & 0 deletions examples/myModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {Elm} = require('./elm');
const app = Elm.MyModule.init();
exports.handler = (event, context, callback) => {
const caller = (output) => {
callback(null, output);
app.ports.outputEvent.unsubscribe(caller);
}
app.ports.outputEvent.subscribe(caller);
app.ports.inputEvent.send(event);
}

0 comments on commit f23878c

Please sign in to comment.