MCPcopy Create free account
hub / github.com/bricks-cloud/BricksLLM / getCreateKeyHandler

Function getCreateKeyHandler

internal/server/web/admin/admin.go:469–557  ·  view source on GitHub ↗
(m KeyManager, prod bool)

Source from the content-addressed store, hash-verified

467}
468
469func getCreateKeyHandler(m KeyManager, prod bool) gin.HandlerFunc {
470 return func(c *gin.Context) {
471 log := util.GetLogFromCtx(c)
472 telemetry.Incr("bricksllm.admin.get_create_key_handler.requests", nil, 1)
473
474 start := time.Now()
475 defer func() {
476 dur := time.Since(start)
477 telemetry.Timing("bricksllm.admin.get_create_key_handler.latency", dur, nil, 1)
478 }()
479
480 path := "/api/key-management/keys"
481 if c == nil || c.Request == nil {
482 c.JSON(http.StatusInternalServerError, &ErrorResponse{
483 Type: "/errors/empty-context",
484 Title: "context is empty error",
485 Status: http.StatusInternalServerError,
486 Detail: "gin context is empty",
487 Instance: path,
488 })
489 return
490 }
491
492 data, err := io.ReadAll(c.Request.Body)
493 if err != nil {
494 logError(log, "error when reading key creation request body", prod, err)
495 c.JSON(http.StatusInternalServerError, &ErrorResponse{
496 Type: "/errors/request-body-read",
497 Title: "request body reader error",
498 Status: http.StatusInternalServerError,
499 Detail: err.Error(),
500 Instance: path,
501 })
502 return
503 }
504
505 rk := &key.RequestKey{}
506 err = json.Unmarshal(data, rk)
507 if err != nil {
508 logError(log, "error when unmarshalling key creation request body", prod, err)
509 c.JSON(http.StatusInternalServerError, &ErrorResponse{
510 Type: "/errors/json-unmarshal",
511 Title: "json unmarshaller error",
512 Status: http.StatusInternalServerError,
513 Detail: err.Error(),
514 Instance: path,
515 })
516 return
517 }
518
519 resk, err := m.CreateKey(rk)
520 if err != nil {
521 errType := "internal"
522
523 defer func() {
524 telemetry.Incr("bricksllm.admin.get_create_key_handler.create_key_error", []string{
525 "error_type:" + errType,
526 }, 1)

Callers 1

NewAdminServerFunction · 0.85

Calls 6

GetLogFromCtxFunction · 0.92
IncrFunction · 0.92
TimingFunction · 0.92
logErrorFunction · 0.70
ErrorMethod · 0.65
CreateKeyMethod · 0.65

Tested by

no test coverage detected