예제 #1
0
파일: host.go 프로젝트: bfloriang/dock2box
func (c *HostController) Create(w http.ResponseWriter, r *http.Request) {
	// Initialize empty struct
	s := models.Host{}

	// Decode JSON into struct
	err := json.NewDecoder(r.Body).Decode(&s)
	if err != nil {
		jsonError(w, r, "Failed to deconde JSON: "+err.Error(), http.StatusInternalServerError, c.envelope)
		return
	}

	// Set ID
	s.ID = bson.NewObjectId()

	// Validate input using JSON Schema
	docLoader := gojsonschema.NewGoLoader(s)
	schemaLoader := gojsonschema.NewReferenceLoader(c.schemaURI)

	res, err := gojsonschema.Validate(schemaLoader, docLoader)
	if err != nil {
		jsonError(w, r, err.Error(), http.StatusInternalServerError, c.envelope)
		return
	}

	if !res.Valid() {
		var errors []string
		for _, e := range res.Errors() {
			errors = append(errors, fmt.Sprintf("%s: %s", e.Context().String(), e.Description()))
		}
		jsonError(w, r, errors, http.StatusInternalServerError, c.envelope)
		return
	}

	// Insert entry
	if err := c.session.DB(c.database).C("hosts").Insert(s); err != nil {
		jsonError(w, r, "Insert: "+err.Error(), http.StatusInternalServerError, c.envelope)
		return
	}

	// Write content-type, header and payload
	jsonWriter(w, r, s, http.StatusCreated, c.envelope)
}
예제 #2
0
파일: host.go 프로젝트: bfloriang/dock2box
func (c *HostController) Get(w http.ResponseWriter, r *http.Request) {
	id := mux.Vars(r)["id"]

	// Validate ObjectId
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	// Get object id
	oid := bson.ObjectIdHex(id)

	// Initialize empty struct
	s := models.Host{}

	// Get entry
	if err := c.session.DB(c.database).C("hosts").FindId(oid).One(&s); err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	// Embed related data
	if r.URL.Query().Get("embed") == "true" {
		// Get Tag
		if err := c.session.DB(c.database).C("tags").FindId(s.TagID).One(&s.Tag); err != nil {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		// Get Tenant
		if err := c.session.DB(c.database).C("tenants").FindId(s.TenantID).One(&s.Tenant); err != nil {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		// Get Site
		if err := c.session.DB(c.database).C("sites").FindId(s.SiteID).One(&s.Site); err != nil {
			w.WriteHeader(http.StatusNotFound)
			return
		}
	}

	// HATEOAS Links
	hateoas := c.hateoas
	switch strings.ToLower(r.URL.Query().Get("hateoas")) {
	case "true":
		hateoas = true
	case "false":
		hateoas = false
	}
	if hateoas == true {
		links := []models.Link{}

		if s.TagID != "" {
			links = append(links, models.Link{
				HRef:   c.baseURI + "/tags/" + s.TagID.Hex(),
				Rel:    "self",
				Method: "GET",
			})
		}

		if s.TenantID != "" {
			links = append(links, models.Link{
				HRef:   c.baseURI + "/tenants/" + s.TenantID.Hex(),
				Rel:    "self",
				Method: "GET",
			})
		}

		if s.SiteID != "" {
			links = append(links, models.Link{
				HRef:   c.baseURI + "/sites/" + s.SiteID.Hex(),
				Rel:    "self",
				Method: "GET",
			})
		}

		s.Links = &links
	}

	// Write content-type, header and payload
	jsonWriter(w, r, s, http.StatusOK, c.envelope)
}