2 Star 0 Fork 0

websample / webserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
option.go 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
wuchao 提交于 2021-09-14 10:26 . 添加部分注解
/*
Copyright 2020 KubeSphere Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package options
import (
"crypto/tls"
"gitee.com/websample/webserver/pkg/apiserver"
"net/http"
"fmt"
flag "github.com/spf13/pflag"
)
type ServerRunOptions struct {
// server bind address
BindAddress string
// insecure port number
InsecurePort int
// secure port number
SecurePort int
// tls cert file
TlsCertFile string
// tls private key file
TlsPrivateKey string
//
DebugMode bool
}
// NewServerRunOptions 初始化server 启动参数
func NewServerRunOptions() *ServerRunOptions {
s := ServerRunOptions{
BindAddress: "0.0.0.0",
InsecurePort: 9090,
SecurePort: 0,
TlsCertFile: "",
TlsPrivateKey: "",
}
return &s
}
// AddFlags 添加程序入参
func (s *ServerRunOptions) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&s.BindAddress, "bind-address", s.BindAddress, "server bind address")
fs.IntVar(&s.InsecurePort, "insecure-port", s.InsecurePort, "insecure port number")
fs.IntVar(&s.SecurePort, "secure-port", s.SecurePort, "secure port number")
fs.StringVar(&s.TlsCertFile, "tls-cert-file", s.TlsCertFile, "tls cert file")
fs.StringVar(&s.TlsPrivateKey, "tls-private-key", s.TlsPrivateKey, "tls private key")
}
// NewAPIServer creates an APIServer instance using given options
func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIServer, error) {
apiServer := &apiserver.APIServer{}
server := &http.Server{
Addr: fmt.Sprintf(":%d", s.InsecurePort),
}
if s.SecurePort != 0 {
certificate, err := tls.LoadX509KeyPair(s.TlsCertFile, s.TlsPrivateKey)
if err != nil {
return nil, err
}
server.TLSConfig.Certificates = []tls.Certificate{certificate}
}
apiServer.Server = server
return apiServer, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/websample/webserver.git
git@gitee.com:websample/webserver.git
websample
webserver
webserver
bd184db6fa25

搜索帮助

344bd9b3 5694891 D2dac590 5694891