Merge commit '3d149fedf45472eff92910324974c762fc37dad6'

This commit is contained in:
Laisky.Cai
2024-04-21 15:05:13 +00:00
45 changed files with 649 additions and 223 deletions

View File

@@ -1,12 +0,0 @@
package config
const (
KeyPrefix = "cfg_"
KeyAPIVersion = KeyPrefix + "api_version"
KeyLibraryID = KeyPrefix + "library_id"
KeyPlugin = KeyPrefix + "plugin"
KeySK = KeyPrefix + "sk"
KeyAK = KeyPrefix + "ak"
KeyRegion = KeyPrefix + "region"
)

View File

@@ -4,12 +4,3 @@ import "time"
var StartTime = time.Now().Unix() // unit: second
var Version = "v0.0.0" // this hard coding will be replaced automatically when building, no need to manually change
var (
// CtxKeyChannel is the key to store the channel in the context
CtxKeyChannel string = "channel_docu"
CtxKeyRequestModel string = "request_model"
CtxKeyRawRequest string = "raw_request"
CtxKeyConvertedRequest string = "converted_request"
CtxKeyOriginModel string = "origin_model"
)

13
common/ctxkey/config.go Normal file
View File

@@ -0,0 +1,13 @@
package ctxkey
const (
ConfigPrefix = "cfg_"
ConfigAPIVersion = ConfigPrefix + "api_version"
ConfigLibraryID = ConfigPrefix + "library_id"
ConfigPlugin = ConfigPrefix + "plugin"
ConfigSK = ConfigPrefix + "sk"
ConfigAK = ConfigPrefix + "ak"
ConfigRegion = ConfigPrefix + "region"
ConfigUserID = ConfigPrefix + "user_id"
)

View File

@@ -1,7 +1,23 @@
package ctxkey
var (
RequestModel = "request_model"
ConvertedRequest = "converted_request"
OriginalModel = "original_model"
const (
Id = "id"
Username = "username"
Role = "role"
Status = "status"
ChannelModel = "channel_model"
ChannelRatio = "channel_ratio"
Channel = "channel"
ChannelId = "channel_id"
SpecificChannelId = "specific_channel_id"
RequestModel = "request_model"
ConvertedRequest = "converted_request"
OriginalModel = "original_model"
Group = "group"
ModelMapping = "model_mapping"
ChannelName = "channel_name"
TokenId = "token_id"
TokenName = "token_name"
BaseURL = "base_url"
AvailableModels = "available_models"
)

View File

@@ -3,15 +3,16 @@ package logger
import (
"context"
"fmt"
"github.com/Laisky/one-api/common/config"
"github.com/Laisky/one-api/common/helper"
"github.com/gin-gonic/gin"
"io"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/Laisky/one-api/common/config"
"github.com/Laisky/one-api/common/helper"
"github.com/gin-gonic/gin"
)
const (
@@ -21,28 +22,20 @@ const (
loggerError = "ERR"
)
var setupLogLock sync.Mutex
var setupLogWorking bool
var setupLogOnce sync.Once
func SetupLogger() {
if LogDir != "" {
ok := setupLogLock.TryLock()
if !ok {
log.Println("setup log is already working")
return
setupLogOnce.Do(func() {
if LogDir != "" {
logPath := filepath.Join(LogDir, fmt.Sprintf("oneapi-%s.log", time.Now().Format("20060102")))
fd, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("failed to open log file")
}
gin.DefaultWriter = io.MultiWriter(os.Stdout, fd)
gin.DefaultErrorWriter = io.MultiWriter(os.Stderr, fd)
}
defer func() {
setupLogLock.Unlock()
setupLogWorking = false
}()
logPath := filepath.Join(LogDir, fmt.Sprintf("oneapi-%s.log", time.Now().Format("20060102")))
fd, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("failed to open log file")
}
gin.DefaultWriter = io.MultiWriter(os.Stdout, fd)
gin.DefaultErrorWriter = io.MultiWriter(os.Stderr, fd)
}
})
}
func SysLog(s string) {
@@ -100,12 +93,7 @@ func logHelper(ctx context.Context, level string, msg string) {
}
now := time.Now()
_, _ = fmt.Fprintf(writer, "[%s] %v | %s | %s \n", level, now.Format("2006/01/02 - 15:04:05"), id, msg)
if !setupLogWorking {
setupLogWorking = true
go func() {
SetupLogger()
}()
}
SetupLogger()
}
func FatalLog(v ...any) {