This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new c5a4ad9  feat: Set up and test the HTTPS certificate, and refactor the 
`main.go` code (#196)
c5a4ad9 is described below

commit c5a4ad999223d2ad68a8ce485e46164d7700f39a
Author: lltgo <[email protected]>
AuthorDate: Thu Feb 9 15:18:22 2023 +0800

    feat: Set up and test the HTTPS certificate, and refactor the `main.go` 
code (#196)
---
 pitr/agent/Makefile |  7 +++++++
 pitr/agent/main.go  | 37 +++++++++++++++++++++----------------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/pitr/agent/Makefile b/pitr/agent/Makefile
index e69de29..b31ca7f 100644
--- a/pitr/agent/Makefile
+++ b/pitr/agent/Makefile
@@ -0,0 +1,7 @@
+.PHONY:openssl-local
+openssl-local:
+       mkdir -p certs && \
+       cd certs  && \
+       openssl req -new -SHA256 -newkey rsa:2048 -nodes -keyout tls.key -out 
tls.csr -subj "/C=CN/ST=beijing/L=beijing/O=/OU=/" && \
+       openssl x509 -req -sha256 -days 365 -in tls.csr -signkey tls.key -out 
tls.crt
+
diff --git a/pitr/agent/main.go b/pitr/agent/main.go
index 9ce1a7e..e9d4324 100644
--- a/pitr/agent/main.go
+++ b/pitr/agent/main.go
@@ -22,6 +22,7 @@ import (
        "fmt"
        "os"
        "os/signal"
+       "strings"
        "syscall"
 
        "github.com/apache/pitr/agent/internal/handler"
@@ -48,17 +49,26 @@ var (
 var (
        logLevel string
        port     string
+       tlsCrt   string
+       tlsKey   string
 )
 
 func init() {
-       // TODO 参数全部通过 flag 输入
-       flag.StringVar(&logLevel, "logLevel", "info", "optional:log 
level,option values:info or debug,info is default")
-       flag.StringVar(&port, "port", "8888", "optional:8888 is default")
+       // 参数通过 flag 输入
+       flag.StringVar(&logLevel, "logLevel", "info", "optional:log 
level,option values:info or debug")
+       flag.StringVar(&port, "port", "443", "optional:443 is default")
+
+       flag.StringVar(&tlsCrt, "tlsCrt", "", "Require:TLS certificate file 
path")
+       flag.StringVar(&tlsKey, "tlsKey", "", "Require:TLS key file path")
 }
 
 func main() {
        flag.Parse()
 
+       if strings.Trim(tlsCrt, " ") == "" || strings.Trim(tlsKey, " ") == "" {
+               panic(fmt.Errorf("lack of HTTPs certificate"))
+       }
+
        var level = zapcore.InfoLevel
        if logLevel == debugLogLevel {
                level = zapcore.DebugLevel
@@ -89,11 +99,16 @@ func main() {
        signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
        <-quit
 
-       WindUp()
+       if app != nil {
+               if err := app.Shutdown(); err != nil {
+                       log.Field(logging.ErrorKey, err.Error()).Error("http 
app closed failure")
+               }
+       }
+       log.Info("app windup successfully.")
        log.Info("app has exited...")
 }
 
-// Serve run an http server on the specified port.
+// Serve run a http server on the specified port.
 func Serve(port string) error {
        app.Use(
                middleware.Recover(logging.Log()),
@@ -122,15 +137,5 @@ func Serve(port string) error {
                return responder.NotFound(ctx, "API not found")
        })
 
-       return app.Listen(fmt.Sprintf(":%s", port))
-}
-
-func WindUp() {
-       if app != nil {
-               if err := app.Shutdown(); err != nil {
-                       log.Field(logging.ErrorKey, err.Error()).Error("http 
app closed failure")
-               }
-       }
-
-       log.Info("app windup successfully.")
+       return app.ListenTLS(fmt.Sprintf(":%s", port), tlsCrt, tlsKey)
 }

Reply via email to