envspew/cmd/web/main.go

41 lines
664 B
Go

package main
import (
"encoding/json"
"net/http"
"os"
"time"
)
type config map[string]interface{}
func envOr(name, val string) string {
if result := os.Getenv(name); result != "" {
return result
}
return val
}
func loadConfig(cfg config) error {
fin, err := os.Open(envOr("CONFIG_PATH", "./envspew.json"))
if err != nil {
return err
}
defer fin.Close()
return json.NewDecoder(fin).Decode(&cfg)
}
func main() {
cfg := config{}
loadConfig(cfg)
cfg["now"] = time.Now().String()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(cfg)
})
http.ListenAndServe(":"+envOr("PORT", "9001"), nil)
}