Skip to content

Commit

Permalink
style: optimize coding (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang authored Dec 12, 2023
1 parent 35d9d72 commit 57676e1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
8 changes: 3 additions & 5 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ func runAdmin(cmd *cobra.Command, args []string) error {
}
}

addr := fmt.Sprintf(":%d", port)

bootOptions, err := config.LoadBootOptions(bootstrapPath)
if err != nil {
return err
Expand All @@ -95,17 +93,17 @@ func runAdmin(cmd *cobra.Command, args []string) error {
}
discovery := boot.NewDiscovery(bootstrapPath)
if err = boot.Boot(context.Background(), discovery); err != nil {
log.Fatal("start failed: %v", err)
log.Fatalf("start failed: %v", err)
return err
}

registryConf := discovery.GetServiceRegistry(context.Background())
serviceDiscovery, err := registry.InitDiscovery(registryConf.Name, registryConf.Options)
if err != nil {
log.Fatal("init service discovery failed: %v", err)
log.Fatalf("init service discovery failed: %v", err)
return err
}

adminServer := admin.New(op, serviceDiscovery)
return adminServer.Listen(addr)
return adminServer.Listen(fmt.Sprintf(":%d", port))
}
14 changes: 7 additions & 7 deletions pkg/admin/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package exception

import (
"fmt"
"net/http"
)

const (
Expand All @@ -30,20 +31,19 @@ const (
)

var _httpStatus = map[Code]int{
CodeNotFound: 404,
CodeInvalidParams: 400,
CodeAuth: 401,
CodeServerError: 500,
CodeNotFound: http.StatusNotFound,
CodeInvalidParams: http.StatusBadRequest,
CodeAuth: http.StatusUnauthorized,
CodeServerError: http.StatusInternalServerError,
}

type Code uint16

func (ec Code) HttpStatus() int {
hc, ok := _httpStatus[ec]
if ok {
if hc, ok := _httpStatus[ec]; ok {
return hc
}
return 500
return http.StatusInternalServerError
}

type APIException struct {
Expand Down
6 changes: 1 addition & 5 deletions pkg/admin/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,5 @@ func validateUser(tenant, username, password string) bool {
return false
}

if username != supervisor.Username || password != supervisor.Password {
return false
}

return true
return !(username != supervisor.Username || password != supervisor.Password)
}
2 changes: 1 addition & 1 deletion pkg/admin/router/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func CreateUser(c *gin.Context) error {
}
}

if err := admin.GetService(c).UpsertUser(c, tenant, &user, user.Username); err != nil {
if err = admin.GetService(c).UpsertUser(c, tenant, &user, user.Username); err != nil {
return err
}

Expand Down

0 comments on commit 57676e1

Please sign in to comment.