mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 09:16:36 +08:00
Improve [Startup Server] Support TLS
- [+] feat(init.go): add TLS certificate and key command line flags - [+] feat(main.go): implement TLS support for the server with fallback to environment variables - [+] fix(main.go): improve logging messages for server start and error handling
This commit is contained in:
parent
8df4a2670b
commit
1168c326c7
@ -3,11 +3,12 @@ package common
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/songquanpeng/one-api/common/config"
|
|
||||||
"github.com/songquanpeng/one-api/common/logger"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
|
"github.com/songquanpeng/one-api/common/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -15,13 +16,15 @@ var (
|
|||||||
PrintVersion = flag.Bool("version", false, "print version and exit")
|
PrintVersion = flag.Bool("version", false, "print version and exit")
|
||||||
PrintHelp = flag.Bool("help", false, "print help and exit")
|
PrintHelp = flag.Bool("help", false, "print help and exit")
|
||||||
LogDir = flag.String("log-dir", "./logs", "specify the log directory")
|
LogDir = flag.String("log-dir", "./logs", "specify the log directory")
|
||||||
|
TLSCertFile = flag.String("tls-cert", "", "path to TLS certificate file")
|
||||||
|
TLSKeyFile = flag.String("tls-key", "", "path to TLS key file")
|
||||||
)
|
)
|
||||||
|
|
||||||
func printHelp() {
|
func printHelp() {
|
||||||
fmt.Println("One API " + Version + " - All in one API service for OpenAI API.")
|
fmt.Println("One API " + Version + " - All in one API service for OpenAI API.")
|
||||||
fmt.Println("Copyright (C) 2023 JustSong. All rights reserved.")
|
fmt.Println("Copyright (C) 2023 JustSong. All rights reserved.")
|
||||||
fmt.Println("GitHub: https://github.com/songquanpeng/one-api")
|
fmt.Println("GitHub: https://github.com/songquanpeng/one-api")
|
||||||
fmt.Println("Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]")
|
fmt.Println("Usage: one-api [--port <port>] [--log-dir <log directory>] [--tls-cert <path>] [--tls-key <path>] [--version] [--help]")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
|
26
main.go
26
main.go
@ -116,9 +116,29 @@ func main() {
|
|||||||
if port == "" {
|
if port == "" {
|
||||||
port = strconv.Itoa(*common.Port)
|
port = strconv.Itoa(*common.Port)
|
||||||
}
|
}
|
||||||
logger.SysLogf("server started on http://localhost:%s", port)
|
|
||||||
err = server.Run(":" + port)
|
// Check for TLS configuration - prioritize command line flags over environment variables
|
||||||
|
certFile := *common.TLSCertFile
|
||||||
|
keyFile := *common.TLSKeyFile
|
||||||
|
|
||||||
|
// If flags are not set, fallback to environment variables
|
||||||
|
if certFile == "" {
|
||||||
|
certFile = os.Getenv("TLS_CERT_FILE")
|
||||||
|
}
|
||||||
|
if keyFile == "" {
|
||||||
|
keyFile = os.Getenv("TLS_KEY_FILE")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the server with or without TLS
|
||||||
|
if certFile != "" && keyFile != "" {
|
||||||
|
logger.SysLogf("server started with TLS on https://localhost:%s", port)
|
||||||
|
err = server.RunTLS(":"+port, certFile, keyFile)
|
||||||
|
} else {
|
||||||
|
logger.SysLogf("server started on http://localhost:%s", port)
|
||||||
|
err = server.Run(":" + port)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.FatalLog("failed to start HTTP server: " + err.Error())
|
logger.FatalLog("failed to start server: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user