gofr README

项目的 README,来自 github.com/gofr-dev/gofr。合并为一个 markdown 文件:README.md。

GoFr

logo

GoFr: An Opinionated Microservice Development Framework

godoc gofr-docs Maintainability Code Coverage Go Report Card Apache 2.0 License discord

Listed in the CNCF Landscape

🎯 Goal

GoFr is designed to simplify microservice development, with a key focus on Kubernetes deployment and out-of-the-box observability. While capable of building generic applications, microservices remain at its core.


💡 Key Features

  1. Simple API Syntax
  2. REST Standards by Default
  3. Configuration Management
  4. Observability (Logs, Traces, Metrics)
  5. Inbuilt Auth Middleware & Custom Middleware Support
  6. gRPC Support
  7. HTTP Service with Circuit Breaker Support
  8. Pub/Sub
  9. Health Check for All Datasources
  10. Database Migration
  11. Cron Jobs
  12. Support for Changing Log Level Without Restarting
  13. Swagger Rendering
  14. Abstracted File Systems
  15. Websockets
  16. LLM Integration (app.AddLLM, ctx.LLM()) with Built-in Tracing, Token Metrics and Streaming
  17. MCP Server (app.EnableMCP) to Expose Read-Only Handlers as Tools for AI Agents

🚀 Getting Started

Prerequisites

Installation

To get started with GoFr, add the following import to your code and use Go’s module support to automatically fetch dependencies:

import "gofr.dev/pkg/gofr"

Alternatively, use the command:

go get -u gofr.dev/pkg/gofr

🏃 Running GoFr

Here's a simple example to get a GoFr application up and running:

package main

import "gofr.dev/pkg/gofr"

func main() {
	app := gofr.New()

	app.GET("/greet", func(ctx *gofr.Context) (any, error) {
		return "Hello World!", nil
	})

	app.Run() // listens and serves on localhost:8000
}

To run this code:

$ go run main.go

Visit localhost:8000/greet to see the result.


🤖 AI Features

GoFr treats LLMs and AI agents like any other datasource: one line to set them up, with tracing, metrics, logs and health checks wired in automatically.

Call an LLM from a handler

app.AddLLM registers an OpenAI-compatible model (OpenAI, Groq, DeepSeek, Together, Ollama, or any endpoint via BaseURL). Handlers reach it through ctx.LLM(), which also offers Chat, Stream and Embed.

package main

import (
	"gofr.dev/pkg/gofr"
	"gofr.dev/pkg/gofr/ai/llm"
)

func main() {
	app := gofr.New()

	// The API key is read from LLM_API_KEY, or GROQ_API_KEY for this provider.
	app.AddLLM(&llm.Client{Provider: llm.Groq, Model: "llama-3.3-70b-versatile"})

	app.POST("/summarize", func(ctx *gofr.Context) (any, error) {
		var in struct {
			Text string `json:"text"`
		}

		if err := ctx.Bind(&in); err != nil {
			return nil, err
		}

		return ctx.LLM().Generate(ctx, "Summarize the following:\n"+in.Text)
	})

	app.Run()
}

A model can also be configured without code, through LLM_PROVIDER, LLM_MODEL and LLM_API_KEY in configs/.env. See the LLM guide.

Expose handlers to AI agents

app.EnableMCP serves your read-only handlers (GET, HEAD, OPTIONS and QUERY) as Model Context Protocol tools, so MCP clients such as Claude Code or Claude Desktop can discover and call them. Write handlers are never exposed. The MCP server listens on loopback at MCP_PORT (default 8200; 0 turns the transport off).

app := gofr.New()

app.EnableMCP()

app.GET("/products/{id}", getProduct)

app.Run()

ctx.LLM().Tools() gives a handler the same tools in-process, so you can build your own agent loop. See Building AI Agents and the using-ai example.


📂 More Examples

Explore a variety of ready-to-run examples in the GoFr examples directory.


👩‍💻 Documentation


👍 Contribute

Join Us in Making GoFr Better

Share your experience: If you’ve found GoFr helpful, consider writing a review or tutorial on platforms like Medium, Dev.to, or your personal blog. Your insights could help others get started faster!

Contribute to the project: Want to get involved? Check out our CONTRIBUTING.md guide to learn how you can contribute code, suggest improvements, or report issues.


🔒 Secure Cloning

To securely clone the GoFr repository, you can use HTTPS or SSH:

Cloning with HTTPS

git clone https://github.com/gofr-dev/gofr.git

Cloning with SSH

git clone [email protected]:gofr-dev/gofr.git

🎁 Get a GoFr T-Shirt & Stickers!

If your PR is merged, or if you contribute by writing articles or promoting GoFr, we invite you to fill out this form to claim your GoFr merchandise as a token of our appreciation!

Partners

JetBrains logo

gofr (gofr-dev/gofr)

正在搜索数据库…

20.8K ·GitHub
这是什么?一个主打快速构建微服务的 Go 框架,风格偏规范化——内置数据库支持和可观测性(日志、链路追踪、指标),你只需写业务代码,不用自己拼基础设施。
GoFr 是整个服务都建立在它之上的框架,还是一个偶尔调用的库?是框架,而且是刻意偏规范化的框架:你需要按照 GoFr 的 API 来组织服务结构,路由、配置、中间件、数据源和可观测性都由它接管。
我怎样才能把第一个服务跑起来?安装 Go 1.26 或更高版本,在代码中添加 GoFr 导入——Go modules 会自动获取依赖——然后运行示例应用,打开 localhost:8000/greet 查看结果。
除了 Go 之外还需要安装别的东西吗?不需要单独安装:数据库和发布/订阅系统通过 GoFr 内置的数据源集成接入,而且每个数据源都会自动获得健康检查。
用这种偏规范化的框架有什么代价?你得接受它的约定和默认配置,而不是自己组装技术栈;虽然也能写通用应用,但 GoFr 其实是为微服务量身打造的。
GoFr 是给谁用的?面向构建微服务的 Go 开发者,尤其是部署在 Kubernetes 上的那些,希望从第一行代码起就接好日志、链路追踪和指标。
用它构建的服务能跑在哪里?README 没有操作系统限制:它是 Go 源码,Go 能跑的地方都能编译——开发时用笔记本,生产环境用 Linux 服务器或容器。
除了 URL 路由,它开箱还提供什么?相当多:默认遵循 REST 规范、配置管理、日志/链路追踪/指标、内置认证加自定义中间件、gRPC、HTTP 熔断器、发布/订阅、数据库迁移、定时任务、Swagger 渲染、websockets 和抽象文件系统——而且日志级别不用重启就能改。
为什么用偏规范化的框架,而不是自己组装 Go 库?它的目标就是简化微服务开发:不用手动接日志、链路追踪、指标、配置和数据库客户端,GoFr 把这些打包在一起并给出合理的默认值,让服务从第一天起就适合 Kubernetes 并且可观测。
这个项目有多成熟、多活跃?它是一个成熟的开源项目(Apache-2.0),2023 年 10 月启动,GitHub 上大约有 20.9k 星,持续活跃开发中,还参与 Hacktoberfest 来吸纳贡献者。
在哪里能看到能跑的示例代码和文档?仓库里有个 examples 目录,里面有可直接运行的示例;另外还有 GoDoc 作为 API 参考,以及一个专门的 GoFr 文档站,提供详尽的指南。
有哪些类似的项目?gf, kratos, go-zero, encore, traefik, echo, apisix, fiber
作者
gofr-dev
大小
32.1 MB
星标
20.8K
时长
3年
提交 · 90 天