Example #1
0
func handleProfile(ctx *macaron.Context) string {
	switch ctx.Query("op") {
	case "startcpu":
		if err := StartCPUProfile(); err != nil {
			return err.Error()
		}
	case "stopcpu":
		if err := StopCPUProfile(); err != nil {
			return err.Error()
		}
	case "mem":
		dumpMemProf()
	case "gc":
		var buf bytes.Buffer
		DumpGCSummary(&buf)
		return string(buf.Bytes())
	default:
		return fmt.Sprintf(`<p>Available operations:</p>
<ol>
	<li><a href="%s?op=startcpu">Start CPU profile</a></li>
	<li><a href="%s?op=stopcpu">Stop CPU profile</a></li>
	<li><a href="%s?op=mem">Dump memory profile</a></li>
	<li><a href="%s?op=gc">Dump GC summary</a></li>
</ol>`, opt.ProfileURLPrefix, opt.ProfileURLPrefix, opt.ProfileURLPrefix, opt.ProfileURLPrefix)
	}
	ctx.Redirect(opt.ProfileURLPrefix)
	return ""
}
Example #2
0
func DoPupDetail(ctx *macaron.Context) {
	id := ctx.Query("Id")
	pup := findPup(id)
	if pup != nil {
		ctx.Data["IsPup"] = true
		ctx.Data["Pup"] = pup
		ctx.HTML(200, "showDetail")
	}
}
Example #3
0
func DoDogDetail(ctx *macaron.Context) {
	id := ctx.Query("Id")
	dog := findDog(id)
	if dog != nil {
		ctx.Data["IsDog"] = true
		ctx.Data["Dog"] = dog
		ctx.HTML(200, "showDetail")
	}
}
Example #4
0
// SignedInID returns the id of signed in user.
func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
	if !models.HasEngine {
		return 0
	}

	// Check access token.
	if IsAPIPath(ctx.Req.URL.Path) {
		tokenSHA := ctx.Query("token")
		if len(tokenSHA) == 0 {
			// Well, check with header again.
			auHead := ctx.Req.Header.Get("Authorization")
			if len(auHead) > 0 {
				auths := strings.Fields(auHead)
				if len(auths) == 2 && auths[0] == "token" {
					tokenSHA = auths[1]
				}
			}
		}

		// Let's see if token is valid.
		if len(tokenSHA) > 0 {
			t, err := models.GetAccessTokenBySHA(tokenSHA)
			if err != nil {
				if models.IsErrAccessTokenNotExist(err) {
					log.Error(4, "GetAccessTokenBySHA: %v", err)
				}
				return 0
			}
			t.Updated = time.Now()
			if err = models.UpdateAccessToekn(t); err != nil {
				log.Error(4, "UpdateAccessToekn: %v", err)
			}
			return t.UID
		}
	}

	uid := sess.Get("uid")
	if uid == nil {
		return 0
	}
	if id, ok := uid.(int64); ok {
		if _, err := models.GetUserByID(id); err != nil {
			if !models.IsErrUserNotExist(err) {
				log.Error(4, "GetUserById: %v", err)
			}
			return 0
		}
		return id
	}
	return 0
}
Example #5
0
func login(ctx *macaron.Context, s session.Store, opt *Options) {
	next := extractPath(ctx.Query(KEY_NEXT_PAGE))
	if s.Get(KEY_TOKEN) == nil {
		// User is not logged in.
		if next == "" {
			next = AppSubUrl + "/"
		}
		// println(111, opt.AuthCodeURL(next, "", ""))
		ctx.Redirect(opt.AuthCodeURL(next, "", ""))
		return
	}
	// No need to login, redirect to the next page.
	ctx.Redirect(next)
}
Example #6
0
func OnSignin(ctx *macaron.Context, f *session.Flash) {
	userName := ctx.Query("username")
	Password := ctx.Query("password")

	c := db.C("Account")
	account := Account{}
	err := c.Find(bson.M{"UserName": userName, "Password": Password}).One(&account)
	if err != nil {
		ctx.Data["Title"] = "出错啦!"
		ctx.Data["Info"] = "您的账户或密码错误,登录失败啦!"
		ctx.HTML(200, "info")
	}
	ctx.Data["Admin"] = account.Role == 1
	ctx.Data["Comments"] = getComments()
	ctx.HTML(200, "profile")
}
Example #7
0
func handleOAuth2Callback(ctx *macaron.Context, s session.Store, opt *Options) {
	next := extractPath(ctx.Query("state"))
	code := ctx.Query("code")
	t, err := opt.NewTransportFromCode(code)
	if err != nil {
		// Pass the error message, or allow dev to provide its own
		// error handler.
		println(err.Error())
		ctx.Redirect(PathError)
		return
	}
	// Store the credentials in the session.
	val, _ := json.Marshal(t.Token())
	s.Set(KEY_TOKEN, val)
	ctx.Redirect(next)
}
Example #8
0
func PutBlobsV2Handler(ctx *macaron.Context, log *logs.BeeLogger) (int, []byte) {
	desc := ctx.Params(":uuid")
	uuid := strings.Split(desc, "?")[0]

	digest := ctx.Query("digest")
	tarsum := strings.Split(digest, ":")[1]

	imagePathTmp := fmt.Sprintf("%v/%v", setting.ImagePath, uuid)
	layerfileTmp := fmt.Sprintf("%v/%v/layer", setting.ImagePath, uuid)
	imagePath := fmt.Sprintf("%v/tarsum/%v", setting.ImagePath, tarsum)
	layerfile := fmt.Sprintf("%v/tarsum/%v/layer", setting.ImagePath, tarsum)
	layerlen, err := modules.CopyImgLayer(imagePathTmp, layerfileTmp, imagePath, layerfile, ctx.Req.Request.Body)
	if err != nil {
		log.Error("[REGISTRY API V2] Save layerfile failed: %v", err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Save layerfile failed"})
		return http.StatusBadRequest, result
	}

	//saving specific tarsum every times is in order to split the same tarsum in HEAD handler
	i := new(models.Image)
	i.Path, i.Size = layerfile, int64(layerlen)
	if err := i.PutTarsum(tarsum); err != nil {
		log.Error("[REGISTRY API V2] Save tarsum failed: %v", err.Error())

		result, _ := json.Marshal(map[string]string{"message": "Save tarsum failed"})
		return http.StatusBadRequest, result
	}

	random := fmt.Sprintf("%s://%s/v2/%s/%s/blobs/%s",
		setting.ListenMode,
		setting.Domains,
		ctx.Params(":namespace"),
		ctx.Params(":repository"),
		digest)

	ctx.Resp.Header().Set("Docker-Content-Digest", digest)
	ctx.Resp.Header().Set("Location", random)

	result, _ := json.Marshal(map[string]string{})
	return http.StatusCreated, result
}
Example #9
0
func OnComment(ctx *macaron.Context) {
	title := ctx.Query("title")
	content := ctx.Query("content")

	c := db.C("Comment")
	comment := Comment{}
	comment.Id = bson.NewObjectId()
	comment.Title = title
	comment.Content = content
	comment.DateTime = time.Now()
	err := c.Insert(comment)
	if err != nil {
		panic(err)
	}
	//	resp := response.NewText("oMl6fs9C4x583NvZJfTcJxqvcomw", "", comment.DateTime, "["+comment.Title+"]"+comment.Content)
	//	mp.WriteRawResponse(ctx.Resp, nil, resp)
	ctx.Data["Title"] = "成功啦!"
	ctx.Data["Info"] = "您的留言已经第一时间发送出去啦!"
	ctx.HTML(200, "info")
}
func queryHandler(ctx *macaron.Context) {
	fmt.Println(ctx.Query("uid"))
	fmt.Println(ctx.QueryInt("uid"))
	fmt.Println(ctx.QueryInt64("uid"))
}
Example #11
0
func logout(ctx *macaron.Context, s session.Store) {
	next := extractPath(ctx.Query(KEY_NEXT_PAGE))
	s.Delete(KEY_TOKEN)
	ctx.Redirect(next)
}