Help us shape the future of Go! Take the 2026 Go Developer Survey

serve

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 4 Imported by: 0

README

serve | 完全基于 Go net/http 的 Web 框架

轻量, 原生, 贴近标准库

其实就是封装了标准库的功能, 仅此而已

开始使用

go get -u github.com/UnknownMp391/serve

特性

分组路由, 方法路由, 路由参数, 中间件

使用 Go 1.22+ 的增强路由特性

配合 BetterMux 的分组, 中间件语法糖包装

实现对基础路由需求的支持

s := serve.NewServe()

s.Get("/{$}", func(ctx *serve.ServeContext) {
    ctx.WriteText("Hello World!")
})

s.Get("/greet/{name}", func(ctx *serve.ServeContext) {
    ctx.WriteText("Hello "+ctx.Request.PathValue("name"))
})

s.With(func (h *http.Handler) {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Println(">> Request")
        h.ServeHTTP(w, r)
    })
}).Get("/logged", func(ctx *serve.ServeContext) {
    ctx.WriteText("Hello with log")
})

http.ListenAndServe(":8080", s)
自动 JSON 解码, 编码

在自定义请求中封装 json.{NewDecoder,NewEncoder} 为 ReadJson(any), WriteJson(any) 实现快捷 JSON 解码和编码

s.Post("/greet", func(ctx *serve.ServeContext) {
    data := ctx.ReadText()

    ctx.WriteText("Hello "+data)
})

type CreateUserReq struct {
    Name string `json:"name"`
}

type CreateUserResp struct {
    Name string `json:"name"`
    Id int `json:"id"`
}

s.Post("/user", func(ctx *serve.ServeContext) {
    req := CreateUserReq{}

    ctx.ReadJson(*req)

    ctx.WriteJson(&CreateUserResp{
        Id: 1,
        Name: req.Name
    })
}
依赖注入 (全局单例, 请求单例)

通过 github.com/UnknownMp391/serve/dependency 提供

使用 Go 1.18+ 的泛型

基于 map 实现的简单 DI

使用 dependency.Dependency 创建一个依赖

通过 dependency.Provide(ctx.{Signletion,Scoped}, OneProvider) 获得依赖

var OtherProvider dependency.Dependency[T] = dependency.Dependency[T]{
    Identifier: "otherid",
    Resolver: func(s *dependency.DependencyManager) (T, error) {
        return result, nil
    },
}

var OneProvider dependency.Dependency[T] = dependency.Dependency[T]{
    Identifier: "id",
    Resolver: func(s *dependency.DependencyManager) (T, error) {

        otherDep := dependency.Provide(s.Singletion, OtherProvider)

        // some logic...

        return result, nil
    },
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Serve

type Serve struct {
	Mux            *bettermux.BetterMux
	SingletionDeps *betterdep.DependencyManager
}

func NewServe

func NewServe() *Serve

func (*Serve) Delete

func (s *Serve) Delete(pattern string, handler ServeHandlerFunc)

func (*Serve) Get

func (s *Serve) Get(pattern string, handler ServeHandlerFunc)

func (*Serve) HandleFunc

func (s *Serve) HandleFunc(pattern string, handler ServeHandlerFunc)

func (*Serve) Options

func (s *Serve) Options(pattern string, handler ServeHandlerFunc)

func (*Serve) Patch

func (s *Serve) Patch(pattern string, handler ServeHandlerFunc)

func (*Serve) Post

func (s *Serve) Post(pattern string, handler ServeHandlerFunc)

func (*Serve) Put

func (s *Serve) Put(pattern string, handler ServeHandlerFunc)

func (*Serve) Route

func (s *Serve) Route(pattern string, h func(*Serve)) *Serve

func (*Serve) ServeHTTP

func (s *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Serve) With

func (s *Serve) With(h func(sdm *betterdep.DependencyManager) func(http.Handler) http.Handler) *Serve

type ServeContext

type ServeContext struct {
	betterwr.BetterWR
	SingletionDeps *betterdep.DependencyManager
	ScopedDeps     *betterdep.DependencyManager
	Request        *http.Request
	ResponseWriter http.ResponseWriter
}

type ServeHandlerFunc

type ServeHandlerFunc func(ctx *ServeContext)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL