Giter VIP home page Giter VIP logo

livedev's Introduction

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.

livedev's People

Contributors

qrtz avatar sdorra avatar

Stargazers

Neemias avatar Brad Pillow avatar choirudin avatar Hasan Hakkaev avatar  avatar Mark Wolfe avatar Robin Gruenke avatar Jatin Jindal avatar Dennisn't avatar Zulhilmi Zainudin avatar Cyan Tarek avatar Robert Gabriel Jakabosky avatar Ignasius Wahyudi avatar Song Liu avatar Damian Fuentes avatar kukibar avatar Alexey Avdeev avatar Misha Khazov avatar copper lee avatar Kiswono Prayogo avatar Felix  avatar Martin Mlynek avatar Ewan Jones avatar jsdir avatar Zach Badgett avatar yudppp avatar  avatar Edison Liu avatar Harold Miao avatar Lubomir Anastasov avatar Gregor Noczinski avatar Eder Sosa avatar Matt Aitchison avatar Luc Heinrich avatar Dhawal Modi avatar  avatar  avatar Sean Huber avatar Takeshi Yamashita avatar  avatar Thach Le avatar Harshpreet Singh avatar Lauris BH avatar Chase Colman avatar Matt Ketmo avatar Hiroaki Nakamura avatar Lucas Bremgartner avatar Kulshekhar Kabra avatar Miki Oracle avatar  avatar Nils avatar William McGann avatar Sean Lindo avatar Wilk avatar Bjørn Erik Pedersen avatar finomxf avatar NoScripter avatar Francois avatar George Xie avatar Michael Reynolds avatar Stig Kleppe-Jørgensen avatar Alfred Hall avatar Jake Scott avatar Kyle Wolfe avatar Rex Chung avatar Tony Holdstock-Brown avatar Patrick Wyatt avatar Sergey Storchay avatar Viacheslav Biriukov avatar Steve Phillips avatar Dario Castañé avatar Roman Heinrich avatar Ingo Oeser avatar kyb avatar Bill avatar Oliver Eilhard avatar Greg Worley avatar  avatar Arne Hormann avatar Andrews Medina avatar

Watchers

Stig Kleppe-Jørgensen avatar  avatar finomxf avatar

livedev's Issues

Config file help please?

I would like to try livedev, but I am having issues getting it to compile my project.It runs and parses the livedev.json file but when I refresh the web page, I get this error:

BUILD ERROR: GetFileAttributesEx controllers.go: The system cannot find the file specified.

Here is my livedev.json file contents (I've tried without the GOROOT and GOPATH server params and also a relative path for the target parameter without succes:

{
    "port":9001,
    "server":[
        {
            "GOROOT":"C:/Go",
            "GOPATH":["C:/Users/Nuri/Dropbox (Personal)/go"],
            "host":"localhost",
            "port": 9000,
            "target":"C:/Users/Nuri/Dropbox (Personal)/go/src/github.com/nkev/web/app/main.go",
            "bin":"/src/github.com/nkev/web/app/web.exe",
            "default":true
        }
    ]
}

Here is my directory structure:

image

The command I use from the root "web" folder is:

livedev -c livedev.json

When it runs, I get:

Livedev 0.1.8
Host: localhost:9000
Proxy:localhost:9001

But when I refresh my browser (Chrome on Windows 8.1) I get the error:

BUILD ERROR: GetFileAttributesEx controllers.go: The system cannot find the file specified.

Can you see what I'm doing wrong?

Add configurable watcher delay

Thanks for this tool, it's very helpful.

But I faced the problem. In our project we use pug templates which compile to html. When I change some template, multiple html files start to rebuild. livedev starts the server restart when it get the first file change notification, but html files are still rebuilding and triggering new fs notifications. It causes multiple server restarts one after the other and takes a lot of time.
So it would be great to have a chance to set some delay between fs event and server restart.

Unclear workflow

Hey there,

I'm not entirely sure how to use this utility. I have two servers defined in my config.json file. The hostnames for both exist in my hosts file and are mapped to 127.0.0.1. The ports that both go apps listen on are also mapped correctly. I don't want this utility listening on Port 80, but it uses the hostname header to proxy the requests. How am I supposed to call my services then?

{
    "port": 9000,
    "server":[
        {
            "host": "local.service1.com",
            "port": 5000,
            "target": "/Users/foo/go/src/bitbucket.org/service1/server.go",
            "startupTimeout": 5
        },
        {
            "host": "local.service2.com",
            "port": 5001,
            "target": "/Users/foo/go/src/bitbucket.org/service2/server.go",
            "startupTimeout": 5
        }
    ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.