Skip to content

Commit

Permalink
feat: add api ping redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
cao7113 committed Sep 11, 2024
1 parent b10c1e6 commit 14aba5a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ FROM ${RUNNER_IMAGE} as runner
# apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
# && apt-get clean && rm -f /var/lib/apt/lists/*_*
RUN apt-get update -y && \
apt-get install -y locales curl iputils-ping dnsutils\
apt-get install -y locales net-tools curl iputils-ping dnsutils\
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# HelloApi
# HelloApi - Simple API based on bandit

[![CI](https://github.com/cao7113/hello-api-elixir/actions/workflows/ci.yml/badge.svg)](https://github.com/cao7113/hello-api-elixir/actions/workflows/ci.yml)
[![Release](https://github.com/cao7113/hello-api-elixir/actions/workflows/release.yml/badge.svg)](https://github.com/cao7113/hello-api-elixir/actions/workflows/release.yml)
Expand Down
1 change: 1 addition & 0 deletions Taskfile-fly-io.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tasks:
# create new app and fly.toml but not deploy
new-app: fly launch --no-deploy
vm: fly machine ls
# vm512: fly machine update e784079b449483 --vm-memory 512
ls: fly app ls
ping: fly app open /ping
# plain env and secrets
Expand Down
5 changes: 3 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ tasks:
# https://hub.docker.com/r/cao7113/hello-api-elixir
# https://github.com/cao7113/hello-api-elixir/pkgs/container/hello-api-elixir
dk-pull-ghcr: docker pull ghcr.io/cao7113/hello-api-elixir
dk-run: docker run --name hello-api-elixir --rm -it -p 4000:4000 ghcr.io/cao7113/hello-api-elixir bin/hello_api start_iex
dk-build: docker build -t hello-api-elixir .
dk-run: docker run --name hello-api-elixir --rm -it -p 4000:4000 hello-api-elixir bin/hello_api start_iex
# http://hello-api-elixir.orb.local:4000 or http://localhost:4000
dk-ping: curl http://hello-api-elixir.orb.local | jq
dk-sh: docker run --rm -it ghcr.io/cao7113/hello-api-elixir sh
dk-sh: docker run --rm -it hello-api-elixir sh
# cat /etc/os-release or cat /etc/debian_version

## Get images
Expand Down
26 changes: 13 additions & 13 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ primary_region = 'nrt'
[build]

[env]
PORT = '8080'
# https://fly.io/docs/elixir/getting-started/#important-ipv6-settings
# ECTO_IPV6 = 'true'
ERL_AFLAGS = '-proto_dist inet6_tcp'
PORT = '8080'
# https://fly.io/docs/elixir/getting-started/#important-ipv6-settings
# ECTO_IPV6 = 'true'
ERL_AFLAGS = '-proto_dist inet6_tcp'

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ['app']
internal_port = 8080
# force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '512mb'
cpu_kind = 'shared'
cpus = 1
memory = '256mb'
cpu_kind = 'shared'
cpus = 1
9 changes: 9 additions & 0 deletions lib/hello_api/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ defmodule HelloApi.Router do
send_resp(conn, 200, "Pong")
end

get "/api/ping" do
conn
|> put_resp_header("location", "/ping")
|> send_resp(
conn.status || 302,
"<html><body>You are being <a href=\"/ping\">redirected</a>.</body></html>"
)
end

get "/hello" do
send_resp(conn, 200, "World")
end
Expand Down

0 comments on commit 14aba5a

Please sign in to comment.