dcgi

package module
v0.0.0-...-f81c55c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2016 License: Apache-2.0 Imports: 16 Imported by: 0

README

go-dcgi

DCGI is a technique for serving web pages dynamically with Docker. As you may know, World Wide Web servers can only serve static files off disk. A DCGI server allows you to execute code in real-time, so the Web page can contain dynamic information.

For each HTTP request that a DGCI server receives, a Docker container is spun up to serve the HTTP request. Inside the Docker container is a CGI executable which handles the request. That executable could do anything – and could be written in any language or framework.

Wow! No longer do we have to build Web sites which just serve static content. For example, you could "hook up" your Unix database to the World Wide Web so people all over the world could query it. Or, you could create HTML forms to allow people to transmit information into your database engine. The possibilities are limitless.

So, what's this library for? go-dcgi is a library for writing DGCI servers. It includes a Go HTTP handler, dcgi.Handler, which serves an HTTP request by running a Docker container.

Usage

Say you've got a really simple CGI script, script.pl:

print "Content-Type: text/html\n\n";
print "<h1>Hello World!</h1>\n";

And a Dockerfile to put it inside a container:

FROM perl
ADD script.pl /code/script.pl
ENTRYPOINT ["perl", "/code/script.pl"]

Build this into a container:

$ docker build -t bfirsh/example-dcgi-app .

You can serve this container over HTTP with go-dcgi:

package main

import (
	"net/http"
	dcgi "github.com/bfirsh/go-dcgi"
	"github.com/docker/engine-api/client"
)

func main() {
	cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.23", nil, nil)
	if err != nil {
		panic(err)
	}

	http.Handle("/", &dcgi.Handler{
		Image:      "bfirsh/example-dcgi-app",
		Client:     cli,
	})
	http.ListenAndServe(":80", nil)
}

Documentation

Overview

Package dcgi implements CGI (Common Gateway Interface), but running inside Docker containers.

Based off https://golang.org/pkg/net/http/cgi/ Copyright 2011 The Go Authors. All rights reserved. https://github.com/golang/go/blob/master/LICENSE

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	Client     *client.Client // Docker client to use
	Image      string         // Docker image to run
	HostConfig *container.HostConfig
	Root       string // root URI prefix of handler or empty for "/"

	Env        []string    // extra environment variables to set, if any, as "key=value"
	InheritEnv []string    // environment variables to inherit from host, as "key"
	Logger     *log.Logger // optional log for errors or nil to use log.Print
	Args       []string    // optional arguments to pass to child process

	// PathLocationHandler specifies the root http Handler that
	// should handle internal redirects when the CGI process
	// returns a Location header value starting with a "/", as
	// specified in RFC 3875 § 6.3.2. This will likely be
	// http.DefaultServeMux.
	//
	// If nil, a CGI response with a local URI path is instead sent
	// back to the client and not redirected internally.
	PathLocationHandler http.Handler
}

Handler runs an executable in a subprocess with a CGI environment.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL