support fetch models
This commit is contained in:
58
internal/controller/proxy/models.go
Normal file
58
internal/controller/proxy/models.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"opencatd-open/internal/dto"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (p *Proxy) HandleModels(c *gin.Context) {
|
||||
models, err := p.getCache()
|
||||
if err != nil {
|
||||
dto.Fail(c, http.StatusBadGateway, err.Error())
|
||||
return
|
||||
}
|
||||
type _model struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
var ms []_model
|
||||
for _, model := range models {
|
||||
ms = append(ms, _model{ID: model})
|
||||
}
|
||||
dto.Success(c, ms)
|
||||
}
|
||||
|
||||
func (p *Proxy) setCache() error {
|
||||
apikeys, err := p.apiKeyDao.FindKeys(nil)
|
||||
models := make(map[string]bool)
|
||||
if err == nil && len(apikeys) > 0 {
|
||||
for _, k := range apikeys {
|
||||
if len(k.SupportModelsArray) > 0 {
|
||||
for _, sm := range k.SupportModelsArray {
|
||||
models[sm] = true
|
||||
}
|
||||
} else {
|
||||
var sma []string
|
||||
json.Unmarshal([]byte(*k.SupportModels), &sma) // nolint:errCheck
|
||||
for _, sm := range sma {
|
||||
models[sm] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("empty data")
|
||||
}
|
||||
var support_models []string
|
||||
for m, _ := range models {
|
||||
support_models = append(support_models, m)
|
||||
}
|
||||
return p.cache.Set("models", support_models)
|
||||
}
|
||||
|
||||
func (p *Proxy) getCache() ([]string, error) {
|
||||
models, err := p.cache.Get("models")
|
||||
return models.([]string), err
|
||||
}
|
||||
Reference in New Issue
Block a user