我创建了Relay:连接API与互联网的开源层
上个月我几乎要放弃自己的副项目,因为我花了14个小时与AWS API Gateway搏斗,只是为了验证一个API密钥。
实际上我没有放弃。我只是对现代API工具的勒索性定价和糟糕的用户体验感到无比沮丧,于是我用Go自己构建了一个网关。
如果你曾经构建过面向公众的API,你肯定知道这个噩梦。你在周末写完核心业务逻辑,然后就会遇到一个巨大的障碍。
那个障碍就是将你的API安全、高效且大规模地暴露给互联网所需的基础设施。AWS API Gateway、Heroku API、Cloudflare Workers——它们都承诺让这个过程变得顺畅,但在实践中,它们往往比解决的问题更多,增加了复杂度、成本和麻烦。
这就是我创建Relay的原因,Relay是一个为简单、灵活和完全免费使用而设计的开源API网关。在这篇文章中,我将带你了解是什么激励我创建它,它的工作原理以及为什么它可能是你一直在寻找的解决方案。
问题:现代API网关已经坏了
老实说——设置API网关很少是直截了当的。我花了很多时间配置、调试和支付那些应该很简单的服务。以下是一些最常见的痛点:
1. 过高的定价模式
大多数API网关按使用量收费——请求、带宽,甚至数据传输。理论上这很合理,但在实践中,它往往导致不可预测的成本。你可能开始一个小项目,看起来很划算,但随着项目增长,你的账单可能会飙升。例如,AWS API Gateway有复杂的定价结构,包括分层定价、请求费用和连接费用,这些很容易成为你预算的黑洞。
2. 复杂的配置
API网关通常带有臃肿的仪表板和复杂的YAML配置,让简单任务感觉像火箭科学。你试过设置基本的速率限制或JWT认证,结果被一大堆你不需要的选项淹没吗?我试过,这并不好玩。
3. 缺乏灵活性
许多API网关将你锁定在其生态系统中。它们可能要求你使用特定的运行时环境,限制你的数据库选择,或强迫你采用它们的方式做事。这种缺乏灵活性会扼杀创新,并使将来迁移到更好的解决方案更加困难。
4. 糟糕的开发者体验
许多API网关的用户界面笨拙且不直观。它们通常缺乏实时监控、轻松调试和清晰文档等特性。这可以把本该是简单任务的事情变成一个令人沮丧的困境。
我创建Relay的历程
多年来,我为几个项目构建和部署过API,每次都遇到了同样的问题。随着我越来越多地使用这些工具,我意识到一定有更好的方法。
我决定通过构建自己的API网关来解决这些问题。但我不打算重新发明轮子,我想创建一个能解决我遇到的具体痛点的工具:
- 简单性:Relay应该易于设置和使用,配置开销最小。
- 灵活性:它应该是高度可定制的,能与任何后端服务配合使用。
- 成本效益:它应该是完全免费的,没有隐藏费用。
- 性能:它应该是快速、可扩展且可靠的。
我选择Go有几个原因。它是一种以其性能、并发能力和简单性而闻名的语言。此外,它有一个强大的标准库和用于构建网络服务的繁荣库生态系统。
Relay的工作原理
Relay被设计为一个轻量级的反向代理,位于你的API和互联网之间。它处理请求路由、认证、速率限制和日志记录等任务,同时将流量转发到你的后端服务。
Relay的关键组件
-
反向代理:Relay充当反向代理,接收来自互联网的请求并将它们转发到你的后端服务。这使得你的后端可以隐藏在公开端点后面,同时仍然可访问。
-
认证:Relay支持多种认证方法,包括API密钥、JWT令牌和OAuth。这确保只有授权用户才能访问你的API。
-
速率限制:为了保护你的后端免受滥用,Relay包括内置的速率限制。你可以根据IP地址、用户ID或其他标准配置规则。
-
请求路由:Relay允许你根据URL模式、标头或其他标准将请求路由到不同的后端服务。这使得构建复杂的API架构变得容易,而无需直接暴露后端端点。
-
日志记录和监控:Relay记录所有传入请求,包括标头、正文和响应时间。这使得调试问题和监控API使用情况变得容易。
示例配置
以下是一个Relay配置的简单示例:
port: 8080
services:
- name: "user-service"
url: "http://localhost:3000"
auth:
type: "api-key"
header: "X-API-Key"
rate_limit:
type: "fixed"
limit: 100
period: "1m"
在这个示例中,Relay监听8080端口,并将请求路由到user-service后端。它需要在X-API-Key标头中提供API密钥,并限制每个IP每分钟100个请求。
Relay的独特之处
Relay与其他API网关的不同之处在于它对简单性、灵活性和成本效益的关注。以下是其一些关键优势:
1. 零成本
Relay完全免费使用。没有订阅费,没有使用费,也没有隐藏费用。这使得它成为初创公司、中小企业和个人开发者的绝佳选择,他们希望在打破银行的情况下构建强大的API。
2. 易于设置
凭借最小的依赖项和简单的配置,Relay可以在几分钟内启动并运行。它不需要复杂的设置过程或对其他服务的依赖,使其适用于所有技能水平的开发者。
3. 高度可定制
Relay被设计为高度可定制。你可以使用Go插件扩展其功能,根据需要添加新功能或与其他服务集成。这种灵活性使其成为各种用例的通用解决方案。
4. 性能导向
用Go构建,Relay被设计为注重性能。它高效地处理高流量负载,并确保即使在重负载下你的API也能保持响应。这使得它成为各种规模应用程序的可靠选择。
5. 开源
作为一个开源项目,Relay是透明的,并由社区驱动。这意味着它不断被一群充满热情的开发者改进和更新。此外,如果你有新功能或改进的想法,你可以为其开发做出贡献。
开始使用Relay
如果你有兴趣尝试Relay,以下是一个快速指南,帮助你开始:
1. 安装Go
确保你的系统上安装了Go。你可以从官方Go网站下载它。
2. 克隆仓库
从GitHub克隆Relay仓库:
git clone https://github.com/mohitdebian/relay.git
cd relay
3. 构建和运行
构建Relay二进制文件:
go build -o relay
运行Relay服务器:
./relay
默认情况下,Relay监听8080端口。你可以通过在配置文件中设置port参数来将其配置为监听不同端口。
4. 配置你的后端
更新配置文件以指向你的后端服务。例如:
port: 8080
services:
- name: "user-service"
url: "http://localhost:3000"
5. 测试你的API
使用curl等工具对你的API发出请求:
curl -H "X-API-Key: your-api-key" http://localhost:8080/user
如果一切设置正确,你应该从你的后端服务收到响应。
结论:构建API的更好方式
设置API网关不应该是噩梦。使用Relay,你可以安全、高效地将你的API暴露给互联网,而且没有成本。通过关注简单性、灵活性和性能,Relay为当前市场上的臃肿和昂贵的解决方案提供了一个令人耳目一新的替代方案。
如果你厌倦了与复杂的API网关和高成本作斗争,不妨试试Relay。它可能是你一直在寻找的解决方案。
精华
Relay为传统API网关提供了一个简单、灵活且经济高效的替代方案。通过利用Go的性能和并发能力,它提供了一个强大而轻量级的解决方案,用于将你的API暴露给互联网。无论你是初创公司、中小企业还是个人开发者,Relay都能帮助你构建和扩展你的API,而无需其他解决方案带来的头痛和高成本。
I Built Relay: The Open-Source Layer Between Your API and the Internet
I almost rage-quit my own side project last month because I spent 14 hours fighting AWS API Gateway just to validate a single API key.
Actually, I didn't quit. I just got so unbelievably frustrated with the extortionate pricing and miserable UX of modern API tools that I rage-built my own Gateway in Go instead.
If you've ever built a public-facing API, you already know the nightmare. You write your core business logic in a weekend, and then hit a massive brick wall.
That brick wall is the infrastructure needed to expose your API to the internet securely, efficiently, and at scale. AWS API Gateway, Heroku API, Cloudflare Workers — they all promise to make this process smooth, but in practice, they often add more complexity, cost, and headaches than they solve.
That's why I built Relay, an open-source API gateway designed to be simple, flexible, and completely free to use. In this post, I'll walk you through what inspired me to build it, how it works under the hood, and why it might be the solution you've been looking for.
The Problem: Modern API Gateways Are Broken
Let's be honest — setting up an API gateway is rarely straightforward. I've spent countless hours configuring, debugging, and paying for services that should have been simple. Here are some of the most common pain points:
1. Exorbitant Pricing Models
Most API gateways charge based on usage — requests, bandwidth, even data transfer. This makes sense in theory, but in practice, it often leads to unpredictable costs. You might start with a small project that seems affordable, only to find your bill skyrocketing as it grows. AWS API Gateway, for example, has a complex pricing structure with tiered pricing, request charges, and connection charges that can easily become a black hole for your budget.
2. Complex Configuration
API gateways often come with bloated dashboards and complex YAML configurations that make simple tasks feel like rocket science. Have you ever tried setting up a basic rate limit or JWT authentication only to be overwhelmed by a sea of options you don't need? I have, and it's not fun.
3. Lack of Flexibility
Many API gateways lock you into their ecosystem. They might require you to use specific runtime environments, limit your choice of databases, or force you to adopt their way of doing things. This lack of flexibility can stifle innovation and make it harder to migrate to better solutions in the future.
4. Poor Developer Experience
The user interface for many API gateways is clunky and unintuitive. They often lack features like real-time monitoring, easy debugging, and clear documentation. This can turn what should be a simple task into a frustrating ordeal.
My Journey to Building Relay
I've built and deployed APIs for several projects over the years, and each time, I've encountered the same issues. The more I worked with these tools, the more I realized that there had to be a better way.
I decided to tackle the problem by building my own API gateway. But instead of reinventing the wheel, I wanted to create something that addressed the specific pain points I'd experienced:
- Simplicity: Relay should be easy to set up and use, with a minimal configuration footprint.
- Flexibility: It should be highly customizable and work with any backend service.
- Cost-Effectiveness: It should be completely free to use, with no hidden charges.
- Performance: It should be fast, scalable, and reliable.
I chose Go for several reasons. It's a language known for its performance, concurrency capabilities, and simplicity. Plus, it has a robust standard library and a thriving ecosystem of libraries for building network services.
How Relay Works Under the Hood
Relay is designed as a lightweight, reverse proxy that sits between your API and the internet. It handles tasks like request routing, authentication, rate limiting, and logging, all while passing through to your backend service.
Key Components of Relay
-
Reverse Proxy: Relay acts as a reverse proxy, receiving requests from the internet and forwarding them to your backend service. This allows your backend to be hidden behind a public-facing endpoint while still being accessible.
-
Authentication: Relay supports multiple authentication methods, including API keys, JWT tokens, and OAuth. This ensures that only authorized users can access your API.
-
Rate Limiting: To protect your backend from abuse, Relay includes built-in rate limiting. You can configure rules based on IP addresses, user IDs, or other criteria.
-
Request Routing: Relay allows you to route requests to different backend services based on URL patterns, headers, or other criteria. This makes it easy to build complex API architectures without exposing your backend endpoints directly.
-
Logging and Monitoring: Relay logs all incoming requests, including headers, body, and response times. This makes it easy to debug issues and monitor API usage.
Example Configuration
Here's a simple example of what a Relay configuration might look like:
port: 8080
services:
- name: "user-service"
url: "http://localhost:3000"
auth:
type: "api-key"
header: "X-API-Key"
rate_limit:
type: "fixed"
limit: 100
period: "1m"
In this example, Relay listens on port 8080 and routes requests to the user-service backend. It requires an API key in the X-API-Key header and limits each IP to 100 requests per minute.
Why Relay Stands Out
What makes Relay different from other API gateways is its focus on simplicity, flexibility, and cost-effectiveness. Here are some of its key advantages:
1. Zero Cost
Relay is completely free to use. There are no subscription fees, no usage charges, and no hidden costs. This makes it an excellent choice for startups, small businesses, and individual developers who want to build powerful APIs without breaking the bank.
2. Easy to Set Up
With minimal dependencies and a straightforward configuration, Relay can be up and running in minutes. It doesn't require complex setup processes or dependencies on other services, making it accessible to developers of all skill levels.
3. Highly Customizable
Relay is designed to be highly customizable. You can extend its functionality using Go plugins, allowing you to add new features or integrate with other services as needed. This flexibility makes it a versatile solution for a wide range of use cases.
4. Performance-Oriented
Built in Go, Relay is designed for performance. It handles high traffic loads efficiently and ensures that your API remains responsive even under heavy load. This makes it a reliable choice for applications of all sizes.
5. Open Source
As an open-source project, Relay is transparent and community-driven. This means it's constantly being improved and updated by a team of passionate developers. Plus, you can contribute to its development if you have ideas for new features or improvements.
Getting Started with Relay
If you're interested in trying out Relay, here's a quick guide to get you started:
1. Install Go
Make sure you have Go installed on your system. You can download it from the official Go website.
2. Clone the Repository
Clone the Relay repository from GitHub:
git clone https://github.com/mohitdebian/relay.git
cd relay
3. Build and Run
Build the Relay binary:
go build -o relay
Run the Relay server:
./relay
By default, Relay listens on port 8080. You can configure it to listen on a different port by setting the port parameter in the configuration file.
4. Configure Your Backend
Update the configuration file to point to your backend service. For example:
port: 8080
services:
- name: "user-service"
url: "http://localhost:3000"
5. Test Your API
Make a request to your API using a tool like curl:
curl -H "X-API-Key: your-api-key" http://localhost:8080/user
If everything is set up correctly, you should receive a response from your backend service.
Conclusion: A Better Way to Build APIs
Setting up an API gateway shouldn't be a nightmare. With Relay, you can expose your API to the internet securely, efficiently, and at no cost. By focusing on simplicity, flexibility, and performance, Relay provides a refreshing alternative to the bloated and expensive solutions currently on the market.
If you're tired of wrestling with complex API gateways and high costs, give Relay a try. It might just be the solution you've been looking for.
Takeaway
Relay offers a simple, flexible, and cost-effective alternative to traditional API gateways. By leveraging Go's performance and concurrency capabilities, it provides a powerful yet lightweight solution for exposing your API to the internet. Whether you're a startup, a small business, or an individual developer, Relay can help you build and scale your API without the headaches and high costs of other solutions.