Ejemplo n.º 1
0
func addNetwork(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) {
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		errors.Write(w, errors.BodyNotFound)
		return
	}

	net, ok := user.CreateNetwork(args[0], data)
	if !ok {
		errors.Write(w, errors.RequestNotJSON)
		return
	}

	oldNet := user.GetNetwork(args[0])
	if oldNet != nil {
		oldNet.Disconnect()
		user.DeleteNetwork(args[0])
	}

	if !user.AddNetwork(net) {
		log.Debugf("%s tried to create network %s for %s, but failed for unknown reasons\n", getIP(r), net.GetName(), user.GetEmail())
		errors.Write(w, errors.Internal)
		return
	}

	user.InitNetworks()
	user.SendNetworkData(net)
	log.Debugf("%s created network %s for %s\n", getIP(r), net.GetName(), user.GetEmail())
}
Ejemplo n.º 2
0
func deleteNetwork(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) {
	net := user.GetNetwork(args[0])
	if net != nil {
		if net.IsConnected() {
			net.ForceDisconnect()
		}
		if user.DeleteNetwork(args[0]) {
			log.Debugf("%s deleted network %s of %s\n", getIP(r), net.GetName(), user.GetEmail())
			w.WriteHeader(http.StatusOK)
		} else {
			log.Debugf("%s tried to delete network %s of %s, but failed for unknown reasons\n", getIP(r), net.GetName(), user.GetEmail())
			errors.Write(w, errors.Internal)
		}
	} else {
		errors.Write(w, errors.NetworkNotFound)
	}
}