style(core): align struct tags and fields

Standardize alignment of struct tags in models and field assignments in
services for better readability. Additionally, include the Binding model
in the test database auto-migration.
This commit is contained in:
zenfun
2025-12-17 00:51:02 +08:00
parent ed6446e586
commit 16fceec8e7
6 changed files with 43 additions and 45 deletions

View File

@@ -157,4 +157,3 @@ func (h *Handler) UpdateBinding(c *gin.Context) {
c.JSON(http.StatusOK, existing) c.JSON(http.StatusOK, existing)
} }

View File

@@ -28,7 +28,7 @@ func newTestHandler(t *testing.T) (*Handler, *gorm.DB) {
if err != nil { if err != nil {
t.Fatalf("open sqlite: %v", err) t.Fatalf("open sqlite: %v", err)
} }
if err := db.AutoMigrate(&model.Provider{}); err != nil { if err := db.AutoMigrate(&model.Provider{}, &model.Binding{}); err != nil {
t.Fatalf("migrate: %v", err) t.Fatalf("migrate: %v", err)
} }

View File

@@ -10,4 +10,3 @@ type BindingDTO struct {
SelectorValue string `json:"selector_value"` SelectorValue string `json:"selector_value"`
Status string `json:"status"` Status string `json:"status"`
} }

View File

