refactor: unified parameter

This commit is contained in:
Vincent Young 2023-11-27 14:50:32 -05:00
parent 365cf185a4
commit 5489e11113
No known key found for this signature in database
GPG Key ID: DD9998BCFD278F6A

103
main.go
View File

@ -2,7 +2,7 @@
* @Author: Vincent Young * @Author: Vincent Young
* @Date: 2023-07-01 21:45:34 * @Date: 2023-07-01 21:45:34
* @LastEditors: Vincent Young * @LastEditors: Vincent Young
* @LastEditTime: 2023-11-27 11:59:51 * @LastEditTime: 2023-11-27 14:49:02
* @FilePath: /DeepLX/main.go * @FilePath: /DeepLX/main.go
* @Telegram: https://t.me/missuo * @Telegram: https://t.me/missuo
* *
@ -36,17 +36,36 @@ var port int
var token string var token string
var authKey string var authKey string
func init() { type Config struct {
const ( Port int
defaultPort = 1188 Token string
usage = "set up the port to listen on" AuthKey string
) }
flag.IntVar(&port, "port", defaultPort, usage) func InitConfig() *Config {
flag.IntVar(&port, "p", defaultPort, usage) cfg := &Config{
flag.StringVar(&token, "token", "", "set the access token for /translate endpoint") Port: 1188,
}
log.SetFlags(log.LstdFlags | log.Lshortfile) flag.IntVar(&cfg.Port, "port", cfg.Port, "set up the port to listen on")
flag.IntVar(&cfg.Port, "p", cfg.Port, "set up the port to listen on")
flag.StringVar(&cfg.Token, "token", "", "set the access token for /translate endpoint")
if cfg.Token == "" {
if token, ok := os.LookupEnv("TOKEN"); ok {
cfg.Token = token
}
}
flag.StringVar(&cfg.AuthKey, "authkey", "", "The authentication key for DeepL API")
if cfg.AuthKey == "" {
if authKey, ok := os.LookupEnv("AUTHKEY"); ok {
cfg.AuthKey = authKey
}
}
flag.Parse()
return cfg
} }
type Lang struct { type Lang struct {
@ -119,17 +138,16 @@ func getTimeStamp(iCount int64) int64 {
} }
} }
type ResData struct { type PayloadFree struct {
TransText string `json:"text"` TransText string `json:"text"`
SourceLang string `json:"source_lang"` SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"` TargetLang string `json:"target_lang"`
} }
type Payload struct { type PayloadAPI struct {
Text []string `json:"text"` Text []string `json:"text"`
TargetLang string `json:"target_lang"` TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"` SourceLang string `json:"source_lang"`
GlossaryID string `json:"glossary_id"`
} }
type Translation struct { type Translation struct {
@ -144,7 +162,7 @@ func translateByAPI(text string, targetLang string, sourceLang string, authKey s
url := "https://api-free.deepl.com/v2/translate" url := "https://api-free.deepl.com/v2/translate"
textArray := strings.Split(text, "\n") textArray := strings.Split(text, "\n")
payload := Payload{ payload := PayloadAPI{
Text: textArray, Text: textArray,
TargetLang: targetLang, TargetLang: targetLang,
SourceLang: sourceLang, SourceLang: sourceLang,
@ -192,39 +210,16 @@ func translateByAPI(text string, targetLang string, sourceLang string, authKey s
} }
func main() { func main() {
// Parsing the command-line flags cfg := InitConfig()
flag.StringVar(&authKey, "authkey", "", "The authentication key for DeepL API")
flag.Parse()
// Displaying initialization information fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", cfg.Port)
fmt.Printf("DeepL X has been successfully launched! Listening on 0.0.0.0:%v\n", port)
fmt.Println("Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>.") fmt.Println("Developed by sjlleo <i@leo.moe> and missuo <me@missuo.me>.")
// Check if the token is set in the environment variable if cfg.Token != "" {
if token == "" { fmt.Println("Access token is set.")
envToken, ok := os.LookupEnv("TOKEN")
if ok {
token = envToken
fmt.Println("Access token is set from the environment variable.")
} }
} if cfg.AuthKey != "" {
fmt.Println("DeepL Official Authentication key is set.")
if token == "" {
fmt.Println("Access token is not set. You can set it using the -token flag or the TOKEN environment variable.")
} else {
fmt.Println("Access token is set. Use the Authorization: Bearer <token> header to access /translate.")
}
if authKey == "" {
envAuthKey, ok := os.LookupEnv("AUTHKEY")
if ok {
authKey = envAuthKey
fmt.Println("Authentication key is set from the environment variable.")
} else {
fmt.Println("Authentication key is not set. You can set it using the -authkey flag or the AUTHKEY environment variable.")
}
} else {
fmt.Println("Authentication key is set via command-line.")
} }
// Generating a random ID // Generating a random ID
@ -245,12 +240,12 @@ func main() {
// Defining the translation endpoint which receives translation requests and returns translations // Defining the translation endpoint which receives translation requests and returns translations
r.POST("/translate", func(c *gin.Context) { r.POST("/translate", func(c *gin.Context) {
reqj := ResData{} req := PayloadFree{}
c.BindJSON(&reqj) c.BindJSON(&req)
if token != "" { if cfg.Token != "" {
providedToken := c.GetHeader("Authorization") providedToken := c.GetHeader("Authorization")
if providedToken != "Bearer "+token { if providedToken != "Bearer "+cfg.Token {
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized, "code": http.StatusUnauthorized,
"message": "Invalid access token", "message": "Invalid access token",
@ -260,9 +255,9 @@ func main() {
} }
// Extracting details from the request JSON // Extracting details from the request JSON
sourceLang := reqj.SourceLang sourceLang := req.SourceLang
targetLang := reqj.TargetLang targetLang := req.TargetLang
translateText := reqj.TransText translateText := req.TransText
// If source language is not specified, auto-detect it // If source language is not specified, auto-detect it
if sourceLang == "" { if sourceLang == "" {
@ -361,16 +356,17 @@ func main() {
} }
if resp.StatusCode == http.StatusTooManyRequests { if resp.StatusCode == http.StatusTooManyRequests {
translatedText, err := translateByAPI(translateText, sourceLang, targetLang, authKey) translatedText, err := translateByAPI(translateText, sourceLang, targetLang, cfg.AuthKey)
if err != nil { if err != nil {
c.JSON(http.StatusTooManyRequests, gin.H{ c.JSON(http.StatusTooManyRequests, gin.H{
"code": http.StatusTooManyRequests, "code": http.StatusTooManyRequests,
"message": "Too Many Requests", "message": "Too Many Requests",
}) })
return
} }
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK, "code": http.StatusOK,
"id": 1000000, "id": 114514,
"data": translatedText, "data": translatedText,
"source_lang": sourceLang, "source_lang": sourceLang,
"target_lang": targetLang, "target_lang": targetLang,
@ -402,11 +398,10 @@ func main() {
}) })
}) })
// Determining which port to run the server on, with a fallback to a default port
envPort, ok := os.LookupEnv("PORT") envPort, ok := os.LookupEnv("PORT")
if ok { if ok {
r.Run(":" + envPort) r.Run(":" + envPort)
} else { } else {
r.Run(fmt.Sprintf(":%v", port)) r.Run(fmt.Sprintf(":%v", cfg.Port))
} }
} }