当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.Abort方法代码示例

本文整理汇总了Golang中github.com/hoisie/web.Context.Abort方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Abort方法的具体用法?Golang Context.Abort怎么用?Golang Context.Abort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/hoisie/web.Context的用法示例。


在下文中一共展示了Context.Abort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: renderTemplate

func renderTemplate(webContext *web.Context, template string, action string, context interface{}, statusCode int) {
	webContext.WriteHeader(statusCode)
	err := templates[template].ExecuteTemplate(webContext, action+".html", context)

	if err != nil {
		log.Println("[ERROR] renderTemplate: ", err)
		webContext.Abort(500, "Unable to process request.")
	}
}
开发者ID:xyproto,项目名称:forum,代码行数:9,代码来源:controllers.go

示例2: S3GetHandler

func S3GetHandler(ctx *web.Context, key string) (ret string) {
	val := FakeS3[key]
	if val == "" {
		ctx.Abort(404, "Not Found")
		return
	} else if val == "FAIL" {
		ctx.Redirect(301, "htttttttp://idon'twork")
		return
	} else {
		return val
	}
}
开发者ID:jpoz,项目名称:s3meta,代码行数:12,代码来源:s3_test.go

示例3: upsertItem

// Upsert an item in the datastore.
func upsertItem(ctx *web.Context, key string) string {
	value := ctx.Params["value"]
	if value == "" {
		ctx.Abort(400, "Item creation is missing a value")
		return ""
	}

	// insert the item
	item := datastore.NewItem(key, value)
	ds.InsertItem(item)

	return "created " + key
}
开发者ID:ranman,项目名称:hipsterdb,代码行数:14,代码来源:main.go

示例4: Flatpage

// A Flatpage view.  Attach it via web.Get wherever you want flatpages to be available
func Flatpage(ctx *web.Context, url string) string {
	p := GetPage(url)
	if p == nil {
		ctx.Abort(404, "Page not found")
		return ""
	}

	return template.Render("base.mandira", M{
		"body":        p.ContentRendered,
		"title":       "jmoiron.net",
		"description": "Blog and assorted media from Jason Moiron.",
	})
}
开发者ID:patrickToca,项目名称:monet,代码行数:14,代码来源:app.go

示例5: documentStream

// documentStream handles websocket requests from the client for a particular document
func documentStream(ctx *web.Context, documentId string) {
	w := ctx.ResponseWriter
	r := ctx.Request
	if r.Header.Get("Origin") != "http://"+r.Host {
		ctx.Abort(403, "Origin not allowed")
		// http.Error(w, "Origin not allowed", 403)
		return
	}
	ws, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		ctx.Abort(400, "Not a websocket handshake")
		return
	} else if err != nil {
		log.Println(err)
		return
	}
	redis_conn, _ := hub.GetRedis()
	defer redis_conn.Close()
	s, err := redis.String(redis_conn.Do("GET", documentId))
	//make new document at that stringId
	if err != nil {
		doc := document.NewDoc(documentId)
		document_hubs[doc.Name] = hub.DocumentHub{
			Document:    doc,
			Broadcast:   make(chan hub.Message),
			Register:    make(chan *hub.DocumentConnection),
			Unregister:  make(chan *hub.DocumentConnection),
			Connections: make(map[*hub.DocumentConnection]bool),
		}
		h := document_hubs[doc.Name]
		go h.Run()
		json_bytes, _ := json.Marshal(doc)
		redis_conn.Do("SET", documentId, string(json_bytes))
		c := &hub.DocumentConnection{Send: make(chan hub.Message, 256), Ws: ws, H: document_hubs[doc.Name]}
		document_hubs[doc.Name].Register <- c
		mainHub.Broadcast <- json_bytes
		go c.WritePump()
		c.ReadPump()
	} else {
		var doc document.Document
		fmt.Println(s)
		err := json.Unmarshal([]byte(s), &doc)
		if err != nil {
			fmt.Println("Error:", err, s)
		}
		c := &hub.DocumentConnection{Send: make(chan hub.Message, 256), Ws: ws, H: document_hubs[doc.Name]}
		document_hubs[doc.Name].Register <- c
		go c.WritePump()
		c.ReadPump()
	}
}
开发者ID:GeorgeErickson,项目名称:6.824-Final-Project,代码行数:52,代码来源:main.go

