diff --git a/ruby-server-example/.gitignore b/ruby-server-example/.gitignore new file mode 100644 index 0000000..8871566 --- /dev/null +++ b/ruby-server-example/.gitignore @@ -0,0 +1,2 @@ + +bsf-result/ \ No newline at end of file diff --git a/ruby-server-example/Dockerfile b/ruby-server-example/Dockerfile new file mode 100644 index 0000000..4578e4d --- /dev/null +++ b/ruby-server-example/Dockerfile @@ -0,0 +1,8 @@ +# Ruby Alpine image (after using bsf dockerfile digest) +FROM ttl.sh/ruby-server:dev AS build + +# Expose the application port +EXPOSE 9898 + +# Command to run the Ruby application +CMD ["ruby", "main.rb"] diff --git a/ruby-server-example/Gemfile b/ruby-server-example/Gemfile new file mode 100644 index 0000000..d1deecc --- /dev/null +++ b/ruby-server-example/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'activesupport', '~> 7.0' diff --git a/ruby-server-example/README.md b/ruby-server-example/README.md new file mode 100644 index 0000000..5a7605f --- /dev/null +++ b/ruby-server-example/README.md @@ -0,0 +1,66 @@ +This is an Example Ruby repo + +## Install nix +``` +curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install + +``` + +``` +To get started using Nix, open a new shell or run `. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh` +``` +You can experiment if the installation was successful by runing `nix run "nixpkgs#hello"` +## Install bsf cli +nix profile install {{URL}} + + +## Initialize the projct +Now is the time to run `bsf init` which will generate the `bsf.hcl` and `bsf.locl` files. It also generates a bsf folder with all nix files requred to build the project. + +## Add files before building +`git add . ` <- Required by nix sandboxing. + +## Develop the porject +This will get you in the development shell of your application +`bsf develop` + +## Interact with the project +You can checkout the ruby version +`ruby -v` +Try to install rails +`gem install rails` + +## Create a base image with Ruby dependencies and using it the Dockerfile +```Dockerfile +# Ruby Alpine image (after using bsf dockerfile digest) +FROM ttl.sh/ruby-server AS build + +# Install all required dependencies for Ruby gems +RUN apk add --no-cache build-base libxml2-dev libxslt-dev tzdata + +WORKDIR /src + +# Copy the Gemfile and Gemfile.lock into the container +COPY Gemfile Gemfile.lock /src/ + +# Install the required gems +RUN bundle install + +# Copy the rest of the application files to the container +COPY . /src + +# Expose the application port +EXPOSE 9898 + +# Command to run the Ruby application +CMD ["ruby", "main.rb"] +``` + +Lets build this image +`docker build -t ttl.sh/ruby-server:dev` + +Now Run the image +`docker run -d -p 9898:9898 ttl.sh/ruby-server:dev` + +## Checkout your application +`curl localhost:9898/ping` here the response should be `Pong!` \ No newline at end of file diff --git a/ruby-server-example/bsf.hcl b/ruby-server-example/bsf.hcl new file mode 100644 index 0000000..730f542 --- /dev/null +++ b/ruby-server-example/bsf.hcl @@ -0,0 +1,16 @@ + +packages { + development = ["coreutils-full@9.5", "bash@5.2.15", "ruby@~3.3.4", "bundler@~2.5.16"] + runtime = ["cacert@3.95"] +} + +oci "pkgs" { + name = "ttl.sh/ruby-server" + layers = ["split(packages.runtime)", "split(packages.dev)"] + isBase = true + cmd = [] + entrypoint = [] + envVars = [] + exposedPorts = [] + importConfigs = [] +} diff --git a/ruby-server-example/bsf.lock b/ruby-server-example/bsf.lock new file mode 100644 index 0000000..1886afb --- /dev/null +++ b/ruby-server-example/bsf.lock @@ -0,0 +1,402 @@ +{ + "app": { + "name": "" + }, + "packages": [ + { + "package": { + "name": "bash", + "revision": "1ebb7d7bba2953a4223956cfb5f068b0095f84a7", + "version": "5.2.15", + "description": "GNU Bourne-Again Shell, the de facto standard shell on Linux", + "homepage": "https://www.gnu.org/software/bash", + "free": true, + "spdx_id": "GPL-3.0-or-later", + "epoch_seconds": 1653393815, + "platforms": [ + "i686-cygwin", + "x86_64-cygwin", + "x86_64-darwin", + "i686-darwin", + "aarch64-darwin", + "armv7a-darwin", + "i686-freebsd13", + "x86_64-freebsd13", + "x86_64-solaris", + "aarch64-linux", + "armv5tel-linux", + "armv6l-linux", + "armv7a-linux", + "armv7l-linux", + "i686-linux", + "loongarch64-linux", + "m68k-linux", + "microblaze-linux", + "microblazeel-linux", + "mips-linux", + "mips64-linux", + "mips64el-linux", + "mipsel-linux", + "powerpc64-linux", + "powerpc64le-linux", + "riscv32-linux", + "riscv64-linux", + "s390-linux", + "s390x-linux", + "x86_64-linux", + "aarch64-netbsd", + "armv6l-netbsd", + "armv7a-netbsd", + "armv7l-netbsd", + "i686-netbsd", + "m68k-netbsd", + "mipsel-netbsd", + "powerpc-netbsd", + "riscv32-netbsd", + "riscv64-netbsd", + "x86_64-netbsd", + "i686-openbsd", + "x86_64-openbsd", + "x86_64-redox" + ] + }, + "runtime": false + }, + { + "package": { + "name": "bundler", + "revision": "2af19cfb6aa40768c4bbefd801a136270e099191", + "version": "2.5.16", + "description": "Manage your Ruby application's gem dependencies", + "homepage": "https://bundler.io", + "free": true, + "spdx_id": "MIT", + "epoch_seconds": 1727301923, + "platforms": [ + "i686-cygwin", + "x86_64-cygwin", + "x86_64-darwin", + "i686-darwin", + "aarch64-darwin", + "armv7a-darwin", + "i686-freebsd", + "x86_64-freebsd", + "aarch64-genode", + "i686-genode", + "x86_64-genode", + "x86_64-solaris", + "javascript-ghcjs", + "aarch64-linux", + "armv5tel-linux", + "armv6l-linux", + "armv7a-linux", + "armv7l-linux", + "i686-linux", + "loongarch64-linux", + "m68k-linux", + "microblaze-linux", + "microblazeel-linux", + "mips-linux", + "mips64-linux", + "mips64el-linux", + "mipsel-linux", + "powerpc64-linux", + "powerpc64le-linux", + "riscv32-linux", + "riscv64-linux", + "s390-linux", + "s390x-linux", + "x86_64-linux", + "mmix-mmixware", + "aarch64-netbsd", + "armv6l-netbsd", + "armv7a-netbsd", + "armv7l-netbsd", + "i686-netbsd", + "m68k-netbsd", + "mipsel-netbsd", + "powerpc-netbsd", + "riscv32-netbsd", + "riscv64-netbsd", + "x86_64-netbsd", + "aarch64_be-none", + "aarch64-none", + "arm-none", + "armv6l-none", + "avr-none", + "i686-none", + "microblaze-none", + "microblazeel-none", + "mips-none", + "mips64-none", + "msp430-none", + "or1k-none", + "m68k-none", + "powerpc-none", + "powerpcle-none", + "riscv32-none", + "riscv64-none", + "rx-none", + "s390-none", + "s390x-none", + "vc4-none", + "x86_64-none", + "i686-openbsd", + "x86_64-openbsd", + "x86_64-redox", + "wasm64-wasi", + "wasm32-wasi", + "x86_64-windows", + "i686-windows" + ], + "attr_name": "bundler" + }, + "runtime": false + }, + { + "package": { + "name": "cacert", + "revision": "ac5c1886fd9fe49748d7ab80accc4c847481df14", + "version": "3.95", + "description": "A bundle of X.509 certificates of public Certificate Authorities (CA)", + "homepage": "https://curl.haxx.se/docs/caextract.html", + "free": true, + "spdx_id": "MPL-2.0", + "epoch_seconds": 1699289668, + "platforms": [ + "i686-cygwin", + "x86_64-cygwin", + "x86_64-darwin", + "i686-darwin", + "aarch64-darwin", + "armv7a-darwin", + "i686-freebsd13", + "x86_64-freebsd13", + "aarch64-genode", + "i686-genode", + "x86_64-genode", + "x86_64-solaris", + "javascript-ghcjs", + "aarch64-linux", + "armv5tel-linux", + "armv6l-linux", + "armv7a-linux", + "armv7l-linux", + "i686-linux", + "loongarch64-linux", + "m68k-linux", + "microblaze-linux", + "microblazeel-linux", + "mips-linux", + "mips64-linux", + "mips64el-linux", + "mipsel-linux", + "powerpc64-linux", + "powerpc64le-linux", + "riscv32-linux", + "riscv64-linux", + "s390-linux", + "s390x-linux", + "x86_64-linux", + "mmix-mmixware", + "aarch64-netbsd", + "armv6l-netbsd", + "armv7a-netbsd", + "armv7l-netbsd", + "i686-netbsd", + "m68k-netbsd", + "mipsel-netbsd", + "powerpc-netbsd", + "riscv32-netbsd", + "riscv64-netbsd", + "x86_64-netbsd", + "aarch64_be-none", + "aarch64-none", + "arm-none", + "armv6l-none", + "avr-none", + "i686-none", + "microblaze-none", + "microblazeel-none", + "mips-none", + "mips64-none", + "msp430-none", + "or1k-none", + "m68k-none", + "powerpc-none", + "powerpcle-none", + "riscv32-none", + "riscv64-none", + "rx-none", + "s390-none", + "s390x-none", + "vc4-none", + "x86_64-none", + "i686-openbsd", + "x86_64-openbsd", + "x86_64-redox", + "wasm64-wasi", + "wasm32-wasi", + "x86_64-windows", + "i686-windows" + ] + }, + "runtime": true + }, + { + "package": { + "name": "coreutils-full", + "revision": "7445ccd775d8b892fc56448d17345443a05f7fb4", + "version": "9.5", + "description": "The GNU Core Utilities", + "homepage": "https://www.gnu.org/software/coreutils/", + "free": true, + "spdx_id": "GPL-3.0-or-later", + "epoch_seconds": 1716993062, + "platforms": [ + "i686-cygwin", + "x86_64-cygwin", + "x86_64-darwin", + "i686-darwin", + "aarch64-darwin", + "armv7a-darwin", + "i686-freebsd", + "x86_64-freebsd", + "x86_64-solaris", + "aarch64-linux", + "armv5tel-linux", + "armv6l-linux", + "armv7a-linux", + "armv7l-linux", + "i686-linux", + "loongarch64-linux", + "m68k-linux", + "microblaze-linux", + "microblazeel-linux", + "mips-linux", + "mips64-linux", + "mips64el-linux", + "mipsel-linux", + "powerpc64-linux", + "powerpc64le-linux", + "riscv32-linux", + "riscv64-linux", + "s390-linux", + "s390x-linux", + "x86_64-linux", + "aarch64-netbsd", + "armv6l-netbsd", + "armv7a-netbsd", + "armv7l-netbsd", + "i686-netbsd", + "m68k-netbsd", + "mipsel-netbsd", + "powerpc-netbsd", + "riscv32-netbsd", + "riscv64-netbsd", + "x86_64-netbsd", + "i686-openbsd", + "x86_64-openbsd", + "x86_64-redox", + "i686-cygwin", + "x86_64-cygwin", + "x86_64-windows", + "i686-windows" + ], + "attr_name": "coreutils-full" + }, + "runtime": false + }, + { + "package": { + "name": "ruby", + "revision": "2af19cfb6aa40768c4bbefd801a136270e099191", + "version": "3.3.4", + "description": "Object-oriented language for quick and easy programming", + "homepage": "https://www.ruby-lang.org/", + "free": true, + "spdx_id": "Ruby", + "epoch_seconds": 1727301923, + "platforms": [ + "i686-cygwin", + "x86_64-cygwin", + "x86_64-darwin", + "i686-darwin", + "aarch64-darwin", + "armv7a-darwin", + "i686-freebsd", + "x86_64-freebsd", + "aarch64-genode", + "i686-genode", + "x86_64-genode", + "x86_64-solaris", + "javascript-ghcjs", + "aarch64-linux", + "armv5tel-linux", + "armv6l-linux", + "armv7a-linux", + "armv7l-linux", + "i686-linux", + "loongarch64-linux", + "m68k-linux", + "microblaze-linux", + "microblazeel-linux", + "mips-linux", + "mips64-linux", + "mips64el-linux", + "mipsel-linux", + "powerpc64-linux", + "powerpc64le-linux", + "riscv32-linux", + "riscv64-linux", + "s390-linux", + "s390x-linux", + "x86_64-linux", + "mmix-mmixware", + "aarch64-netbsd", + "armv6l-netbsd", + "armv7a-netbsd", + "armv7l-netbsd", + "i686-netbsd", + "m68k-netbsd", + "mipsel-netbsd", + "powerpc-netbsd", + "riscv32-netbsd", + "riscv64-netbsd", + "x86_64-netbsd", + "aarch64_be-none", + "aarch64-none", + "arm-none", + "armv6l-none", + "avr-none", + "i686-none", + "microblaze-none", + "microblazeel-none", + "mips-none", + "mips64-none", + "msp430-none", + "or1k-none", + "m68k-none", + "powerpc-none", + "powerpcle-none", + "riscv32-none", + "riscv64-none", + "rx-none", + "s390-none", + "s390x-none", + "vc4-none", + "x86_64-none", + "i686-openbsd", + "x86_64-openbsd", + "x86_64-redox", + "wasm64-wasi", + "wasm32-wasi", + "x86_64-windows", + "i686-windows" + ], + "attr_name": "ruby" + }, + "runtime": false + } + ] +} \ No newline at end of file diff --git a/ruby-server-example/bsf/default.nix b/ruby-server-example/bsf/default.nix new file mode 100755 index 0000000..e69de29 diff --git a/ruby-server-example/bsf/flake.lock b/ruby-server-example/bsf/flake.lock new file mode 100755 index 0000000..559ec59 --- /dev/null +++ b/ruby-server-example/bsf/flake.lock @@ -0,0 +1,163 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nix2container": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1724996935, + "narHash": "sha256-njRK9vvZ1JJsP8oV2OgkBrpJhgQezI03S7gzskCcHos=", + "owner": "nlewo", + "repo": "nix2container", + "rev": "fa6bb0a1159f55d071ba99331355955ae30b3401", + "type": "github" + }, + "original": { + "owner": "nlewo", + "repo": "nix2container", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1712920918, + "narHash": "sha256-1yxFvUcJfUphK9V91KufIQom7gCsztza0H4Rz2VCWUU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "92323443a56f4e9fc4e4b712e3119f66d0969297", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7": { + "locked": { + "lastModified": 1702939464, + "narHash": "sha256-mZqoH5fXxh8UxjUKpqi09UBXEPpz4w+2Ef0AVTLuF+g=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "1ebb7d7bba2953a4223956cfb5f068b0095f84a7", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "rev": "1ebb7d7bba2953a4223956cfb5f068b0095f84a7", + "type": "github" + } + }, + "nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191": { + "locked": { + "lastModified": 1727301923, + "narHash": "sha256-hZIzX7Qx9k1s+j5dDp20jgxJ0jTxX403/P6o1yJxi1k=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "2af19cfb6aa40768c4bbefd801a136270e099191", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "rev": "2af19cfb6aa40768c4bbefd801a136270e099191", + "type": "github" + } + }, + "nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4": { + "locked": { + "lastModified": 1716993062, + "narHash": "sha256-Q7+8FPD2V8OoGAe0d5LoE8HZ5KYJMyb80klPyrZj2hw=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "7445ccd775d8b892fc56448d17345443a05f7fb4", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "rev": "7445ccd775d8b892fc56448d17345443a05f7fb4", + "type": "github" + } + }, + "nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14": { + "locked": { + "lastModified": 1702838883, + "narHash": "sha256-R01Ga1NsBsZsNkLrnhuw+6gJIKtz9B15L9vybTlLpko=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "ac5c1886fd9fe49748d7ab80accc4c847481df14", + "type": "github" + }, + "original": { + "owner": "nixos", + "repo": "nixpkgs", + "rev": "ac5c1886fd9fe49748d7ab80accc4c847481df14", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1728492678, + "narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nix2container": "nix2container", + "nixpkgs": "nixpkgs_2", + "nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7": "nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7", + "nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191": "nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191", + "nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4": "nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4", + "nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14": "nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/ruby-server-example/bsf/flake.nix b/ruby-server-example/bsf/flake.nix new file mode 100755 index 0000000..d1def59 --- /dev/null +++ b/ruby-server-example/bsf/flake.nix @@ -0,0 +1,163 @@ + +{ + description = ""; + + inputs = { + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14.url = "github:nixos/nixpkgs/ac5c1886fd9fe49748d7ab80accc4c847481df14"; + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4.url = "github:nixos/nixpkgs/7445ccd775d8b892fc56448d17345443a05f7fb4"; + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7.url = "github:nixos/nixpkgs/1ebb7d7bba2953a4223956cfb5f068b0095f84a7"; + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191.url = "github:nixos/nixpkgs/2af19cfb6aa40768c4bbefd801a136270e099191"; + + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + + + + + + + + + + nix2container.url = "github:nlewo/nix2container"; + }; + + outputs = inputs@{ self, nixpkgs, + + + + + nix2container , + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14, + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4, + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7, + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191, + }: let + supportedSystems = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin" "aarch64-linux" ]; + + + forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { + inherit system; + nix2containerPkgs = nix2container.packages.${system}; + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs = import nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14 { inherit system; }; + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs = import nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4 { inherit system; }; + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs = import nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7 { inherit system; }; + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs = import nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191 { inherit system; }; + + + pkgs = import nixpkgs { inherit system; }; + + + }); + in { + + devShells = forEachSupportedSystem ({ pkgs, + + + + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs, + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs, + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs, + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs, + ... }: { + devShell = pkgs.mkShell { + # The Nix packages provided in the environment + packages = [ + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs.bash + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs.bundler + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs.coreutils-full + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs.ruby + + ]; + }; + }); + + runtimeEnvs = forEachSupportedSystem ({ pkgs, + + + + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs, nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs, nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs, nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs, ... }: { + runtime = pkgs.buildEnv { + name = "runtimeenv"; + paths = [ + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs.cacert + + ]; + }; + }); + + devEnvs = forEachSupportedSystem ({ pkgs, + + + + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs, nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs, nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs, nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs, ... }: { + development = pkgs.buildEnv { + name = "devenv"; + paths = [ + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs.bash + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs.bundler + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs.coreutils-full + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs.ruby + + ]; + }; + }); + + + + + +ociImage_pkgs = forEachSupportedSystem ({ pkgs, nix2containerPkgs, system , nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs, nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs, nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs, nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs, ... }: { + + + + ociImage_pkgs_base = nix2containerPkgs.nix2container.buildImage { + name = "ttl.sh/ruby-server"; + config = { + cmd = [ ]; + entrypoint = [ ]; + env = [ + + ]; + ExposedPorts = { + + }; + }; + maxLayers = 100; + layers = [ + (nix2containerPkgs.nix2container.buildLayer { + copyToRoot = [ + nixpkgs-ac5c1886fd9fe49748d7ab80accc4c847481df14-pkgs.cacert + ]; + }) (nix2containerPkgs.nix2container.buildLayer { + copyToRoot = [ + nixpkgs-1ebb7d7bba2953a4223956cfb5f068b0095f84a7-pkgs.bash + ]; + }) (nix2containerPkgs.nix2container.buildLayer { + copyToRoot = [ + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs.bundler + ]; + }) (nix2containerPkgs.nix2container.buildLayer { + copyToRoot = [ + nixpkgs-7445ccd775d8b892fc56448d17345443a05f7fb4-pkgs.coreutils-full + ]; + }) (nix2containerPkgs.nix2container.buildLayer { + copyToRoot = [ + nixpkgs-2af19cfb6aa40768c4bbefd801a136270e099191-pkgs.ruby + ]; + }) + + ]; + }; + + + + + ociImage_pkgs_base-as-dir = pkgs.runCommand "image-as-dir" { } "${inputs.self.ociImage_pkgs.${system}.ociImage_pkgs_base.copyTo}/bin/copy-to dir:$out"; + + }); + + + + + }; +} diff --git a/ruby-server-example/main.rb b/ruby-server-example/main.rb new file mode 100644 index 0000000..75a8975 --- /dev/null +++ b/ruby-server-example/main.rb @@ -0,0 +1,43 @@ +require "socket" +require "json" +require "active_support/all" + +port = ENV.fetch("PORT", 9898).to_i +server = TCPServer.new port +puts "Listening on port #{port}..." + +def handle_request(request) + method, path, _ = request.split(" ") + + case [method, path] + when ["GET", "/ping"] + { code: 200, body: "Pong!" } + else + { code: 404, body: "Not Found" } + end +end + +def send_response(client, response) + status_line = case response[:code] + when 200 then "HTTP/1.1 200 OK\r\n" + when 404 then "HTTP/1.1 404 Not Found\r\n" + when 400 then "HTTP/1.1 400 Bad Request\r\n" + else "HTTP/1.1 500 Internal Server Error\r\n" + end + + headers = response[:headers] || { "Content-Type" => "text/html" } + headers_section = headers.map { |k, v| "#{k}: #{v}" }.join("\r\n") + + client.write(status_line) + client.write(headers_section + "\r\n\r\n") + client.write(response[:body]) if response[:body] +end + +loop do + Thread.start(server.accept) do |client| + request = client.readpartial(2048) + response = handle_request(request) + send_response(client, response) + client.close + end +end