-
Notifications
You must be signed in to change notification settings - Fork 960
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from aaakoako/https
feat: support https and multi openai-keys to reduce access pressure
- Loading branch information
Showing
14 changed files
with
335 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ docker.md | |
# Mac OS | ||
.DS_Store | ||
**/.DS_Store | ||
*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/apikey_usage.json | ||
*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
# 飞书 | ||
# 飞书 # 不要随意修改example中配置的顺序和空格,否则docker的sed脚本会执行出错 | ||
APP_ID: cli_axxx | ||
APP_SECRET: xxx | ||
APP_ENCRYPT_KEY: xxx | ||
APP_VERIFICATION_TOKEN: xxx | ||
# 请确保和飞书应用管理平台中的设置一致 | ||
# 请确保和飞书应用管理平台中的设置一致 | ||
BOT_NAME: chatGpt | ||
# openAI | ||
OPENAI_KEY: sk-xxx | ||
# openAI key 支持负载均衡 可以填写多个key 用逗号分隔 | ||
OPENAI_KEY: sk-xxx,sk-xxx,sk-xxx | ||
# 服务器配置 | ||
HTTP_PORT: 9000 | ||
HTTPS_PORT: 9001 | ||
USE_HTTPS: false | ||
CERT_FILE: cert.pem | ||
KEY_FILE: key.pem | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.