本文整理汇总了Golang中github.com/lavab/api/utils.JSONResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang JSONResponse函数的具体用法?Golang JSONResponse怎么用?Golang JSONResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了JSONResponse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: FilesList
func FilesList(c web.C, w http.ResponseWriter, r *http.Request) {
session := c.Env["token"].(*models.Token)
query := r.URL.Query()
email := query.Get("email")
name := query.Get("name")
if email == "" || name == "" {
utils.JSONResponse(w, 400, &FilesListResponse{
Success: false,
Message: "No email or name in get params",
})
return
}
files, err := env.Files.GetInEmail(session.Owner, email, name)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Unable to fetch files")
utils.JSONResponse(w, 500, &FilesListResponse{
Success: false,
Message: "Internal error (code FI/LI/01)",
})
return
}
utils.JSONResponse(w, 200, &FilesListResponse{
Success: true,
Files: &files,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:33,代码来源:files.go
示例2: FilesGet
// FilesGet gets the requested file from the database
func FilesGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the file from the database
file, err := env.Files.GetFile(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &FilesGetResponse{
Success: false,
Message: "File not found",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Check for ownership
if file.Owner != session.Owner {
utils.JSONResponse(w, 404, &FilesGetResponse{
Success: false,
Message: "File not found",
})
return
}
// Write the file to the response
utils.JSONResponse(w, 200, &FilesGetResponse{
Success: true,
File: file,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:30,代码来源:files.go
示例3: LabelsGet
// LabelsGet does *something* - TODO
func LabelsGet(c web.C, w http.ResponseWriter, req *http.Request) {
// Get the label from the database
label, err := env.Labels.GetLabel(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &LabelsGetResponse{
Success: false,
Message: "Label not found",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Check for ownership
if label.Owner != session.Owner {
utils.JSONResponse(w, 404, &LabelsGetResponse{
Success: false,
Message: "Label not found",
})
return
}
totalCount, err := env.Threads.CountByLabel(label.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"label": label.ID,
}).Error("Unable to fetch total threads count")
utils.JSONResponse(w, 500, &LabelsListResponse{
Success: false,
Message: "Internal error (code LA/GE/01)",
})
return
}
unreadCount, err := env.Threads.CountByLabelUnread(label.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"label": label.ID,
}).Error("Unable to fetch unread threads count")
utils.JSONResponse(w, 500, &LabelsListResponse{
Success: false,
Message: "Internal error (code LA/GE/01)",
})
return
}
label.TotalThreadsCount = totalCount
label.UnreadThreadsCount = unreadCount
// Write the label to the response
utils.JSONResponse(w, 200, &LabelsGetResponse{
Success: true,
Label: label,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:61,代码来源:labels.go
示例4: ContactsGet
// ContactsGet gets the requested contact from the database
func ContactsGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the contact from the database
contact, err := env.Contacts.GetContact(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &ContactsGetResponse{
Success: false,
Message: "Contact not found",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Check for ownership
if contact.Owner != session.Owner {
utils.JSONResponse(w, 404, &ContactsGetResponse{
Success: false,
Message: "Contact not found",
})
return
}
// Write the contact to the response
utils.JSONResponse(w, 200, &ContactsGetResponse{
Success: true,
Contact: contact,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:30,代码来源:contacts.go
示例5: EmailsGet
// EmailsGet responds with a single email message
func EmailsGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the email from the database
email, err := env.Emails.GetEmail(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &EmailsGetResponse{
Success: false,
Message: "Email not found",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Check for ownership
if email.Owner != session.Owner {
utils.JSONResponse(w, 404, &EmailsGetResponse{
Success: false,
Message: "Email not found",
})
return
}
// Write the email to the response
utils.JSONResponse(w, 200, &EmailsGetResponse{
Success: true,
Email: email,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:30,代码来源:emails.go
示例6: AccountsGet
// AccountsGet returns the information about the specified account
func AccountsGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the account ID from the request
id := c.URLParams["id"]
// Right now we only support "me" as the ID
if id != "me" {
utils.JSONResponse(w, 501, &AccountsGetResponse{
Success: false,
Message: `Only the "me" user is implemented`,
})
return
}
// Fetch the current session from the database
session := c.Env["token"].(*models.Token)
// Fetch the user object from the database
user, err := env.Accounts.GetAccount(session.Owner)
if err != nil {
utils.JSONResponse(w, 500, &AccountsDeleteResponse{
Success: false,
Message: "Unable to resolve the account",
})
return
}
// Return the user struct
utils.JSONResponse(w, 200, &AccountsGetResponse{
Success: true,
Account: user,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:33,代码来源:accounts.go
示例7: LabelsCreate
// LabelsCreate does *something* - TODO
func LabelsCreate(c web.C, w http.ResponseWriter, req *http.Request) {
// Decode the request
var input LabelsCreateRequest
err := utils.ParseRequest(req, &input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Warn("Unable to decode a request")
utils.JSONResponse(w, 400, &LabelsCreateResponse{
Success: false,
Message: "Invalid input format",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Ensure that the input data isn't empty
if input.Name == "" {
utils.JSONResponse(w, 400, &LabelsCreateResponse{
Success: false,
Message: "Invalid request",
})
return
}
if _, err := env.Labels.GetLabelByNameAndOwner(session.Owner, input.Name); err == nil {
utils.JSONResponse(w, 409, &LabelsCreateResponse{
Success: false,
Message: "Label with such name already exists",
})
return
}
// Create a new label struct
label := &models.Label{
Resource: models.MakeResource(session.Owner, input.Name),
Builtin: false,
}
// Insert the label into the database
if err := env.Labels.Insert(label); err != nil {
utils.JSONResponse(w, 500, &LabelsCreateResponse{
Success: false,
Message: "internal server error - LA/CR/01",
})
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Could not insert a label into the database")
return
}
utils.JSONResponse(w, 201, &LabelsCreateResponse{
Success: true,
Label: label,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:61,代码来源:labels.go
示例8: AccountsWipeData
// AccountsWipeData wipes all data except the actual account and billing info.
func AccountsWipeData(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the account ID from the request
id := c.URLParams["id"]
// Right now we only support "me" as the ID
if id != "me" {
utils.JSONResponse(w, 501, &AccountsWipeDataResponse{
Success: false,
Message: `Only the "me" user is implemented`,
})
return
}
// Fetch the current session from the database
session := c.Env["token"].(*models.Token)
// Fetch the user object from the database
user, err := env.Accounts.GetTokenOwner(session)
if err != nil {
// The session refers to a non-existing user
env.Log.WithFields(logrus.Fields{
"id": session.ID,
"error": err.Error(),
}).Warn("Valid session referred to a removed account")
utils.JSONResponse(w, 410, &AccountsWipeDataResponse{
Success: false,
Message: "Account disabled",
})
return
}
// TODO: Delete contacts
// TODO: Delete emails
// TODO: Delete labels
// TODO: Delete threads
// Delete tokens
err = env.Tokens.DeleteOwnedBy(user.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"id": user.ID,
"error": err.Error(),
}).Error("Unable to remove account's tokens")
utils.JSONResponse(w, 500, &AccountsWipeDataResponse{
Success: false,
Message: "Internal error (code AC/WD/05)",
})
return
}
utils.JSONResponse(w, 200, &AccountsWipeDataResponse{
Success: true,
Message: "Your account has been successfully wiped",
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:61,代码来源:accounts.go
示例9: TokensGet
// TokensGet returns information about the current token.
func TokensGet(c web.C, w http.ResponseWriter, r *http.Request) {
// Initialize
var (
token *models.Token
err error
)
id, ok := c.URLParams["id"]
if !ok || id == "" {
// Get the token from the middleware
token = c.Env["token"].(*models.Token)
} else {
token, err = env.Tokens.GetToken(id)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": id,
}).Warn("Unable to find the token")
utils.JSONResponse(w, 404, &TokensGetResponse{
Success: false,
Message: "Invalid token ID",
})
return
}
}
// Respond with the token information
utils.JSONResponse(w, 200, &TokensGetResponse{
Success: true,
Token: token,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:34,代码来源:tokens.go
示例10: FilesCreate
// FilesCreate creates a new file
func FilesCreate(c web.C, w http.ResponseWriter, r *http.Request) {
// Decode the request
var input FilesCreateRequest
err := utils.ParseRequest(r, &input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Warn("Unable to decode a request")
utils.JSONResponse(w, 400, &FilesCreateResponse{
Success: false,
Message: "Invalid input format",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Ensure that the input data isn't empty
if input.Data == "" || input.Name == "" || input.Encoding == "" {
utils.JSONResponse(w, 400, &FilesCreateResponse{
Success: false,
Message: "Invalid request",
})
return
}
// Create a new file struct
file := &models.File{
Encrypted: models.Encrypted{
Encoding: input.Encoding,
Data: input.Data,
Schema: "file",
VersionMajor: input.VersionMajor,
VersionMinor: input.VersionMinor,
PGPFingerprints: input.PGPFingerprints,
},
Resource: models.MakeResource(session.Owner, input.Name),
}
// Insert the file into the database
if err := env.Files.Insert(file); err != nil {
utils.JSONResponse(w, 500, &FilesCreateResponse{
Success: false,
Message: "internal server error - FI/CR/01",
})
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Could not insert a file into the database")
return
}
utils.JSONResponse(w, 201, &FilesCreateResponse{
Success: true,
Message: "A new file was successfully created",
File: file,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:61,代码来源:files.go
示例11: ThreadsDelete
// ThreadsDelete removes a thread from the database
func ThreadsDelete(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the thread from the database
thread, err := env.Threads.GetThread(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &ThreadsDeleteResponse{
Success: false,
Message: "Thread not found",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Check for ownership
if thread.Owner != session.Owner {
utils.JSONResponse(w, 404, &ThreadsDeleteResponse{
Success: false,
Message: "Thread not found",
})
return
}
// Perform the deletion
err = env.Threads.DeleteID(c.URLParams["id"])
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": c.URLParams["id"],
}).Error("Unable to delete a thread")
utils.JSONResponse(w, 500, &ThreadsDeleteResponse{
Success: false,
Message: "Internal error (code TH/DE/01)",
})
return
}
// Remove dependent emails
err = env.Emails.DeleteByThread(c.URLParams["id"])
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": c.URLParams["id"],
}).Error("Unable to delete emails by thread")
utils.JSONResponse(w, 500, &ThreadsDeleteResponse{
Success: false,
Message: "Internal error (code TH/DE/02)",
})
return
}
// Write the thread to the response
utils.JSONResponse(w, 200, &ThreadsDeleteResponse{
Success: true,
Message: "Thread successfully removed",
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:60,代码来源:threads.go
示例12: ThreadsGet
// ThreadsGet returns information about a single thread.
func ThreadsGet(c web.C, w http.ResponseWriter, r *http.Request) {
thread, err := env.Threads.GetThread(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &ThreadsGetResponse{
Success: false,
Message: "Thread not found",
})
return
}
session := c.Env["token"].(*models.Token)
if thread.Owner != session.Owner {
utils.JSONResponse(w, 404, &ThreadsGetResponse{
Success: false,
Message: "Thread not found",
})
return
}
manifest, err := env.Emails.GetThreadManifest(thread.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": thread.ID,
}).Error("Unable to get a manifest")
} else {
thread.Manifest = manifest
}
var emails []*models.Email
if ok := r.URL.Query().Get("list_emails"); ok == "true" || ok == "1" {
emails, err = env.Emails.GetByThread(thread.ID)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": thread.ID,
}).Error("Unable to fetch emails linked to a thread")
utils.JSONResponse(w, 500, &ThreadsGetResponse{
Success: false,
Message: "Unable to retrieve emails",
})
return
}
}
utils.JSONResponse(w, 200, &ThreadsGetResponse{
Success: true,
Thread: thread,
Emails: &emails,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:54,代码来源:threads.go
示例13: AuthMiddleware
// AuthMiddleware checks whether the token passed with the request is valid
func AuthMiddleware(c *web.C, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Read the Authorization header
header := r.Header.Get("Authorization")
if header == "" {
utils.JSONResponse(w, 401, &AuthMiddlewareResponse{
Success: false,
Message: "Missing auth token",
})
return
}
// Split it into two parts
headerParts := strings.Split(header, " ")
if len(headerParts) != 2 || headerParts[0] != "Bearer" {
utils.JSONResponse(w, 401, &AuthMiddlewareResponse{
Success: false,
Message: "Invalid authorization header",
})
return
}
// Get the token from the database
token, err := env.Tokens.GetToken(headerParts[1])
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Cannot retrieve session from the database")
utils.JSONResponse(w, 401, &AuthMiddlewareResponse{
Success: false,
Message: "Invalid authorization token",
})
return
}
// Check if it's expired
if token.Expired() {
utils.JSONResponse(w, 419, &AuthMiddlewareResponse{
Success: false,
Message: "Authorization token has expired",
})
env.Tokens.DeleteID(token.ID)
return
}
// Continue to the next middleware/route
c.Env["token"] = token
h.ServeHTTP(w, r)
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:52,代码来源:middleware.go
示例14: deleteToken
func deleteToken(c web.C, w http.ResponseWriter, req *http.Request) {
id := c.URLParams["id"]
wr, err := r.Table("tokens").Get(id).Delete().RunWrite(session)
if err != nil {
utils.JSONResponse(w, 500, map[string]interface{}{
"error": err.Error(),
})
return
}
utils.JSONResponse(w, 200, map[string]interface{}{
"dropped": wr.Dropped,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:15,代码来源:tokens_delete.go
示例15: Hello
// Hello shows basic information about the API on its frontpage.
func Hello(w http.ResponseWriter, r *http.Request) {
utils.JSONResponse(w, 200, &HelloResponse{
Message: "Lavaboom API",
DocsURL: "https://docs.lavaboom.io/",
Version: env.Config.APIVersion,
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:8,代码来源:hello.go
示例16: TokensDelete
// TokensDelete destroys either the current auth token or the one passed as an URL param
func TokensDelete(c web.C, w http.ResponseWriter, r *http.Request) {
// Initialize
var (
token *models.Token
err error
)
id, ok := c.URLParams["id"]
if !ok || id == "" {
// Get the token from the middleware
token = c.Env["token"].(*models.Token)
} else {
token, err = env.Tokens.GetToken(id)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": id,
}).Warn("Unable to find the token")
utils.JSONResponse(w, 404, &TokensDeleteResponse{
Success: false,
Message: "Invalid token ID",
})
return
}
}
// Delete it from the database
if err := env.Tokens.DeleteID(token.ID); err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Unable to delete a token")
utils.JSONResponse(w, 500, &TokensDeleteResponse{
Success: false,
Message: "Internal server error - TO/DE/02",
})
return
}
utils.JSONResponse(w, 200, &TokensDeleteResponse{
Success: true,
Message: "Successfully logged out",
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:46,代码来源:tokens.go
示例17: FilesDelete
// FilesDelete removes a file from the database
func FilesDelete(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the file from the database
file, err := env.Files.GetFile(c.URLParams["id"])
if err != nil {
utils.JSONResponse(w, 404, &FilesDeleteResponse{
Success: false,
Message: "File not found",
})
return
}
// Fetch the current session from the middleware
session := c.Env["token"].(*models.Token)
// Check for ownership
if file.Owner != session.Owner {
utils.JSONResponse(w, 404, &FilesDeleteResponse{
Success: false,
Message: "File not found",
})
return
}
// Perform the deletion
err = env.Files.DeleteID(c.URLParams["id"])
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
"id": c.URLParams["id"],
}).Error("Unable to delete a file")
utils.JSONResponse(w, 500, &FilesDeleteResponse{
Success: false,
Message: "Internal error (code FI/DE/01)",
})
return
}
// Write the file to the response
utils.JSONResponse(w, 200, &FilesDeleteResponse{
Success: true,
Message: "File successfully removed",
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:45,代码来源:files.go
示例18: AddressesList
func AddressesList(c web.C, w http.ResponseWriter, r *http.Request) {
session := c.Env["token"].(*models.Token)
addresses, err := env.Addresses.GetOwnedBy(session.Owner)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err.Error(),
}).Error("Unable to fetch addresses")
utils.JSONResponse(w, 500, &AddressesListResponse{
Success: false,
Message: "Internal error (code AD/LI/01)",
})
return
}
utils.JSONResponse(w, 200, &AddressesListResponse{
Success: true,
Addresses: addresses,
})
}
开发者ID:carriercomm,项目名称:lavabot,代码行数:20,代码来源:addresses.go
示例19: Avatars
func Avatars(c web.C, w http.ResponseWriter, r *http.Request) {
// Parse the query params
query := r.URL.Query()
// Settings
var (
widthString = query.Get("width")
width int
)
// Read width
if widthString == "" {
width = 100
} else {
var err error
width, err = strconv.Atoi(widthString)
if err != nil {
utils.JSONResponse(w, 400, map[string]interface{}{
"succes": false,
"message": "Invalid width",
})
return
}
}
hash := c.URLParams["hash"]
ext := c.URLParams["ext"]
// data to parse
var data []byte
// md5 hash
if len(hash) == 32 {
data, _ = hex.DecodeString(hash)
}
// not md5
if data == nil {
hashed := md5.Sum([]byte(hash))
data = hashed[:]
}
// if svg
if ext == "svg" {
w.Header().Set("Content-Type", "image/svg+xml")
avatarConfig.MakeSVG(w, width, false, data)
return
}
// generate the png
w.Header().Set("Content-Type", "image/png")
png.Encode(w, avatarConfig.Make(width, false, data))
}
开发者ID:carriercomm,项目名称:api-1,代码行数:53,代码来源:avatars.go
示例20: AccountsStartOnboarding
func AccountsStartOnboarding(c web.C, w http.ResponseWriter, r *http.Request) {
// Get the account ID from the request
id := c.URLParams["id"]
// Right now we only support "me" as the ID
if id != "me" {
utils.JSONResponse(w, 501, &AccountsStartOnboardingResponse{
Success: false,
Message: `Only the "me" user is implemented`,
})
return
}
// Fetch the current session from the database
session := c.Env["token"].(*models.Token)
data, err := json.Marshal(&events.Onboarding{
Account: session.Owner,
})
if err != nil {
utils.JSONResponse(w, 500, &AccountsStartOnboardingResponse{
Success: false,
Message: "Unable to encode a message",
})
return
}
if err := env.Producer.Publish("hook_onboarding", data); err != nil {
utils.JSONResponse(w, 500, &AccountsCreateResponse{
Success: false,
Message: "Unable to initialize onboarding emails",
})
return
}
utils.JSONResponse(w, 200, &AccountsStartOnboardingResponse{
Success: true,
Message: "Onboarding emails for your account have been initialized",
})
}
开发者ID:carriercomm,项目名称:api-1,代码行数:40,代码来源:accounts.go
注:本文中的github.com/lavab/api/utils.JSONResponse函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论