func populateDestinations() { baos := model.GetAllBaos() destinations = make(map[string]string) for _, bao := range baos { destinations[bao.ID.Hex()] = bao.FunctionName } }
func AdminHandler(w http.ResponseWriter, r *http.Request) { username := "******" password := os.Getenv("AdminPassword") authError := func() { w.Header().Set("WWW-Authenticate", "Basic realm=\"Zork\"") http.Error(w, "authorization failed", http.StatusUnauthorized) } if password == "" { authError() return } auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2) if len(auth) != 2 || auth[0] != "Basic" { authError() return } payload, err := base64.StdEncoding.DecodeString(auth[1]) if err != nil { authError() return } pair := strings.SplitN(string(payload), ":", 2) if len(pair) != 2 || !(pair[0] == username && pair[1] == password) { authError() return } baos := model.GetAllBaos() // reverse for i, j := 0, len(baos)-1; i < j; i, j = i+1, j-1 { baos[i], baos[j] = baos[j], baos[i] } var adminResponse []AdminResponse for _, bao := range baos { adminResponse = append(adminResponse, AdminResponse{ Bao: bao, HexId: bao.ID.Hex(), DateCreated: bao.Ts.Format("3:04pm Jan 2, 2006"), }) } RenderTemplate(w, "admin", adminResponse) }