Compare commits
4 Commits
v0.3.0
...
fb19d8a353
| Author | SHA1 | Date | |
|---|---|---|---|
|
fb19d8a353
|
|||
|
4125c78f33
|
|||
|
31eed99025
|
|||
|
6c9eab09e2
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
openai-api-route
|
openai-api-route
|
||||||
db.sqlite
|
db.sqlite
|
||||||
|
/upstreams.yaml
|
||||||
|
|||||||
10
auth.go
10
auth.go
@@ -21,10 +21,12 @@ func handleAuth(c *gin.Context) error {
|
|||||||
authorization = strings.Trim(authorization[len("Bearer"):], " ")
|
authorization = strings.Trim(authorization[len("Bearer"):], " ")
|
||||||
log.Println("Received authorization", authorization)
|
log.Println("Received authorization", authorization)
|
||||||
|
|
||||||
if authorization != authConfig.Value {
|
for _, auth := range strings.Split(authConfig.Value, ",") {
|
||||||
err = errors.New("wrong authorization header")
|
if authorization != strings.Trim(auth, " ") {
|
||||||
c.AbortWithError(403, err)
|
err = errors.New("wrong authorization header")
|
||||||
return err
|
c.AbortWithError(403, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dbAddr := flag.String("database", "./db.sqlite", "Database address")
|
dbAddr := flag.String("database", "./db.sqlite", "Database address")
|
||||||
|
upstreamsFile := flag.String("upstreams", "./upstreams.yaml", "Upstreams file")
|
||||||
listenAddr := flag.String("addr", ":8888", "Listening address")
|
listenAddr := flag.String("addr", ":8888", "Listening address")
|
||||||
addMode := flag.Bool("add", false, "Add an OpenAI upstream")
|
addMode := flag.Bool("add", false, "Add an OpenAI upstream")
|
||||||
listMode := flag.Bool("list", false, "List all upstream")
|
listMode := flag.Bool("list", false, "List all upstream")
|
||||||
@@ -35,8 +36,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load all upstreams
|
// load all upstreams
|
||||||
upstreams := make([]OPENAI_UPSTREAM, 0)
|
upstreams := readUpstreams(*upstreamsFile)
|
||||||
db.Find(&upstreams)
|
|
||||||
log.Println("Load upstreams number:", len(upstreams))
|
log.Println("Load upstreams number:", len(upstreams))
|
||||||
|
|
||||||
err = initconfig(db)
|
err = initconfig(db)
|
||||||
|
|||||||
@@ -20,9 +20,8 @@ import (
|
|||||||
func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, shouldResponse bool) error {
|
func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, shouldResponse bool) error {
|
||||||
var errCtx error
|
var errCtx error
|
||||||
|
|
||||||
record.UpstreamID = upstream.ID
|
record.UpstreamEndpoint = upstream.Endpoint
|
||||||
record.Response = ""
|
record.Response = ""
|
||||||
record.Authorization = upstream.SK
|
|
||||||
// [TODO] record request body
|
// [TODO] record request body
|
||||||
|
|
||||||
// reverse proxy
|
// reverse proxy
|
||||||
@@ -134,6 +133,7 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
}
|
}
|
||||||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
haveResponse = true
|
haveResponse = true
|
||||||
|
record.ResponseTime = time.Now().Sub(record.CreatedAt)
|
||||||
log.Println("Error", err, upstream.SK, upstream.Endpoint)
|
log.Println("Error", err, upstream.SK, upstream.Endpoint)
|
||||||
|
|
||||||
errCtx = err
|
errCtx = err
|
||||||
|
|||||||
22
record.go
22
record.go
@@ -11,16 +11,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Record struct {
|
type Record struct {
|
||||||
ID int64 `gorm:"primaryKey,autoIncrement"`
|
ID int64 `gorm:"primaryKey,autoIncrement"`
|
||||||
CreatedAt time.Time
|
UpstreamEndpoint string
|
||||||
IP string
|
CreatedAt time.Time
|
||||||
Body string `gorm:"serializer:json"`
|
IP string
|
||||||
Model string
|
Body string `gorm:"serializer:json"`
|
||||||
Response string
|
Model string
|
||||||
ElapsedTime time.Duration
|
Response string
|
||||||
Status int
|
ResponseTime time.Duration
|
||||||
UpstreamID uint
|
ElapsedTime time.Duration
|
||||||
Authorization string
|
Status int
|
||||||
|
UpstreamID uint
|
||||||
|
Authorization string
|
||||||
}
|
}
|
||||||
|
|
||||||
type StreamModeChunk struct {
|
type StreamModeChunk struct {
|
||||||
|
|||||||
30
structure.go
30
structure.go
@@ -1,13 +1,33 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// one openai upstream contain a pair of key and endpoint
|
// one openai upstream contain a pair of key and endpoint
|
||||||
type OPENAI_UPSTREAM struct {
|
type OPENAI_UPSTREAM struct {
|
||||||
gorm.Model
|
SK string `yaml:"sk"`
|
||||||
SK string `gorm:"index:idx_sk_endpoint,unique"` // key
|
Endpoint string `yaml:"endpoint"`
|
||||||
Endpoint string `gorm:"index:idx_sk_endpoint,unique"` // endpoint
|
Timeout int64 `yaml:"timeout"`
|
||||||
Timeout int64 // timeout in seconds
|
}
|
||||||
|
|
||||||
|
func readUpstreams(filepath string) []OPENAI_UPSTREAM {
|
||||||
|
var upstreams []OPENAI_UPSTREAM
|
||||||
|
|
||||||
|
// read yaml file
|
||||||
|
data, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error reading YAML file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal the YAML into the upstreams slice
|
||||||
|
err = yaml.Unmarshal(data, &upstreams)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error unmarshaling YAML: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return upstreams
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user