Skip to content

Commit

Permalink
fix parsing null values of setting_id field
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelu2016 committed Nov 7, 2023
1 parent 020c3b0 commit ff707fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/server/web/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (as *AdminServer) Run() {
as.log.Info("PORT 8001 | PATCH | /api/key-management/keys/:id is set up for updating a key using an id")
as.log.Info("PORT 8001 | PUT | /api/provider-settings is set up for creating a provider setting")
as.log.Info("PORT 8001 | PATCH | /api/provider-settings:id is set up for updating provider setting")
as.log.Info("PORT 8001 | GET | /api/reporting/events is set up for retrieving api metrics")
as.log.Info("PORT 8001 | POST | /api/reporting/events is set up for retrieving api metrics")

if err := as.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
as.log.Sugar().Fatalf("error admin server listening: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/web/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func getChatCompletionHandler(r recorder, prod, private bool, psm ProviderSettin
func (ps *ProxyServer) Run() {
go func() {
ps.log.Info("proxy server listening at 8002")
ps.log.Info("PORT 8002 | POST | /api/providers/openai/v1/chat/completions is ready for forwarding requests to openai")
ps.log.Info("PORT 8002 | POST | /api/providers/openai/v1/chat/completions is ready for forwarding requests to openai")

if err := ps.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
ps.log.Sugar().Fatalf("error proxy server listening: %v", err)
Expand Down
53 changes: 42 additions & 11 deletions internal/storage/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func (s *Store) GetKeysByTag(tag string) ([]*key.ResponseKey, error) {
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
Expand All @@ -359,11 +360,15 @@ func (s *Store) GetKeysByTag(tag string) ([]*key.ResponseKey, error) {
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&k.SettingId,
&settingId,
); err != nil {
return nil, err
}
keys = append(keys, &k)

pk := &k
pk.SettingId = settingId.String

keys = append(keys, pk)
}

return keys, nil
Expand All @@ -382,6 +387,8 @@ func (s *Store) GetKey(keyId string) (*key.ResponseKey, error) {
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString

if err := rows.Scan(
&k.Name,
&k.CreatedAt,
Expand All @@ -397,11 +404,15 @@ func (s *Store) GetKey(keyId string) (*key.ResponseKey, error) {
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&k.SettingId,
&settingId,
); err != nil {
return nil, err
}
keys = append(keys, &k)

pk := &k
pk.SettingId = settingId.String

keys = append(keys, pk)
}

if len(keys) == 0 {
Expand Down Expand Up @@ -500,6 +511,7 @@ func (s *Store) GetAllKeys() ([]*key.ResponseKey, error) {
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString
if err := rows.Scan(
&k.Name,
&k.CreatedAt,
Expand All @@ -515,11 +527,15 @@ func (s *Store) GetAllKeys() ([]*key.ResponseKey, error) {
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&k.SettingId,
&settingId,
); err != nil {
return nil, err
}
keys = append(keys, &k)

pk := &k
pk.SettingId = settingId.String

keys = append(keys, pk)
}

return keys, nil
Expand Down Expand Up @@ -574,6 +590,8 @@ func (s *Store) GetUpdatedKeys(interval time.Duration) ([]*key.ResponseKey, erro
keys := []*key.ResponseKey{}
for rows.Next() {
var k key.ResponseKey
var settingId sql.NullString

if err := rows.Scan(
&k.Name,
&k.CreatedAt,
Expand All @@ -589,11 +607,15 @@ func (s *Store) GetUpdatedKeys(interval time.Duration) ([]*key.ResponseKey, erro
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&k.SettingId,
&settingId,
); err != nil {
return nil, err
}
keys = append(keys, &k)

pk := &k
pk.SettingId = settingId.String

keys = append(keys, pk)
}

return keys, nil
Expand Down Expand Up @@ -684,6 +706,7 @@ func (s *Store) UpdateKey(id string, uk *key.UpdateKey) (*key.ResponseKey, error
defer cancel()

var k key.ResponseKey
var settingId sql.NullString
if err := s.db.QueryRowContext(ctxTimeout, query, values...).Scan(
&k.Name,
&k.CreatedAt,
Expand All @@ -704,7 +727,10 @@ func (s *Store) UpdateKey(id string, uk *key.UpdateKey) (*key.ResponseKey, error
return nil, err
}

return &k, nil
pk := &k
pk.SettingId = settingId.String

return pk, nil
}

func (s *Store) UpdateProviderSetting(id string, setting *provider.Setting) (*provider.Setting, error) {
Expand Down Expand Up @@ -835,6 +861,8 @@ func (s *Store) CreateKey(rk *key.RequestKey) (*key.ResponseKey, error) {
defer cancel()

var k key.ResponseKey

var settingId sql.NullString
if err := s.db.QueryRowContext(ctxTimeout, query, values...).Scan(
&k.Name,
&k.CreatedAt,
Expand All @@ -850,12 +878,15 @@ func (s *Store) CreateKey(rk *key.RequestKey) (*key.ResponseKey, error) {
&k.RateLimitOverTime,
&k.RateLimitUnit,
&k.Ttl,
&k.SettingId,
&settingId,
); err != nil {
return nil, err
}

return &k, nil
pk := &k
pk.SettingId = settingId.String

return pk, nil
}

func (s *Store) DeleteKey(id string) error {
Expand Down

0 comments on commit ff707fb

Please sign in to comment.