一个多层次的可扩展Go HTTP框架
Find a file
WJQSERVER ee0ebc986c
Merge pull request #54 from infinite-iroha/dev
context added FileText method
2025-10-21 15:06:39 +08:00
.github Merge pull request #43 from infinite-iroha/dev 2025-07-24 16:41:14 +08:00
licenses fix cfdt 2025-07-25 00:35:12 +08:00
.gitignore init(v0.0.1) 2025-05-28 18:25:28 +08:00
about-touka.md update about 2025-07-26 18:51:30 +08:00
adapter.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
context.go fix path to filepath 2025-10-21 15:06:26 +08:00
ecw.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
engine.go remove too much log print 2025-07-27 16:34:46 +08:00
fileserver.go fix StaticFS 2025-09-14 08:24:01 +08:00
go.mod update 2025-10-12 15:47:02 +08:00
go.sum update 2025-10-12 15:47:02 +08:00
LICENSE updaten license 2025-06-10 21:39:37 +08:00
logreco.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
maxreader.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
mergectx.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
midware_x.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
path.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
path_test.go use new resolveRoutePath replace path.Join && add UseIf 2025-06-17 14:20:14 +08:00
README.md update about 2025-07-26 18:51:30 +08:00
recovery.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
respw.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
serve.go add RunShutdownWithContext 2025-07-31 20:18:24 +08:00
sse.go fix 2025-09-10 02:40:41 +08:00
testutil.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
touka.go feat: add MPL 2.0 license headers to all go files 2025-07-24 08:07:38 +00:00
tree.go update tree 2025-08-01 10:21:32 +08:00
tree_test.go update tree 2025-08-01 10:21:32 +08:00

Touka(灯花)框架

Touka(灯花) 是一个基于 Go 语言构建的多层次、高性能 Web 框架。其设计目标是为开发者提供更直接的控制、有效的扩展能力,以及针对特定场景的行为优化

想深入了解 Touka 吗?请阅读我们的 -> 深度指南 (about-touka.md)

这份深度指南包含了对框架设计哲学、核心功能(路由、上下文、中间件、错误处理等)的全面剖析,并提供了大量可直接使用的代码示例,帮助您快速上手并精通 Touka。

快速上手

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/fenthope/reco"
	"github.com/infinite-iroha/touka"
)

func main() {
	r := touka.Default() // 使用带 Recovery 中间件的默认引擎

	// 配置日志记录器 (可选)
	logConfig := reco.Config{
		Level:      reco.LevelDebug,
		Mode:       reco.ModeText,
		Output:     os.Stdout,
		Async:      true,
	}
	r.SetLoggerCfg(logConfig)

	// 配置统一错误处理器
	r.SetErrorHandler(func(c *touka.Context, code int, err error) {
		c.JSON(code, touka.H{"error_code": code, "message": http.StatusText(code)})
		c.GetLogger().Errorf("发生HTTP错误: %d, 路径: %s, 错误: %v", code, c.Request.URL.Path, err)
	})

	// 注册路由
	r.GET("/hello/:name", func(c *touka.Context) {
		name := c.Param("name")
		query := c.DefaultQuery("mood", "happy")
		c.String(http.StatusOK, "Hello, %s! You seem %s.", name, query)
	})

	// 启动服务器 (支持优雅关闭)
	log.Println("Touka Server starting on :8080...")
	if err := r.RunShutdown(":8080", 10*time.Second); err != nil {
		log.Fatalf("Touka server failed to start: %v", err)
	}
}

中间件支持

内置

  • Recovery: r.Use(touka.Recovery()) (已包含在 touka.Default() 中)

第三方 (fenthope)

文档与贡献

相关项目

  • gin: Touka 在路由和 API 设计上参考了 Gin。
  • reco: Touka 框架的默认日志库。
  • httpc: 一个现代化且易用的 HTTP Client作为 Touka 框架 Context 携带的 HTTPC。

许可证

本项目基于 Mozilla Public License, v. 2.0 许可。

tree.go 部分代码源自 ginhttprouter,其原始许可为 BSD-style。