本文整理汇总了Golang中github.com/stretchr/goweb/context.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Handle
// Handle writes the error from the context into the HttpResponseWriter with a
// 500 http.StatusInternalServerError status code.
func (h *DefaultErrorHandler) Handle(ctx context.Context) (stop bool, err error) {
var handlerError HandlerError = ctx.Data().Get(DataKeyForError).(HandlerError)
hostname, _ := os.Hostname()
w := ctx.HttpResponseWriter()
// write the error out
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("<!DOCTYPE html><html><head>"))
w.Write([]byte("<style>"))
w.Write([]byte("h1 { font-size: 17px }"))
w.Write([]byte("h1 strong {text-decoration:underline}"))
w.Write([]byte("h2 { background-color: #ffd; padding: 20px }"))
w.Write([]byte("footer { margin-top: 20px; border-top:1px solid black; padding:10px; font-size:0.9em }"))
w.Write([]byte("</style>"))
w.Write([]byte("</head><body>"))
w.Write([]byte(fmt.Sprintf("<h1>Error in <code>%s</code></h1><h2>%s</h2>", handlerError.Handler, handlerError)))
w.Write([]byte(fmt.Sprintf("<h3><code>%s</code> error in Handler <code>%v</code></h3> <code><pre>%s</pre></code>", reflect.TypeOf(handlerError.OriginalError), &handlerError.Handler, handlerError.Handler)))
w.Write([]byte(fmt.Sprintf("on %s", hostname)))
w.Write([]byte("<footer>Learn more about <a href='http://github.com/stretchr/goweb' target='_blank'>Goweb</a></footer>"))
w.Write([]byte("</body></html>"))
// responses are actually ignored
return false, nil
}
开发者ID:nelsam,项目名称:goweb,代码行数:29,代码来源:default_error_handler.go
示例2: ParseSamtoolsArgs
//helper function to translate args in URL query to samtools args
//manual: http://samtools.sourceforge.net/samtools.shtml
func ParseSamtoolsArgs(ctx context.Context) (argv []string, err error) {
query := ctx.HttpRequest().URL.Query()
var (
filter_options = map[string]string{
"head": "-h",
"headonly": "-H",
"count": "-c",
}
valued_options = map[string]string{
"flag": "-f",
"lib": "-l",
"mapq": "-q",
"readgroup": "-r",
}
)
for src, des := range filter_options {
if _, ok := query[src]; ok {
argv = append(argv, des)
}
}
for src, des := range valued_options {
if _, ok := query[src]; ok {
if val := query.Get(src); val != "" {
argv = append(argv, des)
argv = append(argv, val)
} else {
return nil, errors.New(fmt.Sprintf("required value not found for query arg: %s ", src))
}
}
}
return argv, nil
}
开发者ID:narayandesai,项目名称:Shock,代码行数:37,代码来源:streamer.go
示例3: Before
// Before gets called before any other method.
func (r *BooksController) Before(ctx context.Context) error {
// set a Books specific header
ctx.HttpResponseWriter().Header().Set("X-Books-Controller", "true")
return nil
}
开发者ID:JeremyMorgan,项目名称:golang-API-Test,代码行数:8,代码来源:rest-server.go
示例4: Before
// Before gets called before any other method.
func (r *DevelopersController) Before(ctx context.Context) error {
// set a Developers specific header
ctx.HttpResponseWriter().Header().Set("X-Developers-Controller", "true")
ctx.HttpResponseWriter().Header().Set("Connection", "keep-alive")
return nil
}
开发者ID:gustavodecarlo,项目名称:vtex_trial,代码行数:8,代码来源:webgit.go
示例5: ReadMany
// GET /api/session
func (ctrl *SessionController) ReadMany(ctx context.Context) (err error) {
session, _ := session_store.Get(ctx.HttpRequest(), session_name)
if session.Values["username"] != nil {
return goweb.API.RespondWithError(ctx, http.StatusBadRequest, "has session")
}
// check form value(not empty?)
id := ctx.FormValue("id")
passwd := ctx.FormValue("password")
if id == "" || passwd == "" {
return goweb.API.RespondWithError(ctx, http.StatusBadRequest, "not enough query")
}
// get userdata from database
user, err := GetUser(id, passwd)
if err != nil {
return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, "error on database")
}
if user == nil {
return goweb.API.RespondWithError(ctx, http.StatusUnauthorized, "user not found")
}
session.Values["username"] = user.Name
session.Save(ctx.HttpRequest(), ctx.HttpResponseWriter())
return goweb.API.RespondWithData(ctx, nil)
}
开发者ID:umisama,项目名称:kosenconf-080tokyo,代码行数:28,代码来源:sessionController.go
示例6: WriteResponseObject
// WriteResponseObject writes the status code and response object to the HttpResponseWriter in
// the specified context, in the format best suited based on the request.
//
// Goweb uses the WebCodecService to decide which codec to use when responding
// see http://godoc.org/github.com/stretchr/codecs/services#WebCodecService for more information.
//
// This method should be used when the Goweb Standard Response Object does not satisfy the needs of
// the API, but other Respond* methods are recommended.
func (a *GowebAPIResponder) WriteResponseObject(ctx context.Context, status int, responseObject interface{}) error {
service := a.GetCodecService()
acceptHeader := ctx.HttpRequest().Header.Get("Accept")
extension := ctx.FileExtension()
hasCallback := len(ctx.QueryValue(CallbackParameter)) > 0
codec, codecError := service.GetCodecForResponding(acceptHeader, extension, hasCallback)
if codecError != nil {
return codecError
}
var options map[string]interface{}
// do we need to add some options?
if hasCallback {
options = map[string]interface{}{constants.OptionKeyClientCallback: ctx.QueryValue(CallbackParameter)}
}
output, marshalErr := service.MarshalWithCodec(codec, responseObject, options)
if marshalErr != nil {
return marshalErr
}
// use the HTTP responder to respond
ctx.HttpResponseWriter().Header().Set("Content-Type", codec.ContentType()) // TODO: test me
a.httpResponder.With(ctx, status, output)
return nil
}
开发者ID:nelsam,项目名称:goweb,代码行数:42,代码来源:goweb_api_responder.go
示例7: PreAuthRequest
func PreAuthRequest(ctx context.Context) {
id := ctx.PathValue("id")
if p, err := preauth.Load(id); err != nil {
err_msg := "err:@preAuth load: " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, 500, err_msg)
return
} else {
if n, err := node.LoadUnauth(p.NodeId); err == nil {
switch p.Type {
case "download":
filename := n.Id
if fn, has := p.Options["filename"]; has {
filename = fn
}
streamDownload(ctx, n, filename)
preauth.Delete(id)
return
default:
responder.RespondWithError(ctx, 500, "Preauthorization type not supported: "+p.Type)
}
} else {
err_msg := "err:@preAuth loadnode: " + err.Error()
logger.Error(err_msg)
responder.RespondWithError(ctx, 500, err_msg)
}
}
return
}
开发者ID:narayandesai,项目名称:Shock,代码行数:29,代码来源:preauth.go
示例8: Create
func (c *AccountTransactions) Create(ctx context.Context) (err error) {
accountId := ctx.PathValue("account_id")
data, err := ctx.RequestData()
if err != nil {
return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
}
dataMap := data.(map[string]interface{})
transaction := models.Transaction{
AccountId: accountId,
Debit: dataMap["debit"].(float64),
Credit: dataMap["credit"].(float64),
Kind: dataMap["kind"].(string),
}
createServ := &transactions.CreateServ{}
transactionOut, err := createServ.Run(c.DbSession, transaction)
if err != nil {
log.Print(err)
return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
}
return goweb.API.RespondWithData(ctx, transactionOut)
}
开发者ID:sporto,项目名称:kic,代码行数:26,代码来源:account_transactions.go
示例9: serializeGowebResponse
func serializeGowebResponse(
c context.Context,
syntaxName string,
statements chan *goraptor.Statement) error {
var str string
if syntaxName == "trig" {
// real trig mode is crashing
serializer := goraptor.NewSerializer("ntriples")
defer serializer.Free()
ntriples, err := serializer.Serialize(statements, "")
if err != nil {
panic(err)
}
log.Printf("got %d bytes of ntriples", len(ntriples))
str = "<http://projects.bigasterisk.com/room/laundryDoor> { " + ntriples + "}"
log.Printf("str now %d bytes", len(str))
} else {
serializer := goraptor.NewSerializer(syntaxName)
defer serializer.Free()
var err error
str, err = serializer.Serialize(statements, "")
if err != nil {
panic(err)
}
}
c.HttpResponseWriter().Header().Set("Content-Type",
goraptor.SerializerSyntax[syntaxName].MimeType)
return goweb.Respond.With(c, 200, []byte(str))
}
开发者ID:drewp,项目名称:homeauto,代码行数:32,代码来源:laundry.go
示例10: GetPopularLocations
func GetPopularLocations(ctx context.Context) error {
var query bson.M
isPersonal, err := strconv.ParseBool(ctx.FormValue("personal"))
if err != nil {
isPersonal = false
}
if isPersonal {
var result m.PopularLocations
var currentUser, ok = ctx.Data()["user"].(m.User)
if ok {
query = bson.M{"user": currentUser.Id}
if err := m.GetDB("RecommendUser").Find(query).One(&result); err != nil {
//respond with empty arrays it no recommendations/popular location for current user
result = m.PopularLocations{PopularMarkers: make([]m.Marker, 0), Recommendations: make([]m.Recommendation, 0)}
//fmt.Printf("%v %v", err, result)
}
return goweb.API.RespondWithData(ctx, result)
}
}
var trendResult []m.TrendingAllUsersEntry
var recomResult []m.Recommendation
m.GetDB("TrendingAllUsers").Find(query).All(&trendResult)
m.GetDB("RecommendAllUsers").Find(query).All(&recomResult)
result := m.AllUsersTrendingAndRecom{Trend: trendResult, Recom: recomResult}
return goweb.API.RespondWithData(ctx, result)
}
开发者ID:catanm,项目名称:gms,代码行数:26,代码来源:endpoints.go
示例11: DeleteImage
func DeleteImage(ctx context.Context) error {
var currentUser, ok = ctx.Data()["user"].(m.User)
if ok {
imageId := ctx.FormValue("imageId")
if imageId == "" {
return goweb.API.Respond(ctx, 200, nil, []string{"You have to specify an image to be deleted."})
}
var image m.Image
err := m.GetDB("Image").FindId(bson.ObjectIdHex(imageId)).One(&image)
if err != nil {
log.Error("DeleteImage, remove query " + err.Error())
return goweb.API.Respond(ctx, 200, nil, []string{"You have to specify an image to be deleted."})
}
if image.User.Hex() != currentUser.Id.Hex() {
return goweb.API.Respond(ctx, 200, nil, []string{"You cannot delete this image."})
} else {
m.GetDB("Image").RemoveId(image.Id)
err = os.Remove(image.Url)
}
return goweb.API.RespondWithData(ctx, "Image deleted.")
} else {
return goweb.API.Respond(ctx, 200, nil, []string{"Please log in to delete an images."})
}
}
开发者ID:catanm,项目名称:gms,代码行数:28,代码来源:endpoints.go
示例12: GetUserInfo
func GetUserInfo(ctx context.Context) error {
var currentUser, ok = ctx.Data()["user"].(m.User)
if ok {
return goweb.API.RespondWithData(ctx, currentUser)
} else {
return goweb.API.Respond(ctx, 200, nil, []string{"Please log in."})
}
}
开发者ID:catanm,项目名称:gms,代码行数:8,代码来源:endpoints.go
示例13: With
// With writes a response to the request in the specified context.
func (r *GowebHTTPResponder) With(ctx context.Context, httpStatus int, body []byte) error {
r.WithStatus(ctx, httpStatus)
_, writeErr := ctx.HttpResponseWriter().Write(body)
return writeErr
}
开发者ID:nelsam,项目名称:goweb,代码行数:9,代码来源:goweb_http_responder.go
示例14: getUserNameFromCtx
func getUserNameFromCtx(ctx context.Context) (name string) {
session, _ := session_store.Get(ctx.HttpRequest(), session_name)
user_raw := session.Values["username"]
if user_raw == nil {
return ""
}
name = user_raw.(string)
return
}
开发者ID:umisama,项目名称:kosenconf-080tokyo,代码行数:9,代码来源:statusesController.go
示例15: nuttyGetLength
func nuttyGetLength(c context.Context) error {
sessionid := c.PathValue("sessionid")
bs, err := ioutil.ReadFile(basedir + sessionid + "/rec.json")
if err != nil {
log.Println(err)
return goweb.API.RespondWithError(c, http.StatusInternalServerError, "Unable to ReadFile")
}
return goweb.Respond.With(c, 200, bs)
}
开发者ID:krishnasrinivas,项目名称:nuttyapp,代码行数:9,代码来源:recording.go
示例16: AowQuote
func AowQuote(ctx context.Context) error {
index := randInt(len(quotes))
quote := quotes[index]
key := ctx.PathParams().Get("apiKey").Str()
if valid := isKeyValid(key); valid == true {
return goweb.API.RespondWithData(ctx, objx.MSI("quote", quote))
}
return goweb.API.RespondWithError(ctx, 400, "Access Denied: Invalid API Key")
}
开发者ID:darthlukan,项目名称:AOW-Server,代码行数:10,代码来源:controllers.go
示例17: WithStatus
// WithStatus writes the specified HTTP Status Code to the Context's ResponseWriter.
//
// If the Always200ParamName parameter is present, it will ignore the httpStatus argument,
// and always write net/http.StatusOK (200).
func (r *GowebHTTPResponder) WithStatus(ctx context.Context, httpStatus int) error {
// check for always200
if len(ctx.FormValue(Always200ParamName)) > 0 {
// always return OK
httpStatus = http.StatusOK
}
ctx.HttpResponseWriter().WriteHeader(httpStatus)
return nil
}
开发者ID:nelsam,项目名称:goweb,代码行数:15,代码来源:goweb_http_responder.go
示例18: UploadZip
func UploadZip(ctx context.Context) error {
var currentUser, ok = ctx.Data()["user"].(m.User)
if ok {
file, _, err := ctx.HttpRequest().FormFile("file")
if err != nil {
log.Error("Error on zip upload FormFile " + err.Error())
return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."})
}
defer file.Close()
outputZipPath := "../uploads/temp/" + base64.StdEncoding.EncodeToString([]byte(currentUser.Username+time.Now().String()))
if err := os.MkdirAll("../uploads/temp/", 0777); err != nil {
log.Error("Error in creating output dir " + err.Error())
return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."})
}
outputZipFile, err := os.Create(outputZipPath)
if err != nil {
log.Error("Error in creating output file in zip " + err.Error())
return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."})
}
defer outputZipFile.Close()
if _, err = io.Copy(outputZipFile, file); err != nil {
log.Error("Error in copying file " + err.Error())
os.Remove(outputZipPath)
return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."})
}
var extention = ""
if runtime.GOOS == "windows" {
extention = ".exe"
}
extractPath := "../uploads/temp/" + base64.StdEncoding.EncodeToString([]byte(currentUser.Username+time.Now().String()))
commandString := fmt.Sprintf(`7za%s e %s -o%s -p%s -aoa`, extention, outputZipPath, extractPath, "SuperSecurePassword")
commandSlice := strings.Fields(commandString)
commandCall := exec.Command(commandSlice[0], commandSlice[1:]...)
// err = commandCall.Run()
value, err := commandCall.Output()
if err != nil {
log.Error("in unarchiving zip file: " + err.Error())
log.Error("Full info about the error: " + string(value))
}
go utils.ProcessZip(currentUser, extractPath)
return goweb.API.Respond(ctx, 200, nil, nil)
} else {
return goweb.API.Respond(ctx, 200, nil, []string{"Please log in to upload a zip."})
}
}
开发者ID:catanm,项目名称:gms,代码行数:53,代码来源:endpoints.go
示例19: WithStatusText
// WithStatusText writes the specified HTTP Status Code to the Context's ResponseWriter and
// includes a body with the default status text.
func (r *GowebHTTPResponder) WithStatusText(ctx context.Context, httpStatus int) error {
writeStatusErr := r.WithStatus(ctx, httpStatus)
if writeStatusErr != nil {
return writeStatusErr
}
// write the body header
_, writeErr := ctx.HttpResponseWriter().Write([]byte(http.StatusText(httpStatus)))
return writeErr
}
开发者ID:nelsam,项目名称:goweb,代码行数:15,代码来源:goweb_http_responder.go
示例20: nuttyGet
func nuttyGet(c context.Context) error {
sessionid := c.PathValue("sessionid")
tindex := c.PathValue("tindex")
log.Println("GET ", sessionid, " ", tindex)
bs, err := ioutil.ReadFile(basedir + sessionid + "/" + tindex)
if err != nil {
log.Println(err)
return goweb.API.RespondWithError(c, http.StatusInternalServerError, "Unable to ReadFile")
}
return goweb.Respond.With(c, 200, bs)
}
开发者ID:krishnasrinivas,项目名称:nuttyapp,代码行数:13,代码来源:recording.go
注:本文中的github.com/stretchr/goweb/context.Context类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论