示例6: uploader

func uploader(ctx *web.Context, key string) {
	updateRemoteAddr(ctx)

	if _, ok := uploadRequests[key]; ok {
		// key exists
		ctx.Forbidden()
		return
	}

	wait := make(chan string)

	defer delete(uploadRequests, key)
	uploadRequests[key] = uploadRequest{ctx.Request, wait}

	var result string
	select {
	case result = <-wait:
	case <-time.After(WAIT_CLIENT_TIMEOUT):
		result = "wait client timeout"
	}

	if result == "connected" {
		// wait actual result
		result = <-wait
	}

	var body string

	if xrw := ctx.Request.Header.Get("X-Requested-With"); xrw == "XMLHttpRequest" {
		body = "{\"result\": \"" + result + "\"}\n"

		ctx.SetHeader("Content-Type", "application/json", true)
	} else {
		tmpl, err := template.New("uploader").Parse(uploadTemplate)

		if err != nil {
			panic(err)
		}

		var buf bytes.Buffer
		tmpl.Execute(&buf, UploadTemplateValue{result})
		body = buf.String()
	}

	if result == "ok" {
		ctx.WriteString(body)
	} else {
		ctx.Abort(500, body)
		ctx.Request.Body.Close()
	}
}
开发者ID:nakamuray,项目名称:fileproxy,代码行数:51,代码来源:fileproxy.go

示例7: blogDetail

func blogDetail(ctx *web.Context, slug string) string {
	var post = new(Post)
	err := db.Find(post, M{"slug": slug}).One(&post)
	if err != nil {
		fmt.Println(err)
		ctx.Abort(404, "Page not found")
		return ""
	}

	return template.Render("base.mandira", M{
		"body":        RenderPost(post),
		"title":       post.Title,
		"description": post.Summary})
}
开发者ID:inhies,项目名称:monet,代码行数:14,代码来源:app.go

示例8: Flatpage

// A Flatpage view.  Attach it via web.Get wherever you want flatpages to be available
func Flatpage(ctx *web.Context, url string) string {
	p := GetPage(url)
	fmt.Printf("Got page %v for url %s\n", p, url)
	if p == nil {
		ctx.Abort(404, "Page not found")
		return ""
	}

	return template.Render("base.mandira", M{
		"body":        p.ContentRendered,
		"title":       "Jamil Seaidoun",
		"description": "Blog and assorted media from Jamil Seaidoun.",
	})
}
开发者ID:jseaidou,项目名称:monet,代码行数:15,代码来源:app.go

示例9: handlePaginatedSection

/**
 * Handles request for section
 */
func handlePaginatedSection(ctx *web.Context, section string, page string) string {
	config, err := getConfig()
	if err != nil {
		ctx.Abort(500, "Configuration error.")
		return ""
	}
	tpl := pongo.Must(pongo.FromFile(config.TemplateFolder+"/template.html", nil))
	var content, output string
	p, _ := strconv.Atoi(page)
	output, err = getAbstracts(section, p, &config)
	if err != nil {
		ctx.Abort(404, "Page not found. Could not load abstracts")
		return ""
	}
	content = string(blackfriday.MarkdownCommon([]byte(output)))
	menu, err := getMenu(&config)
	if err != nil {
		ctx.Abort(501, "Could not load menu")
		return ""
	}
	var response *string
	response, err = tpl.Execute(&pongo.Context{"content": content, "menu": menu,
		"currentMenu": menu.GetCurrent(section)})
	if err != nil {
		ctx.Abort(501, "")
		return err.Error()
	}
	return *response
}
开发者ID:rredpoppy,项目名称:gosite,代码行数:32,代码来源:site.go

示例10: handleSection

// Wrapper for handling paginated section when no section is given
func handleSection(ctx *web.Context, section string) string {
	if len(section) == 0 {
		config, err := getConfig()
		if err != nil {
			ctx.Abort(500, "Configuration error.")
			return ""
		}
		menu, err := getMenu(&config)
		if err != nil {
			ctx.Abort(501, "Could not load menu")
			return ""
		}
		return handlePaginatedSection(ctx, menu[0].Section, "1")
	}
	return handlePaginatedSection(ctx, section, "1")
}
开发者ID:rredpoppy,项目名称:gosite,代码行数:17,代码来源:site.go

