1 Star 1 Fork 0

fast_api / api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
def.go 1.86 KB
一键复制 编辑 原始数据 按行查看 历史
safe 提交于 2023-10-27 15:00 . support http3
package api
import (
"crypto/tls"
"net/http"
"os"
"gitee.com/fast_api/api/def"
"gitee.com/fast_api/api/dwarf"
ihttp "gitee.com/fast_api/api/http"
"gitee.com/fast_api/api/log"
)
type Optional func(conf *ServerConfig)
var (
_ http.Handler = (*ServerMain)(nil)
defaultConf = ServerConfig{
dwarf: dwarf.NewDwarfMaker(),
listen: "0.0.0.0:8080",
}
)
func loadTls(s *ServerConfig) (tconf *tls.Config, err error) {
certs := make([]tls.Certificate, 1)
certs[0], err = tls.X509KeyPair(defaultConf.certPEMBlock, defaultConf.keyPEMBlock)
if err != nil {
log.Error(err)
return nil, err
}
return &tls.Config{
Certificates: certs,
}, err
}
type ServerMain struct {
maker *dwarf.DwarfMaker
pool *def.MethodsPools
}
func (ad *ServerMain) Maker() *dwarf.DwarfMaker {
return ad.maker
}
func NewServer(pool *def.MethodsPools) *ServerMain {
return &ServerMain{pool: pool}
}
func (ad *ServerMain) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ihttp.DoHttp(rw, req)
}
func WithListen(s string) Optional {
return func(conf *ServerConfig) {
conf.listen = s
}
}
func WithTLS(certPEMBlock, keyPEMBlock []byte) Optional {
return func(conf *ServerConfig) {
conf.certPEMBlock = certPEMBlock
conf.keyPEMBlock = keyPEMBlock
}
}
func WithTLSFile(certFile, keyFile string) Optional {
return func(conf *ServerConfig) {
certPEMBlock, err := os.ReadFile(certFile)
if err != nil {
log.Error(err)
return
}
keyPEMBlock, err := os.ReadFile(keyFile)
if err != nil {
log.Error(err)
return
}
conf.certPEMBlock = certPEMBlock
conf.keyPEMBlock = keyPEMBlock
}
}
func WithCa(caPEMBlock []byte) Optional {
return func(conf *ServerConfig) {
conf.caPEMBlock = caPEMBlock
}
}
func apply(s *ServerConfig, ops ...Optional) {
if ops == nil {
return
}
for i := 0; i < len(ops); i++ {
if ops[i] != nil {
ops[i](s)
}
}
}
1
https://gitee.com/fast_api/api.git
git@gitee.com:fast_api/api.git
fast_api
api
api
master

搜索帮助