Skip to content

Commit 877c627

Browse files
committed
Refactor to replace "user_service" with "user" across codebase
Updated variable names, comments, and log messages to ensure consistent terminology by replacing "user_service" with "user". Simplified API initialization by modifying handler definitions and added a new API secret service for better key management.
1 parent f862606 commit 877c627

13 files changed

Lines changed: 67 additions & 30 deletions

File tree

cmd/server/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
package main
22

33
import (
4+
auth "github.com/softwareplace/http-utils/oauth"
5+
"github.com/softwareplace/http-utils/security"
46
"github.com/softwareplace/http-utils/server"
57
"github.com/softwareplace/wireguard-api/pkg/domain/db"
8+
"github.com/softwareplace/wireguard-api/pkg/domain/service/api_secret_service"
69
"github.com/softwareplace/wireguard-api/pkg/domain/service/peer"
710
"github.com/softwareplace/wireguard-api/pkg/domain/service/user_service"
811
"github.com/softwareplace/wireguard-api/pkg/handlers"
912
"github.com/softwareplace/wireguard-api/pkg/handlers/request"
1013
"github.com/softwareplace/wireguard-api/pkg/handlers/user/user_handler"
14+
"github.com/softwareplace/wireguard-api/pkg/utils/env"
1115
)
1216

1317
func main() {
18+
appEnv := env.AppEnv()
1419
db.InitMongoDB()
15-
api := server.New[*request.ApiContext]()
16-
api.Use(request.ContextBuilder, "API/CONTEXT/INITIALIZER")
17-
1820
service := user_service.GetService()
1921
userAuthenticationUserHandler := user_handler.GetAuthenticationUserHandler(&service)
20-
api.Use(userAuthenticationUserHandler.Handler, "MIDDLEWARE/AUTHENTICATION_USER")
22+
api := server.New[*request.ApiContext](
23+
request.ContextBuilder,
24+
userAuthenticationUserHandler.Handler,
25+
)
2126

22-
handlers.Init(&api)
27+
secretService := api_secret_service.GetService()
28+
securityService := security.GetApiSecurityService[*request.ApiContext](appEnv.ApiSecretAuthorization)
29+
auth.Handler(appEnv.ApiSecretKey, secretService.GetKey, &securityService, api)
30+
handlers.Init(api)
2331
peer.GetService().Load()
2432
service.Init()
2533
api.StartServer()

pkg/domain/repository/user/users_repository_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *usersRepositoryImpl) FindUserBySalt(salt string) (*models.User, error)
3939
}).Decode(&currentUser)
4040

4141
if err != nil {
42-
log.Printf("Error finding user_service by salt: %v", err)
42+
log.Printf("Error finding user by salt: %v", err)
4343
return nil, err
4444
}
4545
return &currentUser, nil
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package api_secret_service
2+
3+
import (
4+
"github.com/softwareplace/http-utils/api_context"
5+
"github.com/softwareplace/wireguard-api/pkg/domain/repository/api_secret"
6+
"github.com/softwareplace/wireguard-api/pkg/handlers/request"
7+
)
8+
9+
type ApiSecretService interface {
10+
GetKey(ctx *api_context.ApiRequestContext[*request.ApiContext]) (string, error)
11+
}
12+
13+
type serviceImpl struct {
14+
repository api_secret.ApiSecretRepository
15+
}
16+
17+
func GetService() ApiSecretService {
18+
return &serviceImpl{
19+
repository: api_secret.GetRepository(),
20+
}
21+
}
22+
23+
func (s *serviceImpl) GetKey(ctx *api_context.ApiRequestContext[*request.ApiContext]) (string, error) {
24+
apiSecret, err := s.repository.GetById(ctx.ApiKey)
25+
if err != nil {
26+
return "", err
27+
}
28+
return apiSecret.Key, nil
29+
}

