Sign inSign up

cyd01/gowebserver

By cyd01

•Updated about 18 hours ago

Image
0

10K+

cyd01/gowebserver repository overview

⁠gowebserver: a simple web server in go

⁠Usage

$ docker run --rm -it cyd01/gowebserver -h
Webserver  0.9
Usage: webserver [options]

Options:
  -alive                                    start an alive server
  -api          <path>                      start simple api server
  -auth         <user:pass>                 credentials (user:pass) for basic authentication
  -badge        <path>                      start badge server
  -cmd          <path1=cmd1[,...]>          add command handler list
  -dir          <.>                         start on specified directory
  -echo                                     start a HTTP echo server
  -echo-txt                                 start a HTTP echo server in text mode
  -gocmd        <path1=cmd1[,...]>          add go script handler list
  -header       <value>                     add a special header: X-Special-Header
  -host         <hostname>                  start on specified host or ip for certificate generation (default is "")
  -info                                     add informations pages
  -no-index                                 disable index page on directory
  -port         <80>                        listen on specified port
  -pid          <path>                      define pid file
  -qrcode       <path>                      start QRCode server
  -rp-file      <filename>                  add reverse proxies definition from file (json or yaml format)
  -stop         </url>                      add stop url (must start with /)
  -tmout        <value>                     timeout for external commands (default is 30)
  -timeout      <3>                         read timeout in seconds
  -tls                                      start HTTPS web server with key.pem key file and cert.pem certificate file
  -http-client  [<Method>] <URL> [<data>]   do simple http call
  -htpasswd     <user:pass>                 generate an Apache password
  -http-proxy                               start a HTTP proxy server
  -http-status                              start a HTTP status code server
  -randfile                                 start a HTTP random file server, add size query parameter to define length
  -version                                  print version

⁠Parameter descriptions

NameEnvironment variableTypeDefault valueDescription
-aliveGOWEB_ALIVEbooleanfalsestart alive service on /alive path
-authGOWEB_AUTHstring-add basic auth credentials, format: user:pass
-badgeGOWEB_BADGEstring-start a SVG badge generator server
-cmdGOWEB_CMDstring-add external dynamic script services (see below)
-dirGOWEB_DIRstring/web/wwwchange default root directory
-echoGOWEB_ECHObooleanfalsestart a HTTP echo server in JSON format
-echo-txtGOWEB_ECHOTXTbooleanfalsestart a HTTP echo server in text format
-headerGOWEB_HEADERstring-add X-Special-Header header
-hostGOWEB_HOSTstring-specify listening host
-no-indexGOWEB_NOINDEXbooleanfalsedisable directory index page
-portGOWEB_PORTinteger8080specify listening port
-pidGOWEB_PIDFILEstring-define pid file
-qrcodeGOWEB_QRCODEstring-start a QRCode generator server
-rp-file-string-add reverse proxy definition
-stopGOWEB_STOPstring-add stop server url
-timeoutGOWEB_TIMEOUTinteger3specify read timeout
-tmoutGOWEB_TMOUTinteger30timeout for external dynamic scripts
-tlsGOWEB_TLSbooleanfalsestart TLS web server (see below)
-htpasswd-string-generate an Apache style password
-http-proxyGOWEB_PROXYbooleanfalsestart a HTTP proxy server
-htt-statusGOWEB_HTTPbooleanfalsestart a HTTP status code web server
-randfileGOWEB_RANDFILEbooleanfalsestart a random file web server

⁠Import directory

The default root directory inside container is /web/www, and the default port is 8080.
To mount stuff inside the container use the mount docker option: -v.
And to redirect the outside port to the inside one use -p docker option.
Example: assuming you documents are located in wwwroot sub-directory, and you finally want to expose your service on port 80 the right syntax will be:

docker run --rm -v $(pwd)/wwwroot:/web/www -p 80:8080 cyd01/gowebserver