示例11: getItem

// Returns a key or abort with an error if it doesn't exist or the item
// has gone mainstream.
func getItem(ctx *web.Context, key string) string {
	item, err := ds.GetItem(key)
	if err != nil {
		var errorStatus int
		if err.Error() == datastore.ACCESS_MISSING {
			errorStatus = 404
		} else {
			errorStatus = 403
		}

		ctx.Abort(errorStatus, err.Error())
		return ""
	}

	return item.Value
}
开发者ID:ranman,项目名称:hipsterdb,代码行数:18,代码来源:main.go

示例12: PublishPost

func (c *Controller) PublishPost(ctx *web.Context) {
	if !c.sessionManager.LoggedIn(ctx) {
		ctx.Redirect(303, "/login")
		return
	}

	file, head, err := ctx.Request.FormFile("publishFile")
	if err != nil {
		ctx.Abort(405, "error, post without a file")
		return
	}
	saveFile, err := os.Create("articles/" + head.Filename)
	defer saveFile.Close()
	io.Copy(saveFile, file)
	file.Close()
	ctx.Redirect(303, "/publish")
}
开发者ID:JanSichula,项目名称:coconut,代码行数:17,代码来源:controller.go

示例13: deleteDocument

func deleteDocument(ctx *web.Context, documentId string) {
	redis_conn, _ := hub.GetRedis()
	defer redis_conn.Close()

	//make new document at that stringId
	if _, ok := document_hubs[documentId]; ok {
		h := document_hubs[documentId]
		var buf bytes.Buffer
		doc := document.Document{Name: h.Document.Name, Title: h.Document.Title, Version: h.Document.Version}
		redis_conn.Do("DEL", documentId)
		json_bytes, _ := json.Marshal(doc)
		h.Broadcast <- hub.Message{M: json_bytes}
		buf.Write(json_bytes)
		io.Copy(ctx, &buf)
	} else {
		ctx.Abort(404, "Document does not exist")
		return
	}
}
开发者ID:GeorgeErickson,项目名称:6.824-Final-Project,代码行数:19,代码来源:main.go

示例14: serveWs

// serverWs handles websocket requests from the client.
func serveWs(ctx *web.Context) {
	w := ctx.ResponseWriter
	r := ctx.Request
	if r.Header.Get("Origin") != "http://"+r.Host {
		ctx.Abort(403, "Origin not allowed")
		// http.Error(w, "Origin not allowed", 403)
		return
	}
	ws, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		ctx.Abort(400, "Not a websocket handshake")
		return
	} else if err != nil {
		log.Println(err)
		return
	}
	c := &hub.Connection{Send: make(chan []byte, 256), Ws: ws, H: mainHub}
	mainHub.Register <- c
	go c.WritePump()
	c.ReadPump()
}
开发者ID:GeorgeErickson,项目名称:6.824-Final-Project,代码行数:22,代码来源:main.go

示例15: PostPaths

func (server *Server) PostPaths(ctx *web.Context) string {
	var input struct {
		Paths        []geotypes.NodeEdge
		Country      string
		SpeedProfile int
	}

	var (
		computed []geotypes.Path
	)

	if err := json.NewDecoder(ctx.Request.Body).Decode(&input); err != nil {
		content, _ := ioutil.ReadAll(ctx.Request.Body)
		ctx.Abort(400, "Couldn't parse JSON: "+err.Error()+" in '"+string(content)+"'")
		return ""
	} else {
		var err error
		computed, err = server.Via.CalculatePaths(input.Paths, input.Country, input.SpeedProfile)
		if err != nil {
			ctx.Abort(422, "Couldn't resolve addresses: "+err.Error())
			return ""
		}
	}

	res, err := json.Marshal(computed)
	if err != nil {
		ctx.Abort(500, "Couldn't serialize paths: "+err.Error())
		return ""
	}

	ctx.Header().Set("Access-Control-Allow-Origin", "*")
	ctx.ContentType("application/json")
	return string(res)
}
开发者ID:nfleet,项目名称:via,代码行数:34,代码来源:api.go


注:本文中的github.com/hoisie/web.Context.Abort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。