Skip to content

Caddy plugin that gives native support for Python WSGI apps.

License

Notifications You must be signed in to change notification settings

gnat/caddy-snake

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Caddy Snake 🐍

Caddy plugin that gives native support for Python WSGI apps.

It embeds the Python interpreter inside Caddy and serves requests directly without going through a reverse proxy or creating a new process.

Install

Go 1.21 and Python 3.12 or later is required, with development files to embed the interpreter.

To install Python3.12 in Ubuntu do:

apt-get install -y software-properties-common
add-apt-repository ppa:deadsnakes/ppa
apt-get install -y python3.12-dev

To install in MacOS do:

brew install [email protected]

Bundling with Caddy

Build this module with caddy at Caddy's official download site. Or:

CGO_ENABLED=1 xcaddy build --with github.com/mliezun/caddy-snake

Use Docker to build

Dockerfile:

FROM python:3.12

WORKDIR /root

RUN apt-get update -y &&\
    wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz &&\
    tar -xvf go1.21.6.linux-amd64.tar.gz &&\
    rm -rf go1.21.6.linux-amd64.tar.gz &&\
    mv go /usr/local &&\
    export GOROOT=/usr/local/go &&\
    export GOPATH=$HOME/go &&\
    export PATH=$GOPATH/bin:$GOROOT/bin:$PATH &&\
    go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest

RUN CGO_ENABLED=1 xcaddy build --with github.com/mliezun/caddy-snake

# Use caddy binary located in: /root/caddy

Example Caddyfile

{
	http_port 9080
	https_port 9443
	log {
		level error
	}
}
localhost:9080 {
    route {
        python "simple_app:main"
    }
}

The python rule is an HTTP handler that expects a wsgi app as an argument.

Examples

  • simple_app. WSGI App that returns the standard hello world message and a UUID.
  • example_flask. Flask application that also returns hello workd message and a UUID.

NOTE

At the moment there's no support for virtual environments. To use one is necessary to set the PYTHONPATH env variable when starting caddy. As follows:

PYTHONPATH="venv/lib/python3.12/site-packages/" caddy run --config Caddyfile

Make sure to use the right python version depending on your case ^.

Dev resources

LICENSE

MIT License.

About

Caddy plugin that gives native support for Python WSGI apps.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 67.1%
  • Go 32.9%