mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-17 05:33:42 +08:00
Refactor and optimize code: streamline error
handling, remove redundant return statements, correct gorm tag syntax, and improve type assertions in relay.go
This commit is contained in:
@@ -39,10 +39,6 @@ func checkWriter(writer io.Writer) stringWriter {
|
||||
var contentType = []string{"text/event-stream"}
|
||||
var noCache = []string{"no-cache"}
|
||||
|
||||
var fieldReplacer = strings.NewReplacer(
|
||||
"\n", "\\n",
|
||||
"\r", "\\r")
|
||||
|
||||
var dataReplacer = strings.NewReplacer(
|
||||
"\n", "\ndata:",
|
||||
"\r", "\\r")
|
||||
@@ -60,9 +56,15 @@ func encode(writer io.Writer, event CustomEvent) error {
|
||||
}
|
||||
|
||||
func writeData(w stringWriter, data interface{}) error {
|
||||
dataReplacer.WriteString(w, fmt.Sprint(data))
|
||||
_, err := dataReplacer.WriteString(w, fmt.Sprint(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.HasPrefix(data.(string), "data") {
|
||||
w.writeString("\n\n")
|
||||
_, err := w.writeString("\n\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package common
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"github.com/gin-contrib/static"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-contrib/static"
|
||||
)
|
||||
|
||||
// Credit: https://github.com/gin-contrib/static/issues/19
|
||||
@@ -15,10 +16,7 @@ type embedFileSystem struct {
|
||||
|
||||
func (e embedFileSystem) Exists(prefix string, path string) bool {
|
||||
_, err := e.Open(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
|
||||
|
||||
@@ -2,7 +2,6 @@ package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"html/template"
|
||||
"log"
|
||||
"math/rand"
|
||||
@@ -13,6 +12,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func OpenBrowser(url string) {
|
||||
@@ -106,13 +107,13 @@ func Seconds2Time(num int) (time string) {
|
||||
}
|
||||
|
||||
func Interface2String(inter interface{}) string {
|
||||
switch inter.(type) {
|
||||
switch v := inter.(type) {
|
||||
case string:
|
||||
return inter.(string)
|
||||
return v
|
||||
case int:
|
||||
return fmt.Sprintf("%d", inter.(int))
|
||||
return fmt.Sprintf("%d", v)
|
||||
case float64:
|
||||
return fmt.Sprintf("%f", inter.(float64))
|
||||
return fmt.Sprintf("%f", v)
|
||||
}
|
||||
return "Not Implemented"
|
||||
}
|
||||
@@ -138,14 +139,17 @@ func GetUUID() string {
|
||||
const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
s := rand.NewSource(time.Now().UnixNano())
|
||||
rand.New(s)
|
||||
}
|
||||
|
||||
func GenerateKey() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var src = rand.NewSource(time.Now().UnixNano())
|
||||
var r = rand.New(src)
|
||||
|
||||
key := make([]byte, 48)
|
||||
for i := 0; i < 16; i++ {
|
||||
key[i] = keyChars[rand.Intn(len(keyChars))]
|
||||
key[i] = keyChars[r.Intn(len(keyChars))]
|
||||
}
|
||||
uuid_ := GetUUID()
|
||||
for i := 0; i < 32; i++ {
|
||||
@@ -159,10 +163,12 @@ func GenerateKey() string {
|
||||
}
|
||||
|
||||
func GetRandomString(length int) string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var src = rand.NewSource(time.Now().UnixNano())
|
||||
var r = rand.New(src)
|
||||
|
||||
key := make([]byte, length)
|
||||
for i := 0; i < length; i++ {
|
||||
key[i] = keyChars[rand.Intn(len(keyChars))]
|
||||
key[i] = keyChars[r.Intn(len(keyChars))]
|
||||
}
|
||||
return string(key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user