VERSION=3.0.1 PORT=3000 go run kibana.go
Last active
August 29, 2015 14:00
-
-
Save tobstarr/11401955 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
) | |
var ( | |
logger = log.New(os.Stderr, "", 0) | |
installDir = os.ExpandEnv("$HOME/.kibana") | |
) | |
func version() string { | |
v := os.Getenv("VERSION") | |
if v == "" { | |
return "3.0.1" | |
} | |
return v | |
} | |
func root() string { | |
return installDir + "/kibana-" + version() | |
} | |
func validate() error { | |
wd, e := os.Getwd() | |
if e != nil { | |
return e | |
} | |
defer os.Chdir(wd) | |
f, e := os.Open(root() + "/src/index.html") | |
if e == nil { | |
f.Close() | |
return nil | |
} | |
if !os.IsNotExist(e) { | |
return e | |
} | |
os.MkdirAll(installDir, 0755) | |
tmpDir, e := ioutil.TempDir(installDir, "tmp") | |
if e != nil { | |
return e | |
} | |
defer os.RemoveAll(tmpDir) | |
logger.Printf("downloading version %q", version()) | |
rsp, e := http.Get("https://github.com/elasticsearch/kibana/archive/v" + version() + ".tar.gz") | |
if e != nil { | |
return e | |
} | |
defer rsp.Body.Close() | |
e = os.Chdir(tmpDir) | |
if e != nil { | |
return e | |
} | |
cmd := exec.Command("tar", "xvfz", "-") | |
cmd.Stdin = rsp.Body | |
e = cmd.Run() | |
if e != nil { | |
return e | |
} | |
defer func() { | |
logger.Printf("finished downloading kibana") | |
}() | |
return os.Rename(tmpDir+"/kibana-"+version(), root()) | |
} | |
func main() { | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "3000" | |
} | |
e := validate() | |
if e != nil { | |
log.Fatal(e) | |
} | |
addr := ":" + port | |
logger.Printf("serving kibana %q on address %q", version(), addr) | |
http.ListenAndServe(addr, http.FileServer(http.Dir(root()+"/src"))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment