本文整理汇总了Golang中github.com/Unknwon/macaron.Context.JSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.JSON方法的具体用法?Golang Context.JSON怎么用?Golang Context.JSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/Unknwon/macaron.Context
的用法示例。
在下文中一共展示了Context.JSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Static
func Static(ctx *macaron.Context) {
if len(ctx.Params(":all")) > 0 {
f, err := os.Open(
path.Join(
"docs",
"gitea",
"images",
ctx.Params(":all")))
defer f.Close()
if err != nil {
ctx.JSON(500, map[string]interface{}{
"error": err.Error(),
})
return
}
if _, err = io.Copy(ctx.RW(), f); err != nil {
ctx.JSON(500, map[string]interface{}{
"error": err.Error(),
})
return
}
return
}
ctx.Error(404)
}
示例2: AnonymousTriggerBuild
func AnonymousTriggerBuild(ctx *macaron.Context) {
r := ctx.Req
owner, repo := r.FormValue("owner"), r.FormValue("repo")
branch := r.FormValue("branch")
if owner == "" || repo == "" {
ctx.Error(500, "owner or repo should not be empty")
return
}
var mrepo = &models.Repository{
Owner: owner,
Repo: repo,
}
has, err := models.DB.Get(mrepo)
if err != nil {
ctx.Error(500, err.Error())
return
}
if has && mrepo.UserId != 0 {
ctx.Error(500, "This repo is limited to its author to build") // TODO: show who is owned
return
}
if err := triggerBuild(owner, repo, branch, "[email protected]"); err != nil {
ctx.Error(500, err.Error())
return
}
ctx.JSON(200, map[string]string{
"message": "build is triggered",
})
}
示例3: GetCustomers
func GetCustomers(ctx *macaron.Context, x *xorm.Engine) {
m, _ := url.ParseQuery(ctx.Req.URL.RawQuery)
glog.V(1).Infof("Debug %#v", m)
skip := 0
limit := 0
var err error
if v, ok := m["skip"]; ok {
skip, _ = strconv.Atoi(v[0])
}
if v, ok := m["limit"]; ok {
limit, _ = strconv.Atoi(v[0])
}
cs := make([]Customer, 0)
err = x.Limit(limit, skip).Find(&cs)
if err != nil {
glog.V(1).Infof("Get customer from db fail:%s", err.Error())
ctx.JSON(http.StatusInternalServerError, map[string]string{"message": err.Error()})
return
}
ctx.JSON(http.StatusOK, cs)
}
示例4: RecentBuild
func RecentBuild(ctx *macaron.Context) {
var repos []models.Repository
err := models.DB.Limit(10).Desc("trigger_at").Where("valid=?", true).Find(&repos)
if err != nil {
ctx.Error(500, err.Error())
return
}
ctx.JSON(200, repos)
}
示例5: PostGood
func PostGood(ctx *macaron.Context, x *xorm.Engine, g Goods) {
_, err := x.Insert(g)
if err != nil {
glog.V(1).Infof("Insert good %#v fail:%s", g, err.Error())
ctx.JSON(http.StatusBadRequest, map[string]string{"message": err.Error()})
}
ctx.JSON(http.StatusCreated, "SUCCESS")
return
}
示例6: PostCustomer
func PostCustomer(ctx *macaron.Context, x *xorm.Engine, c Customer) {
_, err := x.Insert(c)
if err != nil {
glog.V(1).Infof("Insert customer %#v fail:%s", c, err.Error())
ctx.JSON(http.StatusBadRequest, map[string]string{"message": err.Error()})
}
ctx.JSON(http.StatusCreated, "SUCCESS")
return
}
示例7: RepoList
func RepoList(ctx *macaron.Context) {
var repos []models.Repository
// TODO: change limit to paginate
err := models.DB.Limit(100).Where("valid=?", true).Desc("updated_at").Find(&repos)
if err != nil {
ctx.Error(500, err.Error())
return
}
ctx.JSON(200, repos)
}
示例8: DownloadStats
func DownloadStats(ctx *macaron.Context, r *http.Request) {
org := ctx.Params(":org")
name := ctx.Params(":name")
branch := ctx.Params(":branch")
_ = branch
repo := org + "/" + name
osarch := ctx.Params(":os") + "-" + ctx.Params(":arch")
rdx.Incr("downloads:" + repo)
rdx.Incr("downloads:" + repo + ":" + osarch)
ctx.JSON(200, "update success")
}
示例9: myDatabaseListHandler
func myDatabaseListHandler(ctx *macaron.Context) {
db, err := gorm.Open("mysql", "gorm:[email protected]/gorm?charset=utf8&parseTime=True&loc=Local")
if err != nil {
log.Println("Database Error")
}
db.DB()
contact_entries := []ContactEntry{}
db.Find(&contact_entries)
ctx.JSON(200, contact_entries)
}
示例10: Fibonacci
func Fibonacci(ctx *macaron.Context) {
number := ctx.QueryInt("number")
log.Println("DBG:", number)
result := models.FibonacciSequence(number)
if result < 0 {
ctx.JSON(422, map[string]string{
"error": "Unprocessable number: " + com.ToStr(number),
})
return
}
ctx.JSON(200, map[string]interface{}{
"result": result,
})
}
示例11: SignIn
// SignIn registers with user data
func SignIn(ctx *macaron.Context, form auth.SignIn) {
u, err := models.UserSignIn(form.Uname, form.Pass)
if err != nil {
ctx.JSON(http.StatusBadRequest, models.ResponseError{
Success: false,
Message: err.Error(),
})
return
}
tokens, _ := u.SignedString()
ctx.JSON(http.StatusOK, auth.ResponseAuth{
Success: true,
Token: tokens,
})
}
示例12: PostOrder
func PostOrder(ctx *macaron.Context, x *xorm.Engine, o Order) {
alipaytype := ctx.Params("alipaytype")
_, err := x.Insert(o)
if err != nil {
glog.V(1).Infof("Insert order %#v fail:%s", o, err.Error())
ctx.JSON(http.StatusBadRequest, map[string]string{"message": err.Error()})
}
//after saved to db,we call alipay and submit to alipay
outhtml, outscript := alipay.Form(o, alipaytype)
ob := map[string]string{"html": outhtml, "script": outscript}
ctx.Resp.Header().Set("Content-Type", "application/json")
js, _ := json.Marshal(ob)
ctx.Resp.WriteHeader(http.StatusOK)
ctx.Resp.Write([]byte(js))
return
}
示例13: TriggerBuild
func TriggerBuild(tokens oauth2.Tokens, ctx *macaron.Context) {
r := ctx.Req
owner, repo := r.FormValue("owner"), r.FormValue("repo")
branch := r.FormValue("branch")
gh := github.New(tokens.Access())
user, err := gh.User()
if err != nil {
ctx.Error(500, err.Error())
return
}
go func() {
err := models.TriggerTravisBuild(owner, repo, branch, user.Email)
if err != nil {
log.Println(err)
}
}()
ctx.JSON(200, map[string]string{
"message": "build is triggered",
})
}
示例14: UserRepoList
func UserRepoList(user *models.User, ctx *macaron.Context) {
if ctx.Req.Method == "POST" {
if time.Since(user.RepoUpdatedAt) > time.Minute {
if err := user.SyncGithub(); err != nil {
ctx.JSON(500, err.Error())
} else {
ctx.JSON(200, map[string]string{
"message": "sync github success",
})
}
} else {
ctx.JSON(200, map[string]string{
"message": "try after a minute",
})
}
return
}
if ctx.Req.Method != "GET" {
ctx.JSON(500, map[string]string{
"message": fmt.Sprintf("Method %s not supported", ctx.Req.Method),
})
return
}
repos, err := user.Repositories()
if err != nil {
ctx.Error(500, err.Error())
return
}
if len(repos) == 0 && time.Since(user.RepoUpdatedAt) > time.Hour*24 {
user.SyncGithub()
repos, _ = user.Repositories()
}
ctx.JSON(200, repos)
}
示例15: RepoBuild
func RepoBuild(user *models.User, ctx *macaron.Context) {
repo, err := getRepository(ctx.ParamsInt64(":id"))
if err != nil {
ctx.Error(500, err.Error())
return
}
branch := ctx.Req.FormValue("branch")
if ctx.Req.Method == "POST" {
err := triggerBuild(repo.Owner, repo.Repo, "", user.Email)
if err != nil {
ctx.Error(500, err.Error())
return
}
// dur := time.Since(repo.TriggerAt)
// var limitTime = time.Minute * 10
// if dur < limitTime { // too offen is not good
// ctx.Error(500, fmt.Sprintf("Too offen, retry after %v", limitTime-dur))
// return
// }
// repo.TriggerAt = time.Now()
// repo.Valid = true
// log.Println("%v", repo)
// models.DB.Id(repo.Id).Cols("trigger_at", "valid").Update(repo)
// go func() {
// err := models.TriggerTravisBuild(repo.Owner, repo.Repo, branch, user.Email)
// if err != nil {
// log.Println(err)
// }
// }()
ctx.JSON(200, map[string]string{
"message": fmt.Sprintf("build for repo: %s is triggered", strconv.Quote(repo.Owner+"/"+repo.Repo)),
})
return
}
if ctx.Req.Method == "PUT" {
if err := repo.AddBranch(branch); err != nil {
ctx.Error(500, err.Error())
return
}
ctx.JSON(200, map[string]string{
"message": "change to valid",
})
return
}
// other methods
ctx.JSON(500, map[string]string{
"message": fmt.Sprintf("Method %s not supported", ctx.Req.Method),
})
}