2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
2018-04-13 15:19:31 +08:00
|
|
|
|
// 配置管理数据结构定义.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-12-07 14:57:16 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
2018-04-10 10:32:37 +08:00
|
|
|
|
"net/http"
|
2018-08-12 12:22:02 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2018-08-16 18:17:47 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/glog"
|
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
2017-12-07 14:57:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-04-27 22:12:11 +08:00
|
|
|
|
const (
|
2018-09-14 13:02:13 +08:00
|
|
|
|
gDEFAULT_HTTP_ADDR = ":80" // 默认HTTP监听地址
|
|
|
|
|
gDEFAULT_HTTPS_ADDR = ":443" // 默认HTTPS监听地址
|
|
|
|
|
NAME_TO_URI_TYPE_DEFAULT = 0 // 服务注册时对象和方法名称转换为URI时,全部转为小写,单词以'-'连接符号连接
|
|
|
|
|
NAME_TO_URI_TYPE_FULLNAME = 1 // 不处理名称,以原有名称构建成URI
|
|
|
|
|
NAME_TO_URI_TYPE_ALLLOWER = 2 // 仅转为小写,单词间不使用连接符号
|
|
|
|
|
NAME_TO_URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
|
|
|
|
|
gDEFAULT_COOKIE_PATH = "/" // 默认path
|
|
|
|
|
gDEFAULT_COOKIE_MAX_AGE = 86400*365 // 默认cookie有效期(一年)
|
|
|
|
|
gDEFAULT_SESSION_MAX_AGE = 600 // 默认session有效期(600秒)
|
|
|
|
|
gDEFAULT_SESSION_ID_NAME = "gfsessionid" // 默认存放Cookie中的SessionId名称
|
2018-04-27 22:12:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-09-14 13:02:13 +08:00
|
|
|
|
// 自定义日志处理方法类型
|
|
|
|
|
type LogHandler func(r *Request, error ... interface{})
|
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// HTTP Server 设置结构体,静态配置
|
2017-12-07 14:57:16 +08:00
|
|
|
|
type ServerConfig struct {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// 底层http对象配置
|
2018-08-16 18:17:47 +08:00
|
|
|
|
Addr string // 监听IP和端口,监听本地所有IP使用":端口"(支持多个地址,使用","号分隔)
|
|
|
|
|
HTTPSAddr string // HTTPS服务监听地址(支持多个地址,使用","号分隔)
|
|
|
|
|
HTTPSCertPath string // HTTPS证书文件路径
|
|
|
|
|
HTTPSKeyPath string // HTTPS签名文件路径
|
|
|
|
|
Handler http.Handler // 默认的处理函数
|
2018-09-14 13:02:13 +08:00
|
|
|
|
ReadTimeout time.Duration // 读取超时
|
|
|
|
|
WriteTimeout time.Duration // 写入超时
|
|
|
|
|
IdleTimeout time.Duration // 等待超时
|
2018-08-16 18:17:47 +08:00
|
|
|
|
MaxHeaderBytes int // 最大的header长度
|
2018-09-14 13:02:13 +08:00
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// 静态文件配置
|
2018-08-16 18:17:47 +08:00
|
|
|
|
IndexFiles []string // 默认访问的文件列表
|
|
|
|
|
IndexFolder bool // 如果访问目录是否显示目录列表
|
|
|
|
|
ServerAgent string // server agent
|
|
|
|
|
ServerRoot string // 服务器服务的本地目录根路径
|
2018-09-14 13:02:13 +08:00
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// COOKIE
|
|
|
|
|
CookieMaxAge int // Cookie有效期
|
2018-09-14 13:02:13 +08:00
|
|
|
|
CookiePath string // Cookie有效Path(注意同时也会影响SessionID)
|
|
|
|
|
CookieDomain string // Cookie有效Domain(注意同时也会影响SessionID)
|
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// SESSION
|
|
|
|
|
SessionMaxAge int // Session有效期
|
|
|
|
|
SessionIdName string // SessionId名称
|
2018-09-14 13:02:13 +08:00
|
|
|
|
|
2018-08-16 18:17:47 +08:00
|
|
|
|
// ip访问控制
|
|
|
|
|
DenyIps []string // 不允许访问的ip列表,支持ip前缀过滤,如: 10 将不允许10开头的ip访问
|
|
|
|
|
AllowIps []string // 仅允许访问的ip列表,支持ip前缀过滤,如: 10 将仅允许10开头的ip访问
|
|
|
|
|
// 路由访问控制
|
|
|
|
|
DenyRoutes []string // 不允许访问的路由规则列表
|
2018-09-14 13:02:13 +08:00
|
|
|
|
|
|
|
|
|
// 日志配置
|
|
|
|
|
LogPath string // 存放日志的目录路径
|
|
|
|
|
LogHandler LogHandler // 自定义日志处理回调方法
|
|
|
|
|
ErrorLogEnabled bool // 是否开启error log
|
|
|
|
|
AccessLogEnabled bool // 是否开启access log
|
|
|
|
|
|
|
|
|
|
// 其他设置
|
|
|
|
|
NameToUriType int // 服务注册时对象和方法名称转换为URI时的规则
|
2018-08-21 21:18:56 +08:00
|
|
|
|
GzipContentTypes []string // 允许进行gzip压缩的文件类型
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认HTTP Server
|
|
|
|
|
var defaultServerConfig = ServerConfig {
|
2018-08-21 21:18:56 +08:00
|
|
|
|
Addr : "",
|
|
|
|
|
HTTPSAddr : "",
|
|
|
|
|
Handler : nil,
|
|
|
|
|
ReadTimeout : 60 * time.Second,
|
|
|
|
|
WriteTimeout : 60 * time.Second,
|
|
|
|
|
IdleTimeout : 60 * time.Second,
|
|
|
|
|
MaxHeaderBytes : 1024,
|
|
|
|
|
IndexFiles : []string{"index.html", "index.htm"},
|
|
|
|
|
IndexFolder : false,
|
|
|
|
|
ServerAgent : "gf",
|
|
|
|
|
ServerRoot : "",
|
2018-08-12 12:22:02 +08:00
|
|
|
|
|
2018-08-21 21:18:56 +08:00
|
|
|
|
CookieMaxAge : gDEFAULT_COOKIE_MAX_AGE,
|
2018-09-14 13:02:13 +08:00
|
|
|
|
CookiePath : gDEFAULT_COOKIE_PATH,
|
|
|
|
|
CookieDomain : "",
|
|
|
|
|
|
2018-08-21 21:18:56 +08:00
|
|
|
|
SessionMaxAge : gDEFAULT_SESSION_MAX_AGE,
|
|
|
|
|
SessionIdName : gDEFAULT_SESSION_ID_NAME,
|
2018-08-12 12:22:02 +08:00
|
|
|
|
|
2018-08-21 21:18:56 +08:00
|
|
|
|
ErrorLogEnabled : true,
|
|
|
|
|
|
|
|
|
|
GzipContentTypes : defaultGzipContentTypes,
|
2017-12-07 14:57:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取默认的http server设置
|
2018-09-14 13:02:13 +08:00
|
|
|
|
func Config() ServerConfig {
|
2017-12-07 14:57:16 +08:00
|
|
|
|
return defaultServerConfig
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// http server setting设置
|
|
|
|
|
// 注意使用该方法进行http server配置时,需要配置所有的配置项,否则没有配置的属性将会默认变量为空
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetConfig(c ServerConfig) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
if c.Handler == nil {
|
|
|
|
|
c.Handler = http.HandlerFunc(s.defaultHttpHandle)
|
|
|
|
|
}
|
|
|
|
|
s.config = c
|
|
|
|
|
|
2018-09-14 13:02:13 +08:00
|
|
|
|
if c.LogPath != "" {
|
|
|
|
|
s.logger.SetPath(c.LogPath)
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - Addr
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetAddr(addr string) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.Addr = addr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - Port
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetPort(port...int) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("config cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
if len(port) > 0 {
|
|
|
|
|
s.config.Addr = ""
|
|
|
|
|
for _, v := range port {
|
|
|
|
|
if len(s.config.Addr) > 0 {
|
|
|
|
|
s.config.Addr += ","
|
|
|
|
|
}
|
|
|
|
|
s.config.Addr += ":" + strconv.Itoa(v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - HTTPS Addr
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetHTTPSAddr(addr string) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.HTTPSAddr = addr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - HTTPS Port
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetHTTPSPort(port...int) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
if len(port) > 0 {
|
|
|
|
|
s.config.HTTPSAddr = ""
|
|
|
|
|
for _, v := range port {
|
|
|
|
|
if len(s.config.HTTPSAddr) > 0 {
|
|
|
|
|
s.config.HTTPSAddr += ","
|
|
|
|
|
}
|
|
|
|
|
s.config.HTTPSAddr += ":" + strconv.Itoa(v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开启HTTPS支持,但是必须提供Cert和Key文件
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)EnableHTTPS(certFile, keyFile string) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.HTTPSCertPath = certFile
|
|
|
|
|
s.config.HTTPSKeyPath = keyFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - ReadTimeout
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetReadTimeout(t time.Duration) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.ReadTimeout = t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - WriteTimeout
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetWriteTimeout(t time.Duration) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.WriteTimeout = t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - IdleTimeout
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetIdleTimeout(t time.Duration) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.IdleTimeout = t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - MaxHeaderBytes
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetMaxHeaderBytes(b int) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.MaxHeaderBytes = b
|
2018-08-16 18:17:47 +08:00
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - IndexFiles,默认展示文件,如:index.html, index.htm
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetIndexFiles(index []string) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.IndexFiles = index
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 允许展示访问目录的文件列表
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetIndexFolder(index bool) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.IndexFolder = index
|
2018-08-16 18:17:47 +08:00
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - ServerAgent
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetServerAgent(agent string) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.ServerAgent = agent
|
2018-08-16 18:17:47 +08:00
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置http server参数 - ServerRoot
|
2018-08-16 18:17:47 +08:00
|
|
|
|
func (s *Server)SetServerRoot(root string) {
|
2018-08-12 12:22:02 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("cannot be changed while running")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
// RealPath的作用除了校验地址正确性以外,还转换分隔符号为当前系统正确的文件分隔符号
|
|
|
|
|
path := gfile.RealPath(root)
|
|
|
|
|
if path == "" {
|
2018-08-16 18:17:47 +08:00
|
|
|
|
glog.Error("invalid root path \"" + root + "\"")
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
s.config.ServerRoot = strings.TrimRight(path, string(gfile.Separator))
|
2018-08-16 18:17:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) SetDenyIps(ips []string) {
|
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
|
|
|
|
glog.Error("cannot be changed while running")
|
|
|
|
|
}
|
|
|
|
|
s.config.DenyIps = ips
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) SetAllowIps(ips []string) {
|
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
|
|
|
|
glog.Error("cannot be changed while running")
|
|
|
|
|
}
|
|
|
|
|
s.config.AllowIps = ips
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) SetDenyRoutes(routes []string) {
|
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
|
|
|
|
glog.Error("cannot be changed while running")
|
|
|
|
|
}
|
|
|
|
|
s.config.DenyRoutes = routes
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 19:56:01 +08:00
|
|
|
|
func (s *Server) SetGzipContentTypes(types []string) {
|
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
|
|
|
|
glog.Error("cannot be changed while running")
|
|
|
|
|
}
|
|
|
|
|
s.config.GzipContentTypes = types
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// 服务注册时对象和方法名称转换为URI时的规则
|
|
|
|
|
func (s *Server) SetNameToUriType(t int) {
|
2018-09-14 13:02:13 +08:00
|
|
|
|
if s.Status() == SERVER_STATUS_RUNNING {
|
|
|
|
|
glog.Error("cannot be changed while running")
|
|
|
|
|
}
|
|
|
|
|
s.config.NameToUriType = t
|
2018-08-12 12:22:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 19:42:30 +08:00
|
|
|
|
// 添加静态文件搜索目录,必须给定目录的绝对路径
|
|
|
|
|
func (s *Server) AddSearchPath(path string) error {
|
|
|
|
|
return s.paths.Add(path)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-12 12:22:02 +08:00
|
|
|
|
// 获取
|
|
|
|
|
func (s *Server) GetName() string {
|
|
|
|
|
return s.name
|
2018-09-14 13:02:13 +08:00
|
|
|
|
}
|