Skip to content

tiny static file server, with dead simple templates (just $VARIABLES). single binary, no dependencies. linux, osx, windows. #golang

License

Notifications You must be signed in to change notification settings

sgreben/http-subst-server

Repository files navigation

http-subst-server

http-subst-server is a minimal HTTP server that serves files and renders templates with $VARIABLES in them.

A mash-up of http-file-server and subst.

Contents

Get it

Using go get

go get -u github.com/sgreben/http-subst-server

Pre-built binary

Download a binary from the releases page or from the shell:

# Linux
curl -L https://github.com/sgreben/http-subst-server/releases/download/1.2.9/http-subst-server_1.2.9_linux_x86_64.tar.gz | tar xz

# OS X
curl -L https://github.com/sgreben/http-subst-server/releases/download/1.2.9/http-subst-server_1.2.9_osx_x86_64.tar.gz | tar xz

# Windows
curl -LO https://github.com/sgreben/http-subst-server/releases/download/1.2.9/http-subst-server_1.2.9_windows_x86_64.zip
unzip http-subst-server_1.2.9_windows_x86_64.zip

Examples

The CLI has the following general syntax:

http-subst-server [OPTIONS] [[ROUTE=]PATH...]

Template variables can be set via command-line options, environment variables, files, and standard input.

Variables from command-line options

$ cat example/index.html
$GREETING $SUBJECT
$ http-subst-server -v GREETING=hello -v SUBJECT=world /=example
2018/12/16 09:49:09 serving "./example" on "/"
2018/12/16 09:49:09 http-subst-server listening on ":8080"
$ curl localhost:8080
hello world

Variables from the environment

Environment variables with a name prefixed with SUBST_VAR_ (or a custom prefix set via -var-prefix) can be used in the templates. The prefix is stripped from the variable name, so e.g. $SUBST_VAR_hello is $hello in the templates.

$ cat example/index.html
$GREETING $SUBJECT
$ export SUBST_VAR_GREETING=hello
$ export SUBST_VAR_SUBJECT=world
$ http-subst-server /=example
2018/12/16 09:49:09 serving "./example" on "/"
2018/12/16 09:49:09 http-subst-server listening on ":8080"
$ curl localhost:8080
hello world

Environment variables can also be injected by using the shorthand -v NAME (short for -variable NAME) instead of -v NAME=VALUE. When =VALUE is left off, http-subst-server sets NAME to the value of $NAME in the current environment:

$ export GREETING=hello
$ export SUBJECT=world
$ http-subst-server -v GREETING -v SUBJECT /=example
2018/12/16 09:49:09 serving "./example" on "/"
2018/12/16 09:49:09 http-subst-server listening on ":8080"

Variables and routes from the environment

The values of environment variables with names prefixed SUBST_ROUTE_ (or a custom prefix set via -route-prefix) are used as routes. Apart from the name prefix, only the value of the environment variable is relevant.

$ cat example/index.html
$GREETING $SUBJECT
$ export SUBST_VAR_GREETING=hello
$ export SUBST_VAR_SUBJECT=world
$ export SUBST_ROUTE_1=/=example
$ export SUBST_ROUTE_2=/abc=/tmp
$ http-subst-server
2018/12/16 09:49:09 serving "./example" on "/"
2018/12/16 09:49:09 serving "/tmp" on "/abc/"
2018/12/16 09:49:09 http-subst-server listening on ":8080"
$ curl localhost:8080
hello world

Variables from files

You may specify files containing variable definitions NAME[=VALUE] (same syntax as -variable/-v, one per line) using -variable-file/-f. Changes to the definitions in these files will be picked up without having to restart the server.

$ cat example/index.html
$GREETING $SUBJECT
$ http-subst-server -f variables.env /=example
2018/12/16 09:49:09 serving "./example" on "/"
2018/12/16 09:49:09 http-subst-server listening on ":8080"
$ curl localhost:8080
$GREETING $SUBJECT
$ echo GREETING=hello > variables.env
$ cat variables.env
GREETING=hello
$ curl localhost:8080
hello $SUBJECT
$ echo SUBJECT=world >> variables.env
$ cat variables.env
GREETING=hello
SUBJECT=world
$ curl localhost:8080
hello world

Variables from standard input

If the -variables-from-stdin/-i flag is given, variable definitions NAME[=VALUE] (same syntax as -variable/-v, one per line) are continuously streamed from standard input (until it is closed). New definitions are available to templates immediately.

$ cat example/index.html
$GREETING $SUBJECT
$ http-subst-server -i /=example
2018/12/16 09:49:09 serving "./example" on "/"
2018/12/16 09:49:09 http-subst-server listening on ":8080"
018/12/16 21:28:17 reading variable definitions NAME[=VALUE] from stdin
GREETING=foo
SUBJECT=bar
$ curl localhost:8080
foo bar

Usage

http-subst-server [OPTIONS] [[ROUTE=]PATH...]
Usage of http-subst-server:
  -a string
    	(alias for -addr) (default ":8080")
  -addr string
    	address to listen on (environment variable "ADDR") (default ":8080")
  -escape string
    	set the escape string - a '$' preceded by this string is not treated as a variable (default "\\")
  -f value
    	(alias for -variable-file)
  -i	(alias for -variables-from-stdin)
  -o string
    	(alias for -output)
  -output string
    	write the variable mapping to this file whenever it changes
  -p int
    	(alias for -port)
  -port int
    	port to listen on (overrides -addr port) (environment variable "PORT")
  -q	(alias for -quiet)
  -quiet
    	disable all log output (environment variable "QUIET")
  -r value
    	(alias for -route)
  -route value
    	a route definition ROUTE=PATH (ROUTE defaults to basename of PATH if omitted)
  -route-prefix string
    	use values of environment variables with this prefix as routes (default "SUBST_ROUTE_")
  -template-suffix string
    	replace $variables in files with this suffix (default ".html")
  -undefined value
    	handling of undefined $variables, one of [ignore empty error] (default ignore) (default ignore)
  -v value
    	(alias for -variable)
  -var-prefix string
    	use environment variables with this prefix in templates (default "SUBST_VAR_")
  -variable value
    	a variable definition NAME[=VALUE] (if the value is omitted, the value of the environment variable with the given name is used)
  -variable-file value
    	a file consisting of lines with one variable definition NAME[=VALUE] per line
  -variable-file-reload duration
    	reload interval for variable files (default 1s)
  -variables-from-stdin
    	read lines with variable definitions NAME[=VALUE] from stdin

About

tiny static file server, with dead simple templates (just $VARIABLES). single binary, no dependencies. linux, osx, windows. #golang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published