Skip to content
xyzzyappsPublic

About

CGI script runner in golang

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

easycgi

Zero-config multi-language CGI server with embedded static files, production/debug errors, SQLite access logging (including script execution time), and a separate analytics dashboard.

Drop scripts in a folder. Run one binary. No YAML, no Apache handler maps.

Adapted from mback2k/simple-cgi-server (GPLv3).

Doc Purpose
TUTORIAL.md Step-by-step from install to first app + dashboard
SPEC.md Software requirements specification (SRS)
README.md (this file) Operator / developer reference

Features

  • Zero config — extension → interpreter built in; missing tools skipped
  • Longest-match routing — /foo/bar/baz → foo/bar.* or foo.* with PATH_INFO=/bar/baz
    (Python, PHP, shell, binaries/.exe, templates)
  • Static files — same document root; scripts always win on the same route
  • Templates — Go html/template built-in; Mustache/Twig/Blade/ERB/EJS/… when CLI exists
  • Fail-fast conflicts — e.g. index.py + index.php refuse to start
  • Production / debug errors — hide or show detail; custom 404.html / 500.html
  • Timeouts & body limits — default 30s / 10 MiB
  • Security headers + optional CORS; symlink-safe path containment
  • SQLite access log (duration_ms + script_ms) + easycgi-dashboard
  • init, doctor, graceful shutdown, reverse-proxy headers

Install

Requires Go 1.22+ (module may select a newer toolchain for SQLite).

git clone <this-repo> easycgi && cd easycgi
go build -o easycgi .
go build -o easycgi-dashboard ./cmd/easycgi-dashboard
go install .
go install ./cmd/easycgi-dashboard

Quick start

easycgi init ./mysite
easycgi doctor ./mysite
easycgi -debug -sqlite=off ./mysite
# http://localhost:8080/

# production-ish + analytics
easycgi -addr 127.0.0.1:8080 -trust-proxy -sqlite easycgi.db ./mysite
easycgi-dashboard -db easycgi.db -addr 127.0.0.1:9090
# http://127.0.0.1:9090/

Demos:

# small set
easycgi -debug -sqlite=off ./examples

# full showcase — plain easycgi (auto-finds MSYS perl/bash)
.\demo\build.ps1                 # once: Go/Rust/C → demo\bin\
.\easycgi.exe .\demo             # http://localhost:8080/

Binaries

Binary Role
easycgi CGI + embed + static + logging
easycgi-dashboard Analytics UI + JSON API over SQLite

CLI (easycgi)

easycgi [flags] [document-root]
easycgi init [dir]
easycgi doctor [flags] [document-root]
Flag Environment Default Description
-addr EASYCGI_ADDR :8080 Listen address (prefer 127.0.0.1:8080 behind a proxy)
-root EASYCGI_ROOT . Document root (positional overrides)
-debug EASYCGI_DEBUG off Full errors, reindex every request, /_easycgi/*
-timeout EASYCGI_TIMEOUT 30s Per-request process timeout (0 = none)
-max-body EASYCGI_MAX_BODY 10485760 Max body bytes (0 = unlimited)
-trust-proxy EASYCGI_TRUST_PROXY off Trust X-Forwarded-For / X-Real-IP / X-Forwarded-Proto
-access-log EASYCGI_ACCESS_LOG on Stderr access lines
-sqlite EASYCGI_SQLITE easycgi.db SQLite path; off / none / - disables
-quiet EASYCGI_QUIET off Less startup noise
-security-headers EASYCGI_SECURITY_HEADERS on nosniff, frame-deny, CSP, …
-cors EASYCGI_CORS off e.g. * or https://app.example
-check Index routes, print, exit

Disable SQLite: -sqlite=off (avoid empty "" on PowerShell).

Graceful stop: Ctrl+C / SIGTERM flushes SQLite.

Access log line (CGI):

127.0.0.1 GET /hello/x 200 12.3ms script=11.8ms cgi:hello

CLI (easycgi-dashboard)

Flag Environment Default
-db EASYCGI_SQLITE easycgi.db
-addr EASYCGI_DASHBOARD_ADDR :9090 (prefer 127.0.0.1:9090)

UI: time window, kind / status / path filters, live SSE tail, total + script latency.

API:
/api/summary?window=24h&kind=cgi:&path=/api&statusClass=5xx
/api/top/paths, /ips, /status, /kinds, /methods
/api/series/hourly, /api/slow, /api/recent, /api/health
/api/tail (SSE)


Routing

Order:

  1. Debug routes (if -debug): /_easycgi/health, /_easycgi/routes
  2. Longest script stem → remainder is PATH_INFO
  3. Static file / index.html
  4. Error page
URL Files Result
/foo/bar/baz foo/bar.py run + PATH_INFO=/baz
/foo/bar/baz foo.py / foo.sh / foo.exe run + PATH_INFO=/bar/baz
/foo foo.py and foo/index.py foo.py (exact beats index)
/docs docs/index.html only static index
/page page.py + page.html CGI
/page.html same static
/page.py registered script not served as source

Ambiguous stems (index.py + index.php, x.py + x.tmpl) → startup fails.


Languages

Interpreters discovered on PATH at prepare. Missing optional CLIs are skipped.

CGI

Extensions Candidates
.py .pyw .pycgi python3, python
.php .phtml .php3–.php8 php-cgi, php
.pl .pm perl
.rb ruby
.js .mjs .cjs node
.ts … ts-node, tsx
.sh .bash .ksh .zsh .command bash, sh
.lua lua, luajit
.cgi / bare +x (Unix) / .exe (Windows) execute file

Embed / templates

Engine Extensions Runtime
Go html/template .tmpl .gohtml .gotmpl Built into easycgi
Mustache .mustache .ms mustache / mustache-cli
Twig .twig .html.twig … twig / twig-cli / twiggy
Blade .blade.php .blade blade / blade-cli / bladeone
EJS .ejs ejs-cli, ejsc, ejs
ERB .erb .rhtml erb-cli, erb
Mako, ePerl, Jinja2, Handlebars, Nunjucks, Pug, Haml, Slim, Liquid, TT various first CLI on PATH

Compound extensions (.blade.php, .html.twig) are matched before plain .php / .twig.

Go template data

<!-- greet.tmpl → /greet?name=Ada -->
<p>Hello {{.name}}</p>
<p>{{.PATH_INFO}} {{.QUERY_STRING}} {{.Method}}</p>

Also: .Params, .Query, .ScriptName, …

CGI scripts

print("Content-Type: text/plain")
print()
print("hello")

PHP

Use header(), not echoed CGI headers, under php-cgi.

Env extras

Variable Meaning
EASYCGI_DATA JSON request metadata (when small)
EASYCGI_DATA_FILE Path to same JSON
REDIRECT_STATUS=200 PHP

Working directory = script’s directory.


Static files

  • After CGI miss
  • Dir index: index.html, index.htm
  • No directory listings
  • Dotfiles / .git / .env blocked
  • Script sources not downloadable
  • Symlinks that escape document root rejected

Errors

Mode Client sees
Production Generic message
Debug Full detail + badge; X-Easycgi-Mode: debug

Overrides: 404.html, 500.html, 500.debug.html, errors/…
JSON when Accept: application/json (without text/html).


Logging & dashboard

Sink Content
Stderr IP method URI status totalMs [script=…] kind
SQLite ts, method, path, query, status, duration_ms, script_ms, bytes, client_ip, user_agent, kind, host, referer

script_ms = child process time for CGI/embed (0 for static/miss).

Dashboard filters: window, kind (cgi: / static / miss), status class or exact code, path prefix. Live tail via SSE.


Debug mode

easycgi -debug -sqlite=off ./app
  • Verbose errors
  • Reindex every request
  • GET /_easycgi/health, GET /_easycgi/routes

Do not expose -debug on a public bind.


Security (short)

  • Scripts = trusted code (RCE as the server user).
  • Prefer -addr 127.0.0.1:8080 + reverse proxy TLS.
  • -trust-proxy only when the peer is a trusted proxy.
  • Dashboard has no auth — bind loopback or put behind ACL.
  • See SPEC.md §6A Threat model.

Deploy samples: deploy/ (systemd + Windows NSSM notes).


Project layout

easycgi/
  main.go init.go doctor.go
  cgiserver/          # HTTP, routing, exec, static, templates, errors
  accesslog/          # SQLite writer + analytics
  cmd/easycgi-dashboard/
  examples/
  deploy/
  README.md TUTORIAL.md SPEC.md

Testing & benchmarks

go test ./...
go test ./cgiserver/ -count=1
go test ./accesslog/ -count=1
go test ./cgiserver/ -bench=. -benchmem
go test ./accesslog/ -bench=. -benchmem
easycgi doctor ./examples
easycgi -check -sqlite=off ./examples

Coverage includes routing edge cases, conflicts (including compound exts), static/traversal/dotfiles, CGI PATH_INFO/POST, timeouts, proxy headers, prod/debug errors, Go templates, SQLite script_ms, dashboard filters, security headers/CORS.


License

GPLv3 — see LICENSE.

About

CGI script runner in golang

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages