本文整理匯總了Golang中github.com/microcosm-cc/microcosm/models.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Read
// Read handles GET
func (ctl *SiteReservedController) Read(c *models.Context) {
host, exists := c.RouteVars["subdomain"]
if !exists {
c.RespondWithErrorMessage("No subdomain query specified", http.StatusBadRequest)
return
}
reserved, err := models.IsReservedSubdomain(host)
if err != nil {
c.RespondWithErrorMessage(err.Error(), http.StatusInternalServerError)
return
}
var responseBody string
if reserved {
responseBody = `{"reserved":true}`
c.ResponseWriter.Header().Set("Content-Length", strconv.Itoa(len(responseBody)))
} else {
responseBody = `{"reserved":false}`
c.ResponseWriter.Header().Set("Content-Length", strconv.Itoa(len(responseBody)))
}
c.ResponseWriter.Header().Set("Content-Type", "application/json")
c.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
c.ResponseWriter.WriteHeader(http.StatusOK)
c.ResponseWriter.Write([]byte(responseBody))
}
示例2: Update
// Update handles PUT
func (ctl *IgnoredController) Update(c *models.Context) {
m := models.IgnoreType{}
err := c.Fill(&m)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("The post data is invalid: %v", err.Error()),
http.StatusBadRequest,
)
return
}
if c.Auth.ProfileID < 1 {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
m.ProfileID = c.Auth.ProfileID
status, err := m.Update()
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithOK()
}
示例3: Delete
// Delete handles DELETE
func (ctl *ProfileController) Delete(c *models.Context) {
// Right now no-one can delete as it would break attribution
// of things like Comments
c.RespondWithNotImplemented()
return
/*
_, itemTypeID, itemID, status, err := c.GetItemTypeAndItemID()
if err != nil {
c.RespondWithErrorDetail(err, status)
}
m := models.ProfileType{}
m.Id = itemID
status, err := m.Delete()
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
audit.Replace(
c.Site.ID,
h.ItemTypes[h.ItemTypeProfile],
m.Id,
c.Auth.ProfileID,
time.Now(),
c.IP,
)
c.RespondWithOK()
*/
}
示例4: Delete
// Delete handles DELETE
func (ctl *MenuController) Delete(c *models.Context, siteID int64) {
// Start :: Auth
site, status, err := models.GetSite(siteID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Use the user ID to check, since the current context is a different site (the root site)
// than the site the owner profile is associated with.
owner, status, err := models.GetProfileSummary(site.ID, site.OwnedByID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
if owner.UserID != c.Auth.UserID {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
// End :: Auth
status, err = models.DeleteMenu(siteID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithOK()
}
示例5: Read
// Read handles GET
func (ctl *PermissionController) Read(c *models.Context) {
ac, status, err := GetAuthContext(c)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
m := models.GetPermission(ac)
c.RespondWithData(m)
}
示例6: Read
// Read handles GET
func (ctl *MenuController) Read(c *models.Context, siteID int64) {
ems, status, err := models.GetMenu(siteID)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(ems)
}
示例7: Read
// Read handles GET
func (ctl *GeoCodeController) Read(c *models.Context) {
c.ResponseWriter.Header().Set("Content-Type", "application/json")
// Debugging info
dur := time.Now().Sub(c.StartTime)
place := strings.Trim(c.Request.URL.Query().Get("q"), " ")
if strings.Trim(c.Request.URL.Query().Get("q"), " ") == "" {
ctl.Error(c, "query needed", http.StatusBadRequest)
return
}
if c.Auth.ProfileID <= 0 {
ctl.Error(c, "no auth", http.StatusForbidden)
return
}
u, _ := url.Parse("http://open.mapquestapi.com/nominatim/v1/search.php")
q := u.Query()
q.Set("format", "json")
// We are not interested in the array returned, just the best match which is the first response
q.Set("limit", "1")
q.Set("q", place)
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
ctl.Error(c, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ctl.Error(c, err.Error(), http.StatusInternalServerError)
return
}
// Again, not interested in the outer array [], so we substring that out
t := string(body)
t = t[1 : len(t)-1]
// But if we now have nothing (no matches were found, we need to return an empty object)
if strings.Trim(t, ` `) == `` {
t = `{}`
}
body = []byte(t)
// Return our JavaScript object
contentLength := len(body)
c.ResponseWriter.Header().Set("Content-Length", strconv.Itoa(contentLength))
go models.SendUsage(c, http.StatusOK, contentLength, dur, []string{})
c.WriteResponse([]byte(body), http.StatusOK)
}
示例8: ReadMany
// ReadMany handles GET
func (ctl *EventsController) ReadMany(c *models.Context) {
// Start Authorisation
perms := models.GetPermission(
models.MakeAuthorisationContext(
c, 0, h.ItemTypes[h.ItemTypeEvent], 0),
)
if !perms.CanRead {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
// End Authorisation
// Fetch query string args if any exist
query := c.Request.URL.Query()
limit, offset, status, err := h.GetLimitAndOffset(query)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
attending, status, err := h.GetAttending(query)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
ems, total, pages, status, err := models.GetEvents(c.Site.ID, c.Auth.ProfileID, attending, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
// Construct the response
thisLink := h.GetLinkToThisPage(*c.Request.URL, offset, limit, total)
m := models.EventsType{}
m.Events = h.ConstructArray(
ems,
h.APITypeEvent,
total,
limit,
offset,
pages,
c.Request.URL,
)
m.Meta.Links =
[]h.LinkType{
h.LinkType{Rel: "self", Href: thisLink.String()},
}
m.Meta.Permissions = perms
c.RespondWithData(m)
}
示例9: Read
// Read handles GET
func (ctl *AuthController) Read(c *models.Context) {
// Extract access token from request and retrieve its metadata
m, status, err := models.GetAccessToken(c.RouteVars["id"])
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("Error retrieving access token: %v", err.Error()),
status,
)
return
}
c.RespondWithData(m)
}
示例10: Error
// Error is a generic error handler for the Geo controller
func (ctl *GeoCodeController) Error(c *models.Context, message string, status int) {
errorJSON := `{"error":["` + message + `"]}`
contentLength := len(errorJSON)
c.ResponseWriter.Header().Set("Content-Length", strconv.Itoa(contentLength))
dur := time.Now().Sub(c.StartTime)
go models.SendUsage(c, status, contentLength, dur, []string{"message"})
c.WriteResponse([]byte(errorJSON), status)
return
}
示例11: Read
// Read handles GET
func (ctl *SearchController) Read(c *models.Context) {
results, status, err := models.Search(
c.Site.ID,
*c.Request.URL,
c.Auth.ProfileID,
)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
c.RespondWithData(results)
}
示例12: Create
// Create handles GET
func (ctl *MicrocosmsController) Create(c *models.Context) {
// Validate inputs
m := models.MicrocosmType{}
err := c.Fill(&m)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("The post data is invalid: %v", err.Error()),
http.StatusBadRequest,
)
return
}
// Start : Authorisation
perms := models.GetPermission(
models.MakeAuthorisationContext(c, 0, h.ItemTypes[h.ItemTypeSite], c.Site.ID),
)
if !perms.CanCreate {
c.RespondWithErrorMessage(h.NoAuthMessage, http.StatusForbidden)
return
}
// End : Authorisation
// Populate where applicable from auth and context
m.SiteID = c.Site.ID
m.Meta.CreatedByID = c.Auth.ProfileID
m.Meta.Created = time.Now()
m.OwnedByID = c.Auth.ProfileID
status, err := m.Insert()
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
audit.Create(
c.Site.ID,
h.ItemTypes[h.ItemTypeMicrocosm],
m.ID,
c.Auth.ProfileID,
time.Now(),
c.IP,
)
c.RespondWithSeeOther(
fmt.Sprintf(
"%s/%d",
h.APITypeMicrocosm,
m.ID,
),
)
}
示例13: Update
// Update handles PUT
func (ctl *ProfileOptionsController) Update(c *models.Context) {
var m = models.ProfileOptionType{}
err := c.Fill(&m)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("The post data is invalid: %v", err.Error()),
http.StatusBadRequest,
)
return
}
// Profile ID cannot be changed
m.ProfileID = c.Auth.ProfileID
status, err := m.Update()
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
audit.Update(
c.Site.ID,
h.ItemTypes[h.ItemTypeProfile],
m.ProfileID,
c.Auth.ProfileID,
time.Now(),
c.IP,
)
// Respond
c.RespondWithSeeOther(
fmt.Sprintf("/api/v1/profiles/options"),
)
}
示例14: Read
// Read handles GET either by /site or /sites/{site_id}
func (ctl *SiteController) Read(c *models.Context) {
// Check whether this site is being accessed by ID
siteQuery, exists := c.RouteVars["site_id"]
if exists {
siteID, err := strconv.ParseInt(siteQuery, 10, 64)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("The supplied site ID ('%s') is not a number.", siteQuery),
http.StatusBadRequest,
)
return
}
site, status, err := models.GetSite(siteID)
if err != nil {
c.RespondWithErrorMessage(
fmt.Sprintf("No site with ID %d found", siteID),
status,
)
return
}
c.RespondWithData(site)
return
}
// Site already populated in context, so only fetch permissions
c.Site.Meta.Permissions = models.GetPermission(
models.MakeAuthorisationContext(
c, 0, h.ItemTypes[h.ItemTypeSite], c.Site.ID),
)
c.RespondWithData(c.Site)
return
}
示例15: ReadMany
// ReadMany handles GET for the collection
func (ctl *AttachmentsController) ReadMany(c *models.Context) {
itemTypeID, itemID, perms, status, err := ParseItemInfo(c)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
if !perms.CanRead {
c.RespondWithErrorMessage(
h.NoAuthMessage,
http.StatusForbidden,
)
return
}
query := c.Request.URL.Query()
limit, offset, status, err := h.GetLimitAndOffset(query)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
attachments, total, pages, status, err := models.GetAttachments(itemTypeID, itemID, limit, offset)
if err != nil {
c.RespondWithErrorDetail(err, status)
return
}
thisLink := h.GetLinkToThisPage(*c.Request.URL, offset, limit, total)
m := models.AttachmentsType{}
m.Attachments = h.ConstructArray(
attachments,
h.APITypeAttachment,
total,
limit,
offset,
pages,
c.Request.URL,
)
m.Meta.Links =
[]h.LinkType{
h.LinkType{Rel: "self", Href: thisLink.String()},
}
m.Meta.Permissions = perms
c.RespondWithData(m)
}