Last active
August 29, 2015 14:14
-
-
Save halkeye/21f8d0463e27467f3a7f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"path" | |
"path/filepath" | |
"strings" | |
) | |
var directory = flag.String("directory", "/apps", "Root directory to scan") | |
var port = flag.String("port", "5090", "Port to run webserver on") | |
func handler(w http.ResponseWriter, r *http.Request) { | |
var versions map[string]string = make(map[string]string) | |
// allow cross domain AJAX requests | |
w.Header().Set("Access-Control-Allow-Origin", "*") | |
versionFiles, err := filepath.Glob(path.Join(*directory, "*", "current", "VERSION")) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
for _, file := range versionFiles { | |
parts := strings.Split(file, string(os.PathSeparator)) | |
app := parts[len(parts)-3] | |
dat, err := ioutil.ReadFile(file) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
versions[app] = strings.Trim(string(dat), "\n") | |
} | |
b, err := json.Marshal(versions) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(b) | |
} | |
func main() { | |
flag.Parse() | |
http.HandleFunc("/", handler) | |
fmt.Printf("Now launching http://0.0.0.0:%s\n", *port) | |
err := http.ListenAndServe(":"+*port, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment