func ServeBalancerEditForm(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) if !bson.IsObjectIdHex(vars["id"]) { http.Error(w, "Not Found", http.StatusNotFound) return } bal, err := data.GetBalancer(bson.ObjectIdHex(vars["id"])) if err != nil { panic(err) } err = TplBalancerEditForm.Execute(w, struct { Balancer *data.Balancer Protocols []data.Protocol Algorithms []data.Algorithm CipherSuites []data.CipherSuite }{ Balancer: bal, Protocols: data.Protocols, Algorithms: data.Algorithms, CipherSuites: data.CipherSuites, }) if err != nil { panic(err) } }
func HandleServerCreate(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) if !bson.IsObjectIdHex(vars["id"]) { http.Error(w, "Not Found", http.StatusNotFound) return } bal, err := data.GetBalancer(bson.ObjectIdHex(vars["id"])) if err != nil { panic(err) } err = r.ParseForm() if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } body := struct { Label string `schema:"label"` Settings struct { Address string `schema:"address"` } `schema:"settings"` }{} err = schema.NewDecoder().Decode(&body, r.PostForm) if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } srv := data.Server{} srv.BalancerId = bal.Id srv.Label = body.Label srv.Settings.Address = body.Settings.Address err = srv.Put() if err != nil { panic(err) } err = feline.Commit(bal) if err != nil { panic(err) } http.Redirect(w, r, "/servers/"+srv.Id.Hex()+"/edit", http.StatusSeeOther) }
func ServeServerNewForm(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) if !bson.IsObjectIdHex(vars["id"]) { http.Error(w, "Not Found", http.StatusNotFound) return } bal, err := data.GetBalancer(bson.ObjectIdHex(vars["id"])) if err != nil { panic(err) } err = TplServerNewForm.Execute(w, struct { Balancer *data.Balancer }{ Balancer: bal, }) if err != nil { panic(err) } }
func HandleBalancerUpdate(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) if !bson.IsObjectIdHex(vars["id"]) { http.Error(w, "Not Found", http.StatusNotFound) return } bal, err := data.GetBalancer(bson.ObjectIdHex(vars["id"])) if err != nil { panic(err) } err = r.ParseForm() if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } body := struct { Label string `schema:"label"` Settings struct { Hostname string `schema:"hostname"` Port int `schema:"port"` Protocol string `schema:"protocol"` Algorithm string `schema:"algorithm"` SSLOptions struct { CipherSuite string `schema:"cipher_suite"` Certificate *string `schema:"certificate"` PrivateKey *string `schema:"private_key"` } `schema:"ssl_options"` } `schema:"settings"` }{} err = schema.NewDecoder().Decode(&body, r.PostForm) if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } bal.Label = body.Label bal.Settings.Hostname = body.Settings.Hostname bal.Settings.Port = body.Settings.Port bal.Settings.Protocol = data.Protocol(body.Settings.Protocol) bal.Settings.Algorithm = data.Algorithm(body.Settings.Algorithm) if body.Settings.Protocol == "https" { bal.Settings.SSLOptions.CipherSuite = "recommended" if body.Settings.SSLOptions.Certificate != nil { bal.Settings.SSLOptions.Certificate = []byte(*body.Settings.SSLOptions.Certificate) } if body.Settings.SSLOptions.PrivateKey != nil { bal.Settings.SSLOptions.PrivateKey = []byte(*body.Settings.SSLOptions.PrivateKey) } } err = bal.Put() if err != nil { panic(err) } err = feline.Commit(bal) if err != nil { panic(err) } http.Redirect(w, r, "/balancers/"+bal.Id.Hex(), http.StatusSeeOther) }