⁠TLS web server

With -tls parameter the web server starts in secure mode.
Server key must be provided in key.pem file (PEM format) and certificate is provided in cert.pem file (PEM format).
If no file is provided, the webserver will generate then, signed by an internal certificate authority.

⁠Builtin HTTP web servers

This webserver provides some pre-embedded HTTP web server

⁠Alive server -alive

The Alive server is just a simple Web server except that /alive path will answer an simple alive message.

$ curl http://localhost:8080/alive
alive
⁠Echo server -echo

The Echo Server answers to HTTP request with a JSON object containing all the request items:

$ docker run --rm -p 8080:8080 cyd01/gowebserver -echo
2020/11/23 14:58:51.470689 Webserver Version 0.9 Build 20201122-183529
2020/11/23 14:58:51.470840 Initiating echo server
2020/11/23 14:58:51.470844 Adding echo handler: < / >
2020/11/23 14:58:51.470848 Starting Web server on < :8080 > on directory < /web/www >
2020/11/23 14:59:32.723306 GET 172.17.0.1:39604 /test []
...
$ curl http://localhost:8080/test
{"URL":{"forceQuery":false,"fragment":"","host":"","opaque":"","path":"/test","query":"","rawPath":"","scheme":""},"close":false,"contentLength":0,"headers":{"Accept":"*/*","User-Agent":"curl/7.29.0"},"host":"localhost:8080","method":"GET","proto":"HTTP/1.1","protoMajor":1,"protoMinor ":1,"remoteAddr":"172.17.0.1:39604","requestURI":"/test","server":{"build":"20201122-183529","executable":"/usr/local/bin/webserver","hostname":"ae6d7601b36c","path":"/web/www","pid":69,"ppid":68,"time":"2020-11-23 14:59:32.723225796 +0100 CET m=+41.254488184","version":"0.9"},"url":"/test"}
⁠Text echo server -echo-txt

This web server is exactly the same as the previous one excepting that the message is text and not JSON.

⁠HTTP status codes server -http-status

The HTTPS status code server expose a server that simulate HTTP status code behind /http path (see example below):

$ docker run --rm -p 8080:8080 cyd01/gowebserver -http-status
2020/11/23 15:08:57.843317 Webserver Version 0.9 Build 20201122-183529
2020/11/23 15:08:57.843455 Initiating web server
2020/11/23 15:08:57.843458 Adding info handler: < /_info >
2020/11/23 15:08:57.843461 Adding HTTP status code handler: < /http >
2020/11/23 15:08:57.843475 Adding favicon handler: < /favicon.ico >
2020/11/23 15:08:57.843478 Adding file webserver handler: < / >
2020/11/23 15:08:57.843483 Starting Web server on < :8080 > on directory < /web/www >
2020/11/23 15:15:12.450077 GET 172.17.0.1:39660 /http/501 []
$ curl http://localhost:8080/http/501
501 - Not Implemented
⁠Random file server -randfile

The random file server, serves random file containing alphabetic and numeric characters. The size of the file can be defined with the size query parameter.

$ docker run --rm -p 8080:8080 cyd01/gowebserver -randfile
2020/11/23 15:18:56.169619 Webserver Version 0.9 Build 20201122-183529
2020/11/23 15:18:56.169751 Initiating random file server
2020/11/23 15:18:56.169766 Adding random file handler: < / >
2020/11/23 15:18:56.169770 Adding stop url: <  /stop  >
2020/11/23 15:18:56.169773 Starting Web server on < :8080 > on directory < /web/www >
2020/11/23 15:19:09.759286 GET 172.17.0.1:39680 / [64]
$ curl http://localhost:8080/?size=64
M4kRQCu1PGc0FPu1KBzl3Ej3OL4pbVWL3x2wUSikjmiRDl2EOzQNc8DtDLHNBHQb

⁠Authentication

