Exemplo n.º 1
0
func ServerCreate(w http.ResponseWriter, r *http.Request) {
	if authentication.IsAllowed(w, r) {
		var server pulp.Server
		body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
		if err != nil {
			logger.Log("could not read POST body, Error: "+err.Error(), logger.ERROR)
		}

		if err := r.Body.Close(); err != nil {
			logger.Log("could not close POST body, Error: "+err.Error(), logger.ERROR)
		}

		if err := json.Unmarshal(body, &server); err != nil {
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(422) // unprocessable entity
			if err := json.NewEncoder(w).Encode(err); err != nil {
				logger.Log("could not json/encode error, Error: "+err.Error(), logger.ERROR)
			}
		}
		session, collection := db.InitServerCollection()
		defer session.Close()
		server.Added = time.Now()
		err = collection.Insert(server)
		if err != nil {
			logger.Log("could not insert server to DB, Error: "+err.Error(), logger.ERROR)
		}
		w.Header().Set("Content-Type", "application/json; charset=UTF=8")
		w.WriteHeader(http.StatusCreated)
		if err := json.NewEncoder(w).Encode(server); err != nil {
			panic(err)
		}
	}
}
Exemplo n.º 2
0
// Example restricted handler
func RestrictedHandler(w http.ResponseWriter, r *http.Request) {
	// if user has no or invalid token error message is generated by Authenticate
	// if token is valid the content below is shown
	if authentication.IsAllowed(w, r) {
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		w.WriteHeader(http.StatusOK)
		json.NewEncoder(w).Encode(map[string]string{"secret": "you made it to the secret area"})
	}
}
Exemplo n.º 3
0
func ServerDelete(w http.ResponseWriter, r *http.Request) {
	if authentication.IsAllowed(w, r) {
		oid := getOID("serverId", r)
		session, collection := db.InitServerCollection()
		defer session.Close()
		err := collection.Remove(bson.M{"_id": oid})
		if err != nil {
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(http.StatusInternalServerError)
			if err := json.NewEncoder(w).Encode(err); err != nil {
				logger.Log("could not json/encode error, Error: "+err.Error(), logger.ERROR)
			}
		}
	}
}