一个框架用于在Go中构建RESTful API

网友投稿 586 2022-10-25

一个框架用于在Go中构建RESTful API

一个框架用于在Go中构建RESTful API

Build fast and complete RESTful APIs in Go

Go-Relax aims to provide the tools to help developers build RESTful web services, and information needed to abide by REST architectural constraints using correct HTTP semantics.

Quick Start

Install using "go get":

go get github.com/codehack/go-relax

Then import from your source:

import "github.com/codehack/go-relax"

View example_test.go for an extended example of basic usage and features.

Also, check the wiki for HowTo's and recipes.

Features

Helps build API's that follow the REST concept using ROA principles.Built-in support of HATEOAS constraint with Web Linking header tags.Follows REST "best practices", with inspiration from Heroku and GitHub.Works fine along with http.ServeMux or independently as http.HandlerSupports different media types, and mixed for requests and responses.It uses jsON media type by default, but also includes XML (needs import).The default routing engine uses trie with regexp matching for speed and flexibility.Comes with a complete set of filters to build a working API. "Batteries included"Uses sync.pool to efficiently use resources when under heavy load.

Included filters

Content - handles mixed request/response encodings, language preference, and versioning. Basic authentication - to protect any resource with passwords. CORS - Cross-Origin Resource Sharing, for remote client-server setups. ETag - entity tagging with conditional requests for efficient caching. GZip - Dynamic gzip content data compression, with ETag support. Logging - custom logging with pre- and post- request event support. Method override - GET/POST method override via HTTP header and query string. Security - Various security practices for request handling. Limits - request throttler, token-based rate limiter, and memory limits.

Upcoming filters

Relaxed - Test API's compliance with Relax API Specification (based on REST). JSON-API support. JSON-Schema for validating requests and responses. Collection-JSON support.

Documentation

The full code documentation is located at GoDoc:

http://godoc.org/github.com/codehack/go-relax

The source code is thoroughly commented, have a look.

Hello World

This minimal example creates a new Relax service that handles a Hello resource.

package mainimport ( "github.com/codehack/go-relax")type Hello stringfunc (h *Hello) Index(ctx *relax.Context) { ctx.Respond(h)}func main() { h := Hello("hello world!") svc := relax.NewService("http://api.company.com/") svc.Resource(&h) svc.Run()}

$ curl -i -X GET http://api.company.com/hello

Response:

HTTP/1.1 200 OKContent-Type: application/json;charset=utf-8Link: ; rel="self"Link: ; rel="index"Request-Id: 61d430de-7bb6-4ff8-84da-aff6fe81c0d2Server: Go-Relax/0.5.0Date: Thu, 14 Aug 2014 06:20:48 GMTContent-Length: 14"hello world!"

Credits

Go-Relax is Copyright (c) Codehack. Published under an MIT License

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:从美团外卖小程序观察到的一些思考
下一篇:周末分享-Linux 相关资料(基础知识,系统开发,高并发网络编程)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~