It is possible to add one basic authentication with -auth parameter.
Here an example with user:pass credential.

$ docker run --rm -p 8080:8080 cyd01/gowebserver -auth user:pass
2020/11/23 15:24:07.746404 Webserver Version 0.9 Build 20201122-183529
2020/11/23 15:24:07.746535 Initiating web server
2020/11/23 15:24:07.746538 Adding info handler: < /_info >
2020/11/23 15:24:07.746552 Adding favicon handler: < /favicon.ico >
2020/11/23 15:24:07.746554 Adding file webserver handler: < / >
2020/11/23 15:24:07.746557 Starting Web server on < :8080 > on directory < /web/www >
2020/11/23 15:24:26.474015 GET 172.17.0.1:39720 /test [?, ]
2020/11/23 15:24:35.509895 GET 172.17.0.1:39724 /test []

Without credential:

$ curl http://localhost:8080/test
Not authorized

With credential:

$ curl -u 'user:pass' http://localhost:8080/test
It works!

⁠Virtual hosts

Web root directory is searched recursively in

⁠External dynamic services

The web server provides a light dynamic pages engine, based on external bash scripts. With -cmd parameters it possible to define a list of corresponding paths and scripts. Scripts must be located on running directory: /web/bin.
Example:
-cmd start=start.sh,stop=stop.sh will start a web server where /start and /stop paths will execute respectively /web/bin/start.shand/web/bin/stop.sh` bash scripts.

Another example with this hello.sh scripts:

#!/bin/bash
# Send headers
echo "Content-type: text/plain"
echo
# Send body
echo "Hello world!"

Start the web server mapping hello.sh script to /test path:

$ docker run --rm -p 8080:8080 -v $(pwd)/hello.sh:/web/bin/test.sh cyd01/gowebserver -cmd "/test=/bin/sh -c /web/bin/test.sh"
2020/11/23 15:40:10.303563 Webserver Version 0.9 Build 20201122-183529
2020/11/23 15:40:10.303702 Initiating web server
2020/11/23 15:40:10.303704 Adding info handler: < /_info >
2020/11/23 15:40:10.303717 Adding favicon handler: < /favicon.ico >
2020/11/23 15:40:10.303759 Adding command handler: < /test >
2020/11/23 15:40:10.303768 Adding file webserver handler: < / >
2020/11/23 15:40:10.303771 Starting Web server on < :8080 > on directory < /web/www >
2020/11/23 15:40:22.467251 GET 172.17.0.1:39756 /test [Script(/scripts/test.sh)]

Call to /test handler:

$ curl http://localhost:8080/test
Hello world!

⁠External Go services

External Go script can be defined with -gocmd option.

Example:

$ ./webserver -gocmd "extern=ext.go" -tmout 30

In this example the Go script ext.go will be accessed on path /extern.

Here is the pattern for external Go script:

package extern

import (
  "net/http"
  "time"
)

func Main(w http.ResponseWriter,r *http.Request) {
    w.Write([]byte("We are in a script"))
}

Package name must be extern, and the entrypoint function must be Main.

⁠Startup script

If /startup.sh is present, it will be run just before the web server is started.

$ docker run --rm -it -v $(pwd)/start.sh:/startup.sh cyd01/gowebserver
Running startup script...
2020/11/19 23:32:26.993742 Webserver Version 0.9 Build 20201119-224246
2020/11/19 23:32:26.999077 Initiating web server
2020/11/19 23:32:26.999087 Adding info handler: < /_info >
2020/11/19 23:32:26.999105 Adding favicon handler: < /favicon.ico >
2020/11/19 23:32:26.999108 Adding file webserver handler: < / >
2020/11/19 23:32:26.999115 Starting Web server on < :8080 > on directory < /web/www >

Tag summary

Content type

Image

Digest

sha256:16f8860f9…

Size

71.7 MB

Last updated

about 18 hours ago

docker pull cyd01/gowebserver