pkg/domain/service/user_service/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type serviceImpl struct {
2626
func (s *serviceImpl) LoadUserRoles(ctx api_context.ApiRequestContext[*request.ApiContext]) []string {
2727
user, err := s.repository.FindUserBySalt(ctx.RequestData.AccessId)
2828
if err != nil {
29-
log.Printf("[%s]:: error finding user_service: %v", ctx.GetSessionId(), err)
30-
ctx.Error("Error finding user_service in the database", http.StatusInternalServerError)
29+
log.Printf("[%s]:: error finding user: %v", ctx.GetSessionId(), err)
30+
ctx.Error("Error finding user in the database", http.StatusInternalServerError)
3131
return nil
3232
}
3333
ctx.RequestData.User = user

pkg/handlers/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/softwareplace/wireguard-api/pkg/handlers/user"
88
)
99

10-
func Init(api *server.ApiRouterHandler[*request.ApiContext]) {
10+
func Init(api server.ApiRouterHandler[*request.ApiContext]) {
1111
user.Init(api)
1212
peer.Init(api)
1313
}

pkg/handlers/peer/handlers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (h *handlerImpl) Service() peer.Service {
2323
return peer.GetService()
2424
}
2525

26-
func Init(api *server.ApiRouterHandler[*request.ApiContext]) {
26+
func Init(api server.ApiRouterHandler[*request.ApiContext]) {
2727
handler := GetHandler()
28-
(*api).Get(handler.GetAvailablePeer, "peers", "resource:peers:get:peer")
29-
(*api).Post(handler.Stream, "peers/stream", "resource:peers:stream:peers")
28+
api.Get(handler.GetAvailablePeer, "peers", "resource:peers:get:peer")
29+
api.Post(handler.Stream, "peers/stream", "resource:peers:stream:peers")
3030
}

pkg/handlers/request/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (ctx *ApiContext) GetRoles() (roles []string, err error) {
5757
if user != nil && len(user.Roles) > 0 {
5858
return user.Roles, nil
5959
}
60-
return nil, fmt.Errorf("user_service roles not found")
60+
return nil, fmt.Errorf("user roles not found")
6161
}
6262

6363
func (ctx *ApiContext) SetUser(user *models.User) {

pkg/handlers/user/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ func (h *handlerImpl) validateUserFields(ctx *api_context.ApiRequestContext[*req
4343
user.Status = "ACTIVE"
4444

4545
if err := h.UsersRepository().Save(user); err != nil {
46-
log.Printf("[%s]:: error saving user_service to the database: %v", ctx.GetSessionId(), err)
47-
ctx.Error("Error saving user_service to the database", http.StatusInternalServerError)
46+
log.Printf("[%s]:: error saving user to the database: %v", ctx.GetSessionId(), err)
47+
ctx.Error("Error saving user to the database", http.StatusInternalServerError)
4848
return
4949
}
5050

51-
log.Printf("[%s]:: user_service created successfully", ctx.GetSessionId())
51+
log.Printf("[%s]:: user created successfully", ctx.GetSessionId())
5252
ctx.Created(map[string]interface{}{"message": "User created successfully"})
5353
}

pkg/handlers/user/handlers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func (h *handlerImpl) ApiSecurityService() security.ApiSecurityService[*request.
2929
return security.GetApiSecurityService[*request.ApiContext](env.AppEnv().ApiSecretAuthorization)
3030
}
3131

32-
func Init(api *server.ApiRouterHandler[*request.ApiContext]) {
32+
func Init(api server.ApiRouterHandler[*request.ApiContext]) {
3333
handler := handlerImpl{}
34-
(*api).PublicRouter(handler.Login, "login", "POST")
35-
(*api).Post(handler.CreateUser, "user", "POST", "resource:users:create:user")
36-
(*api).Put(handler.UpdateUser, "user/:id", "resource:users:update:user")
37-
(*api).Put(handler.UpdateUser, "user")
34+
api.PublicRouter(handler.Login, "login", "POST")
35+
api.Post(handler.CreateUser, "user", "POST", "resource:users:create:user")
36+
api.Put(handler.UpdateUser, "user/:id", "resource:users:update:user")
37+
api.Put(handler.UpdateUser, "user")
3838
}

pkg/handlers/user/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (h *handlerImpl) checkUserCredentials(ctx *api_context.ApiRequestContext[*r
3535
return
3636
}
3737
ctx.InternalServerError("Internal Server Error")
38-
log.Printf("[%s]:: find user_service by username or email failed: %v", ctx.GetSessionId(), err)
38+
log.Printf("[%s]:: find user by username or email failed: %v", ctx.GetSessionId(), err)
3939

4040
return
4141
}

0 commit comments

Comments
 (0)