@@ -11,31 +11,31 @@ import (
// Master represents a tenant account. // Master represents a tenant account.
type Master struct { type Master struct {
gorm.Model gorm.Model
Name string `gorm:"size:255" json:"name"` Name string `gorm:"size:255" json:"name"`
MasterKey string `gorm:"size:255" json:"-"` // bcrypt hash of master key MasterKey string `gorm:"size:255" json:"-"` // bcrypt hash of master key
MasterKeyDigest string `gorm:"size:64;uniqueIndex" json:"-"` // sha256 digest for lookup MasterKeyDigest string `gorm:"size:64;uniqueIndex" json:"-"` // sha256 digest for lookup
Group string `gorm:"size:100;default:'default'" json:"group"` // routing group Group string `gorm:"size:100;default:'default'" json:"group"` // routing group
DefaultNamespace string `gorm:"size:100;default:'default'" json:"default_namespace"` DefaultNamespace string `gorm:"size:100;default:'default'" json:"default_namespace"`
Namespaces string `gorm:"size:1024;default:'default'" json:"namespaces"` // Comma-separated namespaces Namespaces string `gorm:"size:1024;default:'default'" json:"namespaces"` // Comma-separated namespaces
Epoch int64 `gorm:"default:1" json:"epoch"` // used for revocation/rotation Epoch int64 `gorm:"default:1" json:"epoch"` // used for revocation/rotation
Status string `gorm:"size:50;default:'active'" json:"status"` // active, suspended Status string `gorm:"size:50;default:'active'" json:"status"` // active, suspended
MaxChildKeys int `gorm:"default:5" json:"max_child_keys"` MaxChildKeys int `gorm:"default:5" json:"max_child_keys"`
GlobalQPS int `gorm:"default:3" json:"global_qps"` GlobalQPS int `gorm:"default:3" json:"global_qps"`
} }
// Key represents a child access token issued by a Master. // Key represents a child access token issued by a Master.
type Key struct { type Key struct {
gorm.Model gorm.Model
MasterID uint `gorm:"not null;index" json:"master_id"` MasterID uint `gorm:"not null;index" json:"master_id"`
KeySecret string `gorm:"size:255;column:key_secret" json:"-"` // bcrypt hash of child key KeySecret string `gorm:"size:255;column:key_secret" json:"-"` // bcrypt hash of child key
TokenHash string `gorm:"size:64;uniqueIndex" json:"token_hash"` // sha256 digest of child key TokenHash string `gorm:"size:64;uniqueIndex" json:"token_hash"` // sha256 digest of child key
Group string `gorm:"size:100;default:'default'" json:"group"` // routing group Group string `gorm:"size:100;default:'default'" json:"group"` // routing group
Scopes string `gorm:"size:1024" json:"scopes"` // Comma-separated scopes Scopes string `gorm:"size:1024" json:"scopes"` // Comma-separated scopes
DefaultNamespace string `gorm:"size:100;default:'default'" json:"default_namespace"` DefaultNamespace string `gorm:"size:100;default:'default'" json:"default_namespace"`
Namespaces string `gorm:"size:1024;default:'default'" json:"namespaces"` // Comma-separated namespaces Namespaces string `gorm:"size:1024;default:'default'" json:"namespaces"` // Comma-separated namespaces
IssuedAtEpoch int64 `gorm:"not null" json:"issued_at_epoch"` // copy of master epoch at issuance IssuedAtEpoch int64 `gorm:"not null" json:"issued_at_epoch"` // copy of master epoch at issuance
Status string `gorm:"size:50;default:'active'" json:"status"` // active, suspended Status string `gorm:"size:50;default:'active'" json:"status"` // active, suspended
IssuedBy string `gorm:"size:20;default:'master'" json:"issued_by"` IssuedBy string `gorm:"size:20;default:'master'" json:"issued_by"`
} }
// Provider remains the same. // Provider remains the same.

View File

@@ -42,16 +42,16 @@ func (s *MasterService) CreateMaster(name, group string, maxChildKeys, globalQPS
masterKeyDigest := tokenhash.HashToken(rawMasterKey) masterKeyDigest := tokenhash.HashToken(rawMasterKey)
master := &model.Master{ master := &model.Master{
Name: name, Name: name,
MasterKey: string(hashedMasterKey), MasterKey: string(hashedMasterKey),
MasterKeyDigest: masterKeyDigest, MasterKeyDigest: masterKeyDigest,
Group: group, Group: group,
DefaultNamespace: "default", DefaultNamespace: "default",
Namespaces: "default", Namespaces: "default",
MaxChildKeys: maxChildKeys, MaxChildKeys: maxChildKeys,
GlobalQPS: globalQPS, GlobalQPS: globalQPS,
Status: "active", Status: "active",
Epoch: 1, Epoch: 1,
} }
if err := s.db.Create(master).Error; err != nil { if err := s.db.Create(master).Error; err != nil {
@@ -147,16 +147,16 @@ func (s *MasterService) issueChildKey(masterID uint, group string, scopes string
} }
key := &model.Key{ key := &model.Key{
MasterID: masterID, MasterID: masterID,
KeySecret: string(hashedChildKey), KeySecret: string(hashedChildKey),
TokenHash: tokenHash, TokenHash: tokenHash,
Group: group, Group: group,
Scopes: scopes, Scopes: scopes,
DefaultNamespace: strings.TrimSpace(master.DefaultNamespace), DefaultNamespace: strings.TrimSpace(master.DefaultNamespace),
Namespaces: strings.TrimSpace(master.Namespaces), Namespaces: strings.TrimSpace(master.Namespaces),
IssuedAtEpoch: master.Epoch, IssuedAtEpoch: master.Epoch,
Status: "active", Status: "active",
IssuedBy: strings.TrimSpace(issuedBy), IssuedBy: strings.TrimSpace(issuedBy),
} }
if strings.TrimSpace(key.DefaultNamespace) == "" { if strings.TrimSpace(key.DefaultNamespace) == "" {
key.DefaultNamespace = "default" key.DefaultNamespace = "default"

View File

@@ -35,11 +35,11 @@ func (s *SyncService) SyncKey(key *model.Key) error {
} }
fields := map[string]interface{}{ fields := map[string]interface{}{
"master_id": key.MasterID, "master_id": key.MasterID,
"issued_at_epoch": key.IssuedAtEpoch, "issued_at_epoch": key.IssuedAtEpoch,
"status": key.Status, "status": key.Status,
"group": key.Group, "group": key.Group,
"scopes": key.Scopes, "scopes": key.Scopes,
"default_namespace": key.DefaultNamespace, "default_namespace": key.DefaultNamespace,
"namespaces": key.Namespaces, "namespaces": key.Namespaces,
} }
@@ -259,11 +259,11 @@ func (s *SyncService) SyncAll(db *gorm.DB) error {
return fmt.Errorf("token hash missing for key %d", k.ID) return fmt.Errorf("token hash missing for key %d", k.ID)
} }
pipe.HSet(ctx, fmt.Sprintf("auth:token:%s", tokenHash), map[string]interface{}{ pipe.HSet(ctx, fmt.Sprintf("auth:token:%s", tokenHash), map[string]interface{}{
"master_id": k.MasterID, "master_id": k.MasterID,
"issued_at_epoch": k.IssuedAtEpoch, "issued_at_epoch": k.IssuedAtEpoch,
"status": k.Status, "status": k.Status,
"group": k.Group, "group": k.Group,
"scopes": k.Scopes, "scopes": k.Scopes,
"default_namespace": k.DefaultNamespace, "default_namespace": k.DefaultNamespace,
"namespaces": k.Namespaces, "namespaces": k.Namespaces,
}) })