Skip to content

Commit

Permalink
Add tls completed (caoyingjunz#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyingjunz authored Dec 16, 2024
1 parent 0ad655b commit 10eff7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
25 changes: 25 additions & 0 deletions cmd/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package config

import (
"fmt"

"github.com/caoyingjunz/pixiu/pkg/jobmanager"
logutil "github.com/caoyingjunz/pixiu/pkg/util/log"
)
Expand All @@ -37,6 +39,7 @@ type Config struct {
Mysql MysqlOptions `yaml:"mysql"`
Worker WorkerOptions `yaml:"worker"`
Audit jobmanager.AuditOptions `yaml:"audit"`
TLS *TLS `yaml:"tls"`
}

type DefaultOptions struct {
Expand Down Expand Up @@ -86,6 +89,25 @@ func (w WorkerOptions) Valid() error {
return nil
}

type TLS struct {
CertFile string `yaml:"cert_file"`
KeyFile string `yaml:"key_file"`
}

func (t *TLS) Valid() error {
if t != nil {
if len(t.CertFile) == 0 {
return fmt.Errorf("listen on tls, no cert_file found")
}

if len(t.KeyFile) == 0 {
return fmt.Errorf("listen on tls, no key_file found")
}
}

return nil
}

func (c *Config) Valid() (err error) {
if err = c.Default.Valid(); err != nil {
return
Expand All @@ -96,6 +118,9 @@ func (c *Config) Valid() (err error) {
if err = c.Worker.Valid(); err != nil {
return
}
if err = c.TLS.Valid(); err != nil {
return err
}

return
}
11 changes: 9 additions & 2 deletions cmd/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ func Run(opt *options.Options) error {

// Initializing the server in a goroutine so that it won't block the graceful shutdown handling below
go func() {
klog.Info("starting pixiu server")
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
var err error
if opt.ComponentConfig.TLS != nil {
klog.Info("starting pixiu server with TLS")
err = srv.ListenAndServeTLS(opt.ComponentConfig.TLS.CertFile, opt.ComponentConfig.TLS.KeyFile)
} else {
klog.Info("starting pixiu server with no TLS")
err = srv.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
klog.Fatal("failed to listen pixiu server: ", err)
}
}()
Expand Down
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ default:
log_format: json
log_level: info

#tls:
# cert_file: test.pem
# key_file: test.key

# 数据库地址信息
mysql:
host: peng
Expand Down

0 comments on commit 10eff7a

Please sign in to comment.