1 Star 0 Fork 0

Jorzion/vodka

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
listen.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
Jorzion 提交于 2021-08-10 09:59 . init
// Package vodka is a high productive and modular web framework in Golang.
package vodka
import (
"log"
"os"
"runtime"
"strconv"
"strings"
)
func (m *Vodka) Listen(args ...interface{}) {
addr := GetAddress(args...)
if runtime.NumCPU() > 1 {
runtime.GOMAXPROCS(runtime.NumCPU())
} else {
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
}
m.DoActionHook("VodkaListen")
m.Server.Addr = addr
log.Fatal(m.Server.ListenAndServe())
}
func (m *Vodka) ListenTLS(certFile, keyFile string, args ...interface{}) {
addr := GetAddress(args...)
if runtime.NumCPU() > 1 {
runtime.GOMAXPROCS(runtime.NumCPU())
} else {
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
}
m.DoActionHook("VodkaListenTLS")
m.Server.Addr = addr
log.Fatal(m.Server.ListenAndServeTLS(certFile, keyFile))
}
func GetAddress(args ...interface{}) string {
var host string
var port int
if len(args) == 1 {
switch arg := args[0].(type) {
case string:
addrs := strings.Split(args[0].(string), ":")
if len(addrs) == 1 {
host = addrs[0]
} else if len(addrs) >= 2 {
host = addrs[0]
_port, _ := strconv.ParseInt(addrs[1], 10, 0)
port = int(_port)
}
case int:
port = arg
case int64:
port = int(arg)
}
} else if len(args) >= 2 {
if arg, ok := args[0].(string); ok {
host = arg
}
if arg, ok := args[1].(int); ok {
port = arg
}
}
if iHost := os.Getenv("HOST"); len(iHost) != 0 {
host = iHost
} else if len(host) == 0 {
host = "0.0.0.0"
}
if iPort, _ := strconv.ParseInt(os.Getenv("PORT"), 10, 32); iPort != 0 {
port = int(iPort)
} else if port == 0 {
port = 8000
}
addr := host + ":" + strconv.FormatInt(int64(port), 10)
return addr
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jorzion/vodka.git
git@gitee.com:jorzion/vodka.git
jorzion
vodka
vodka
master

搜索帮助