DEV Community

Eduardo Issao Ito
Eduardo Issao Ito

Posted on • Edited on

5 1

Caddy as a simple "api gateway" with SSL

What is Caddy

Caddy is an open source web server with automatic HTTPS.
We will use it to create a simple "api gateway".

https://caddyserver.com/

Preparing sample services

Start sample services

docker container run -d -p 5000:5000 proclaim/crp-app1
docker container run -d -p 8800:8800 proclaim/crp-app2

Test services


curl http://localhost:5000/api/member
curl http://localhost:8800/api/food

Configuring local caddy

mkdir ~/api-gateway
cd ~/api-gateway

Obtain machine information

hostname
ziba-Aspire-A515-51G.adzubla.net

hostname -I
192.168.59.50 

Create the SSL certificate for this machine

openssl req -nodes -new -x509 -keyout server.key -out server.cert \
-subj "/C=AU/ST=Some-State/L=City/O=Company Name/OU=Department/CN=ziba-Aspire-A515-51G.adzubla.net"

Create Caddyfile configuration

ziba-Aspire-A515-51G.adzubla.net {
    tls /etc/server.cert /etc/server.key
    proxy /api/member  192.168.59.50:5000
    proxy /api/food    192.168.59.50:8800
}

Start caddy server

docker run -d \
  -p 443:2015 \
  -v $HOME/api-gateway/Caddyfile:/etc/Caddyfile \
  -v $HOME/api-gateway/server.key:/etc/server.key \
  -v $HOME/api-gateway/server.cert:/etc/server.cert \
  abiosoft/caddy:1.0.3-no-stats

Test https services through caddy

curl -k https://ziba-aspire-a515-51g.adzubla.net/api/member
curl -k https://ziba-aspire-a515-51g.adzubla.net/api/food

Reference: https://medium.com/bumps-from-a-little-front-end-programmer/caddy-reverse-proxy-tutorial-faa2ce22a9c6

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay