Created
September 9, 2015 14:17
-
-
Save d-tux/088bc461cb97da281a36 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 ( | |
"encoding/json" | |
"log" | |
"net/http" | |
"strings" | |
"time" | |
) | |
type SetupInfo struct { | |
Status string | |
Updated time.Time | |
} | |
func Info(status string) *SetupInfo { | |
return &SetupInfo{ | |
status, | |
time.Now(), | |
} | |
} | |
func (i *SetupInfo) Update(status string) { | |
if status != i.Status { | |
i.Status = status | |
i.Updated = time.Now() | |
} | |
} | |
var stata map[string]*SetupInfo | |
const address = "0.0.0.0:3030" | |
func Addr(s string) string { | |
i := strings.LastIndex(s, ":") | |
if -1 != i { | |
return s[:i] | |
} else { | |
return s | |
} | |
} | |
func setOrUpdate(remoteAddr string, status string) { | |
slot := Addr(remoteAddr) | |
if nil == stata[slot] { | |
stata[slot] = Info(status) | |
} else { | |
stata[slot].Update(status) | |
} | |
} | |
func Start(w http.ResponseWriter, req *http.Request) { | |
log.Printf("Install on %s has started", req.RemoteAddr) | |
setOrUpdate(req.RemoteAddr, "started") | |
} | |
func Post(w http.ResponseWriter, req *http.Request) { | |
log.Printf("Install on %s has reached the post-install stage", req.RemoteAddr) | |
setOrUpdate(req.RemoteAddr, "post-install") | |
} | |
func Done(w http.ResponseWriter, req *http.Request) { | |
log.Printf("Install on %s has completed", req.RemoteAddr) | |
setOrUpdate(req.RemoteAddr, "done") | |
} | |
func Get(w http.ResponseWriter, req *http.Request) { | |
w.Header().Set("Content-type", "application/json") | |
encoder := json.NewEncoder(w) | |
encoder.Encode(stata) | |
} | |
func main() { | |
stata = make(map[string]*SetupInfo) | |
http.HandleFunc("/install/start", Start) | |
http.HandleFunc("/install/post", Post) | |
http.HandleFunc("/install/done", Done) | |
http.HandleFunc("/get", Get) | |
log.Printf("Listening on %s\n", address) | |
http.ListenAndServe(address, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment