Skip to content

Commit

Permalink
refactor: clean code for part:gin start
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Mar 10, 2023
1 parent 0c3ca08 commit 282b37a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
58 changes: 58 additions & 0 deletions code/initialization/gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package initialization

import (
"crypto/tls"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"time"
)

func loadCertificate(config Config) (cert tls.Certificate, err error) {
cert, err = tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
return cert, fmt.Errorf("failed to load certificate: %v", err)
}
// check certificate expiry
certExpiry := cert.Leaf.NotAfter
if certExpiry.Before(time.Now()) {
return cert, fmt.Errorf("certificate expired on %v", certExpiry)
}
return cert, nil
}
func startHTTPServer(config Config, r *gin.Engine) (err error) {
log.Printf("http server started: http://localhost:%d/webhook/event\n", config.HttpPort)
err = r.Run(fmt.Sprintf(":%d", config.HttpPort))
if err != nil {
return fmt.Errorf("failed to start http server: %v", err)
}
return nil
}
func startHTTPSServer(config Config, r *gin.Engine) (err error) {
cert, err := loadCertificate(config)
if err != nil {
return fmt.Errorf("failed to load certificate: %v", err)
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", config.HttpsPort),
Handler: r,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
fmt.Printf("https server started: https://localhost:%d/webhook/event\n", config.HttpsPort)
err = server.ListenAndServeTLS("", "")
if err != nil {
return fmt.Errorf("failed to start https server: %v", err)
}
return nil
}
func StartServer(config Config, r *gin.Engine) (err error) {
if config.UseHttps {
err = startHTTPSServer(config, r)
} else {
err = startHTTPServer(config, r)
}
return err
}
39 changes: 6 additions & 33 deletions code/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package main

import (
"context"
"crypto/tls"
"fmt"
"net/http"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
"log"
"start-feishubot/handlers"
"start-feishubot/initialization"
"start-feishubot/services"

larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"

larkcard "github.com/larksuite/oapi-sdk-go/v3/card"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -57,33 +54,9 @@ func main() {
sdkginext.NewCardActionHandlerFunc(
cardHandler))

if config.UseHttps {
certFile := config.GetCertFile()
keyFile := config.GetKeyFile()

cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
panic(err)
}

server := &http.Server{
Addr: fmt.Sprintf(":%d", config.HttpsPort),
Handler: r,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}

fmt.Printf("https server started: https://localhost:%d/webhook/event\n", config.HttpsPort)
err = server.ListenAndServeTLS("", "")
if err != nil {
panic(err)
}
} else {
fmt.Printf("http server started: http://localhost:%d/webhook/event\n", config.HttpPort)
err := r.Run(fmt.Sprintf(":%d", config.HttpPort))
if err != nil {
panic(err)
}
err := initialization.StartServer(*config, r)
if err != nil {
log.Fatalf("failed to start server: %v", err)
}

}

0 comments on commit 282b37a

Please sign in to comment.