Compare commits

..

2 Commits

Author SHA1 Message Date
412aefdacc fix: record resp time 2023-11-30 10:24:00 +08:00
2c3532f12f add hostname 2023-11-30 10:18:01 +08:00
4 changed files with 15 additions and 0 deletions

12
main.go
View File

@@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
@@ -38,11 +39,17 @@ func main() {
PrepareStmt: true,
SkipDefaultTransaction: true,
})
if err != nil {
log.Fatalf("Error to connect sqlite database: %s", err)
}
case "postgres":
db, err = gorm.Open(postgres.Open(config.DBAddr), &gorm.Config{
PrepareStmt: true,
SkipDefaultTransaction: true,
})
if err != nil {
log.Fatalf("Error to connect postgres database: %s", err)
}
default:
log.Fatalf("Unsupported database type: '%s'", config.DBType)
}
@@ -88,8 +95,13 @@ func main() {
})
engine.POST("/v1/*any", func(c *gin.Context) {
hostname, err := os.Hostname()
if config.Hostname != "" {
hostname = config.Hostname
}
record := Record{
IP: c.ClientIP(),
Hostname: hostname,
CreatedAt: time.Now(),
Authorization: c.Request.Header.Get("Authorization"),
UserAgent: c.Request.Header.Get("User-Agent"),

View File

@@ -105,6 +105,7 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
var contentType string
proxy.ModifyResponse = func(r *http.Response) error {
haveResponse = true
record.ResponseTime = time.Now().Sub(record.CreatedAt)
record.Status = r.StatusCode
if !shouldResponse && r.StatusCode != 200 {
log.Println("upstream return not 200 and should not response", r.StatusCode)

View File

@@ -6,6 +6,7 @@ import (
type Record struct {
ID int64 `gorm:"primaryKey,autoIncrement"`
Hostname string
UpstreamEndpoint string
UpstreamSK string
CreatedAt time.Time

View File

@@ -9,6 +9,7 @@ import (
type Config struct {
Address string `yaml:"address"`
Hostname string `yaml:"hostname"`
DBType string `yaml:"dbtype"`
DBAddr string `yaml:"dbaddr"`
Authorization string `yaml:"authorization"`