Skip to content

qrtz/livedev

Repository files navigation

livedev

livedev is a development proxy server for golang that allows live reloading.
It supports multiple server configuration.
It uses the request's header "Host" field to determine which server the request should be routed to.
If no match is found the request is routed the default server.

Compatible with: go version go1.0.2+

Features

  • Cross-platform
  • Unobstructive. No code change required
  • Simple json configuration file
  • Multiple server support
  • Automated build service.
  • Dependency change detection
  • Autorealod the page when assets (js, css, image, html...) change

Installation

go get -u github.com/qrtz/livedev

Configuration

livedev is controlled by a json configuration file:

  • port: (int, default:"80") proxy port
  • GOROOT: (string, optional)
  • GOPATH: (string, optional)
  • server: ([]Server) A list of Server object with the following options:
    • GOROOT: (string, optional) Server specific GOROOT for compiling with different go version
    • GOPATH: ([]string, optional) Server specific GOPATH.
    • host: (string) server hostname (must be unique)
    • port: (int, optional) server port
    • target: (string, optional) Build target. The file that contains the main function.
      if target is not in the GOPATH, livedev will attempt to add it by guessing the workspace from the filename.
      When target is ommited, the build step is skipped.
    • workingDir: (string, optional) workingDir specifies the working directory of the server executable. If workingDir is empty, it defaults to the parent directory of the executable.
    • env: (map, optional) A map of key value pairs to set as environment variables on the server.
    • resources: (optional) A list of resources such as template files. Any change to these files will cause the server to restart.
      • ignore: (string, optional) filename pattern to ignore.
      • paths: ([]string) A list of files or directories to monitor
    • assets: (optional) A list of assets such as css, javascript, image files. Any change to these files will cause a page to reload.
      • ignore: (string, optional) filename pattern to ignore.
      • paths: ([]string) A list of files or directories to monitor
    • bin: (string, optional) server executable file. When absent, it default to /tmp/livedev[hostname]
    • builder: ([]string, optional) To use a builder other than the go build tool. The first element is the command and the rest its arguments
    • startup: ([]string, optional) server startup argument list
    • default: (bool, optinal) Specifies the default server.
      Defaults to the first server in the list
    • startupTimeout: (int, default=5) Specifies the time (in seconds) limit to wait for the server to complete the startup operation.

In the server configuration block, properties can be referred on using "${PROPERTY}" or "$PROPERTY" variable substutitions
Along with the configuration properties, the process environment variables are also available.

Usage

$ livedev -c config.json

config.json

{
    "port":8080,
    "server":[
        {
            "host":"dev.service1.com",
            "port": 8081,
            "target":"/projects/src/serviceone/main.go",
            "workingDir": "/projects/src/serviceone"
            "resources":{
                "ignore":"static*",
                "paths":["${workingDir}/templates"]
             },
            "startup": ["-host", "$host", "-port", "${port}"]
            "bin":"/projects/bin/serviceone"
        },
        {
            "host":"dev.service2.com",
            "env": {
                "HOST": "${host}",
                "PORT": "${port}"
            },
            "target":"/projects/src/servicetwo/main.go",
            "workingDir": "/projects/src/servicetwo"
            "resources":{
                "ignore":"static*",
                "paths":["${workingDir}/templates"]
             },
            "bin":"/projects/bin/servicetwo"
        }
    ]
}
# host file
127.0.0.1 dev.service1.com dev.service2.com

dev.service1.com

URL: http://dev.service1.com:8080
The request is forwarded to http://dev.serviceone.com:8081
The server access "host and "port" from the command-line argument as specified in the "startup" property of the configuration

packgage main

import (
    "flag"
    "net"
    "net/http"
) 

func main(){
    host := flag.String("host", "localhost", "host name")
    port := flag.String("port", "8081", "port")

    flag.Parse()

    addr := net.JoinHostPort(*host, *port)
    http.ListenAndServe(addr, handler)
}

dev.service2.com

URL: http://dev.service2.com:8080
The request is forwarded to http://dev.service2.com:`port`. Where port is a randomly generated port.
The server gets access to "host and "port" from environment variables as specified in the "env" property of the configuration

packgage main

import (
    "net"
    "net/http"
    "os"
) 

func main(){
    host := os.Getenv("HOST")
    port := os.Getenv("PORT")
    addr := net.JoinHostPort(host, port)
    http.ListenAndServe(addr, handler)
}

Live Reload

Livedv uses a deceptively simple protocol to enable live reload.
It injects a small inline javascript into HTML pages at the end of the document right before the closing body tag.
The script opens a websocket connection and reloads the page once the connection is closed.
Live reload events are as follow.

  • Change to go files:
    Rebuild, Restart the server and close liveReload websocket connections
  • Change to files listed under "resources" in the configuration:
    Restart the server and close liveReload websocket connections
  • Change to files listed under "assets" in the configuration:
    Close liveReload websocket connections

Closing liveReload connections causes all connected HTML pages to reload.

About

Live reload for Go web development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages