Skip to content

Commit 082aac5

Browse files
authored
Exposed handler waiting time as configurable (#38)
1 parent ee4bc4e commit 082aac5

File tree

11 files changed

+164
-149
lines changed

11 files changed

+164
-149
lines changed

.env.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ LOG_LEVEL=DEBUG
33
MONGO_URI=mongodb://localhost:27017
44
MONGO_DB=switcher-gitops-test
55
GIT_TOKEN_PRIVATE_KEY=SecretSecretSecretSecretSecretSe
6+
HANDLER_WAITING_TIME=1m
67

78
SWITCHER_API_URL=https://switcherapi.com/api
89
SWITCHER_API_JWT_SECRET=SecretSecretSecretSecretSecretSe

src/controller/account.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
)
1414

1515
type AccountController struct {
16-
CoreHandler *core.CoreHandler
17-
AccountRepository repository.AccountRepository
18-
RouteAccountPath string
16+
coreHandler *core.CoreHandler
17+
accountRepository repository.AccountRepository
18+
routeAccountPath string
1919
}
2020

2121
type ErrorResponse struct {
@@ -24,18 +24,18 @@ type ErrorResponse struct {
2424

2525
func NewAccountController(repo repository.AccountRepository, coreHandler *core.CoreHandler) *AccountController {
2626
return &AccountController{
27-
CoreHandler: coreHandler,
28-
AccountRepository: repo,
29-
RouteAccountPath: "/account",
27+
coreHandler: coreHandler,
28+
accountRepository: repo,
29+
routeAccountPath: "/account",
3030
}
3131
}
3232

3333
func (controller *AccountController) RegisterRoutes(r *mux.Router) http.Handler {
34-
r.NewRoute().Path(controller.RouteAccountPath).Name("CreateAccount").HandlerFunc(controller.CreateAccountHandler).Methods(http.MethodPost)
35-
r.NewRoute().Path(controller.RouteAccountPath).Name("UpdateAccount").HandlerFunc(controller.UpdateAccountHandler).Methods(http.MethodPut)
36-
r.NewRoute().Path(controller.RouteAccountPath + "/{domainId}").Name("GelAllAccountsByDomainId").HandlerFunc(controller.FetchAllAccountsByDomainIdHandler).Methods(http.MethodGet)
37-
r.NewRoute().Path(controller.RouteAccountPath + "/{domainId}/{enviroment}").Name("GetAccount").HandlerFunc(controller.FetchAccountHandler).Methods(http.MethodGet)
38-
r.NewRoute().Path(controller.RouteAccountPath + "/{domainId}/{enviroment}").Name("DeleteAccount").HandlerFunc(controller.DeleteAccountHandler).Methods(http.MethodDelete)
34+
r.NewRoute().Path(controller.routeAccountPath).Name("CreateAccount").HandlerFunc(controller.CreateAccountHandler).Methods(http.MethodPost)
35+
r.NewRoute().Path(controller.routeAccountPath).Name("UpdateAccount").HandlerFunc(controller.UpdateAccountHandler).Methods(http.MethodPut)
36+
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}").Name("GelAllAccountsByDomainId").HandlerFunc(controller.FetchAllAccountsByDomainIdHandler).Methods(http.MethodGet)
37+
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}/{enviroment}").Name("GetAccount").HandlerFunc(controller.FetchAccountHandler).Methods(http.MethodGet)
38+
r.NewRoute().Path(controller.routeAccountPath + "/{domainId}/{enviroment}").Name("DeleteAccount").HandlerFunc(controller.DeleteAccountHandler).Methods(http.MethodDelete)
3939

4040
return r
4141
}
@@ -53,7 +53,7 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
5353
accountRequest.Token = utils.Encrypt(accountRequest.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
5454
}
5555

56-
accountCreated, err := controller.AccountRepository.Create(&accountRequest)
56+
accountCreated, err := controller.accountRepository.Create(&accountRequest)
5757
if err != nil {
5858
utils.Log(utils.LogLevelError, "Error creating account: %s", err.Error())
5959
utils.ResponseJSON(w, ErrorResponse{Error: "Error creating account"}, http.StatusInternalServerError)
@@ -62,7 +62,7 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
6262

6363
// Initialize account handler
6464
gitService := core.NewGitService(accountCreated.Repository, accountCreated.Token, accountCreated.Branch)
65-
go controller.CoreHandler.StartAccountHandler(accountCreated.ID.Hex(), gitService)
65+
go controller.coreHandler.StartAccountHandler(accountCreated.ID.Hex(), gitService)
6666

6767
utils.ResponseJSON(w, accountCreated, http.StatusCreated)
6868
}
@@ -71,7 +71,7 @@ func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter,
7171
domainId := mux.Vars(r)["domainId"]
7272
enviroment := mux.Vars(r)["enviroment"]
7373

74-
account, err := controller.AccountRepository.FetchByDomainIdEnvironment(domainId, enviroment)
74+
account, err := controller.accountRepository.FetchByDomainIdEnvironment(domainId, enviroment)
7575
if err != nil {
7676
utils.Log(utils.LogLevelError, "Error fetching account: %s", err.Error())
7777
utils.ResponseJSON(w, ErrorResponse{Error: "Account not found"}, http.StatusNotFound)
@@ -84,7 +84,7 @@ func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter,
8484
func (controller *AccountController) FetchAllAccountsByDomainIdHandler(w http.ResponseWriter, r *http.Request) {
8585
domainId := mux.Vars(r)["domainId"]
8686

87-
accounts := controller.AccountRepository.FetchAllByDomainId(domainId)
87+
accounts := controller.accountRepository.FetchAllByDomainId(domainId)
8888
if accounts == nil {
8989
utils.Log(utils.LogLevelError, "Not found accounts for domain: %s", domainId)
9090
utils.ResponseJSON(w, ErrorResponse{Error: "Not found accounts for domain: " + domainId}, http.StatusNotFound)
@@ -108,7 +108,7 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
108108
accountRequest.Token = utils.Encrypt(accountRequest.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
109109
}
110110

111-
accountUpdated, err := controller.AccountRepository.Update(&accountRequest)
111+
accountUpdated, err := controller.accountRepository.Update(&accountRequest)
112112
if err != nil {
113113
utils.Log(utils.LogLevelError, "Error updating account: %s", err.Error())
114114
utils.ResponseJSON(w, ErrorResponse{Error: "Error updating account"}, http.StatusInternalServerError)
@@ -122,7 +122,7 @@ func (controller *AccountController) DeleteAccountHandler(w http.ResponseWriter,
122122
domainId := mux.Vars(r)["domainId"]
123123
enviroment := mux.Vars(r)["enviroment"]
124124

125-
err := controller.AccountRepository.DeleteByDomainIdEnvironment(domainId, enviroment)
125+
err := controller.accountRepository.DeleteByDomainIdEnvironment(domainId, enviroment)
126126
if err != nil {
127127
utils.Log(utils.LogLevelError, "Error deleting account: %s", err.Error())
128128
utils.ResponseJSON(w, ErrorResponse{Error: "Error deleting account: " + err.Error()}, http.StatusInternalServerError)

src/controller/account_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestCreateAccountHandler(t *testing.T) {
1919
// Test
2020
accountV1.Domain.ID = "123-controller-create-account"
2121
payload, _ := json.Marshal(accountV1)
22-
req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload))
22+
req, _ := http.NewRequest(http.MethodPost, accountController.routeAccountPath, bytes.NewBuffer(payload))
2323
response := executeRequest(req)
2424

2525
// Assert
@@ -37,7 +37,7 @@ func TestCreateAccountHandler(t *testing.T) {
3737
t.Run("Should not create an account - invalid request", func(t *testing.T) {
3838
// Test
3939
payload := []byte("")
40-
req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload))
40+
req, _ := http.NewRequest(http.MethodPost, accountController.routeAccountPath, bytes.NewBuffer(payload))
4141
response := executeRequest(req)
4242

4343
// Assert
@@ -51,7 +51,7 @@ func TestCreateAccountHandler(t *testing.T) {
5151

5252
// Test
5353
payload, _ := json.Marshal(accountV1)
54-
req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload))
54+
req, _ := http.NewRequest(http.MethodPost, accountController.routeAccountPath, bytes.NewBuffer(payload))
5555
response := executeRequest(req)
5656

5757
// Assert
@@ -68,7 +68,7 @@ func TestFetchAccountHandler(t *testing.T) {
6868

6969
// Test
7070
payload := []byte("")
71-
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1.Domain.ID+"/"+accountV1.Environment, bytes.NewBuffer(payload))
71+
req, _ := http.NewRequest(http.MethodGet, accountController.routeAccountPath+"/"+accountV1.Domain.ID+"/"+accountV1.Environment, bytes.NewBuffer(payload))
7272
response := executeRequest(req)
7373

7474
// Assert
@@ -83,7 +83,7 @@ func TestFetchAccountHandler(t *testing.T) {
8383
t.Run("Should not fetch an account by domain ID / environment - not found", func(t *testing.T) {
8484
// Test
8585
payload := []byte("")
86-
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/not-found/default", bytes.NewBuffer(payload))
86+
req, _ := http.NewRequest(http.MethodGet, accountController.routeAccountPath+"/not-found/default", bytes.NewBuffer(payload))
8787
response := executeRequest(req)
8888

8989
// Assert
@@ -100,7 +100,7 @@ func TestFetchAccountHandler(t *testing.T) {
100100

101101
// Test
102102
payload := []byte("")
103-
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload))
103+
req, _ := http.NewRequest(http.MethodGet, accountController.routeAccountPath+"/"+accountV1.Domain.ID, bytes.NewBuffer(payload))
104104
response := executeRequest(req)
105105

106106
// Assert
@@ -115,7 +115,7 @@ func TestFetchAccountHandler(t *testing.T) {
115115
t.Run("Should not fetch all accounts by domain ID - not found", func(t *testing.T) {
116116
// Test
117117
payload := []byte("")
118-
req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/not-found", bytes.NewBuffer(payload))
118+
req, _ := http.NewRequest(http.MethodGet, accountController.routeAccountPath+"/not-found", bytes.NewBuffer(payload))
119119
response := executeRequest(req)
120120

121121
// Assert
@@ -138,7 +138,7 @@ func TestUpdateAccountHandler(t *testing.T) {
138138

139139
// Test
140140
payload, _ := json.Marshal(accountV2)
141-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
141+
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath, bytes.NewBuffer(payload))
142142
response := executeRequest(req)
143143

144144
// Assert
@@ -164,7 +164,7 @@ func TestUpdateAccountHandler(t *testing.T) {
164164
accountRequest.Token = "new-token"
165165

166166
payload, _ := json.Marshal(accountRequest)
167-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
167+
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath, bytes.NewBuffer(payload))
168168
response := executeRequest(req)
169169

170170
// Assert
@@ -185,7 +185,7 @@ func TestUpdateAccountHandler(t *testing.T) {
185185
t.Run("Should not update an account - invalid request", func(t *testing.T) {
186186
// Test
187187
payload := []byte("")
188-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
188+
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath, bytes.NewBuffer(payload))
189189
response := executeRequest(req)
190190

191191
// Assert
@@ -203,7 +203,7 @@ func TestUpdateAccountHandler(t *testing.T) {
203203

204204
// Test
205205
payload, _ := json.Marshal(accountV1)
206-
req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath, bytes.NewBuffer(payload))
206+
req, _ := http.NewRequest(http.MethodPut, accountController.routeAccountPath, bytes.NewBuffer(payload))
207207
response := executeRequest(req)
208208

209209
// Assert
@@ -219,7 +219,7 @@ func TestDeleteAccountHandler(t *testing.T) {
219219
accountController.CreateAccountHandler(givenAccountRequest(accountV1))
220220

221221
// Test
222-
req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/"+accountV1.Domain.ID+"/"+accountV1.Environment, nil)
222+
req, _ := http.NewRequest(http.MethodDelete, accountController.routeAccountPath+"/"+accountV1.Domain.ID+"/"+accountV1.Environment, nil)
223223
response := executeRequest(req)
224224

225225
// Assert
@@ -228,7 +228,7 @@ func TestDeleteAccountHandler(t *testing.T) {
228228

229229
t.Run("Should not delete an account by domain ID / environment - not found", func(t *testing.T) {
230230
// Test
231-
req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/not-found/default", nil)
231+
req, _ := http.NewRequest(http.MethodDelete, accountController.routeAccountPath+"/not-found/default", nil)
232232
response := executeRequest(req)
233233

234234
// Assert
@@ -241,7 +241,7 @@ func TestDeleteAccountHandler(t *testing.T) {
241241

242242
func givenAccountRequest(data model.Account) (*httptest.ResponseRecorder, *http.Request) {
243243
w := httptest.NewRecorder()
244-
r := httptest.NewRequest(http.MethodPost, accountController.RouteAccountPath, nil)
244+
r := httptest.NewRequest(http.MethodPost, accountController.routeAccountPath, nil)
245245

246246
// Encode the account request as JSON
247247
body, _ := json.Marshal(data)

src/controller/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
type ApiController struct {
14-
CoreHandler *core.CoreHandler
15-
RouteCheckApiPath string
14+
coreHandler *core.CoreHandler
15+
routeCheckApiPath string
1616
}
1717

1818
type ApiCheckResponse struct {
@@ -32,13 +32,13 @@ type ApiSettingsResponse struct {
3232

3333
func NewApiController(coreHandler *core.CoreHandler) *ApiController {
3434
return &ApiController{
35-
CoreHandler: coreHandler,
36-
RouteCheckApiPath: "/api/check",
35+
coreHandler: coreHandler,
36+
routeCheckApiPath: "/api/check",
3737
}
3838
}
3939

4040
func (controller *ApiController) RegisterRoutes(r *mux.Router) http.Handler {
41-
r.NewRoute().Path(controller.RouteCheckApiPath).Name("CheckApi").HandlerFunc(controller.CheckApiHandler).Methods(http.MethodGet)
41+
r.NewRoute().Path(controller.routeCheckApiPath).Name("CheckApi").HandlerFunc(controller.CheckApiHandler).Methods(http.MethodGet)
4242

4343
return r
4444
}
@@ -52,7 +52,7 @@ func (controller *ApiController) CheckApiHandler(w http.ResponseWriter, r *http.
5252
SwitcherURL: config.GetEnv("SWITCHER_API_URL"),
5353
SwitcherSecret: len(config.GetEnv("SWITCHER_API_JWT_SECRET")) > 0,
5454
GitTokenSecret: len(config.GetEnv("GIT_TOKEN_PRIVATE_KEY")) > 0,
55-
CoreHandlerStatus: controller.CoreHandler.Status,
55+
CoreHandlerStatus: controller.coreHandler.Status,
5656
NumGoroutines: runtime.NumGoroutine(),
5757
},
5858
}, http.StatusOK)

src/core/api.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ type IAPIService interface {
2929
}
3030

3131
type ApiService struct {
32-
ApiKey string
33-
ApiUrl string
32+
apiKey string
33+
apiUrl string
3434
}
3535

3636
func NewApiService(apiKey string, apiUrl string) *ApiService {
3737
return &ApiService{
38-
ApiKey: apiKey,
39-
ApiUrl: apiUrl,
38+
apiKey: apiKey,
39+
apiUrl: apiUrl,
4040
}
4141
}
4242

@@ -70,7 +70,7 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string,
7070

7171
func (a *ApiService) ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error) {
7272
reqBody, _ := json.Marshal(diff)
73-
responseBody, err := a.doPostRequest(a.ApiUrl+"/gitops/apply", domainId, reqBody)
73+
responseBody, err := a.doPostRequest(a.apiUrl+"/gitops/apply", domainId, reqBody)
7474

7575
if err != nil {
7676
return ApplyChangeResponse{}, err
@@ -83,11 +83,11 @@ func (a *ApiService) ApplyChangesToAPI(domainId string, environment string, diff
8383

8484
func (a *ApiService) doGraphQLRequest(domainId string, query string) (string, error) {
8585
// Generate a bearer token
86-
token := generateBearerToken(a.ApiKey, domainId)
86+
token := generateBearerToken(a.apiKey, domainId)
8787

8888
// Create a new request
8989
reqBody, _ := json.Marshal(GraphQLRequest{Query: query})
90-
req, _ := http.NewRequest("POST", a.ApiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody))
90+
req, _ := http.NewRequest("POST", a.apiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody))
9191

9292
// Set the request headers
9393
setHeaders(req, token)
@@ -106,7 +106,7 @@ func (a *ApiService) doGraphQLRequest(domainId string, query string) (string, er
106106

107107
func (a *ApiService) doPostRequest(url string, domainId string, body []byte) (string, error) {
108108
// Generate a bearer token
109-
token := generateBearerToken(a.ApiKey, domainId)
109+
token := generateBearerToken(a.apiKey, domainId)
110110

111111
// Create a new request
112112
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))

src/core/git.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ type IGitService interface {
2222
}
2323

2424
type GitService struct {
25-
RepoURL string
26-
EncryptedToken string
27-
BranchName string
25+
repoURL string
26+
encryptedToken string
27+
branchName string
2828
}
2929

3030
func NewGitService(repoURL string, encryptedToken string, branchName string) *GitService {
3131
return &GitService{
32-
RepoURL: repoURL,
33-
EncryptedToken: encryptedToken,
34-
BranchName: branchName,
32+
repoURL: repoURL,
33+
encryptedToken: encryptedToken,
34+
branchName: branchName,
3535
}
3636
}
3737

3838
func (g *GitService) UpdateRepositorySettings(repository string, encryptedToken string, branch string) {
39-
g.RepoURL = repository
40-
g.EncryptedToken = encryptedToken
41-
g.BranchName = branch
39+
g.repoURL = repository
40+
g.encryptedToken = encryptedToken
41+
g.branchName = branch
4242
}
4343

4444
func (g *GitService) GetRepositoryData(environment string) (*model.RepositoryData, error) {
@@ -140,14 +140,14 @@ func (g *GitService) getCommitObject() (*object.Commit, error) {
140140

141141
func (g *GitService) getRepository(fs billy.Filesystem) (*git.Repository, error) {
142142
return git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
143-
URL: g.RepoURL,
144-
ReferenceName: plumbing.NewBranchReferenceName(g.BranchName),
143+
URL: g.repoURL,
144+
ReferenceName: plumbing.NewBranchReferenceName(g.branchName),
145145
Auth: g.getAuth(),
146146
})
147147
}
148148

149149
func (g *GitService) getAuth() *http.BasicAuth {
150-
decryptedToken, err := utils.Decrypt(g.EncryptedToken, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
150+
decryptedToken, err := utils.Decrypt(g.encryptedToken, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
151151

152152
if err != nil || decryptedToken == "" {
153153
return nil

src/core/git_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func TestNewGitService(t *testing.T) {
2929
gitService := NewGitService(repoURL, encryptedToken, branchName)
3030

3131
// Assert
32-
assert.Equal(t, repoURL, gitService.RepoURL)
33-
assert.Equal(t, encryptedToken, gitService.EncryptedToken)
34-
assert.Equal(t, branchName, gitService.BranchName)
32+
assert.Equal(t, repoURL, gitService.repoURL)
33+
assert.Equal(t, encryptedToken, gitService.encryptedToken)
34+
assert.Equal(t, branchName, gitService.branchName)
3535
}
3636

3737
func TestUpdateRepositorySettings(t *testing.T) {
@@ -45,9 +45,9 @@ func TestUpdateRepositorySettings(t *testing.T) {
4545
gitService.UpdateRepositorySettings("newRepoURL", "newEncryptedToken", "newBranch")
4646

4747
// Assert
48-
assert.Equal(t, "newRepoURL", gitService.RepoURL)
49-
assert.Equal(t, "newEncryptedToken", gitService.EncryptedToken)
50-
assert.Equal(t, "newBranch", gitService.BranchName)
48+
assert.Equal(t, "newRepoURL", gitService.repoURL)
49+
assert.Equal(t, "newEncryptedToken", gitService.encryptedToken)
50+
assert.Equal(t, "newBranch", gitService.branchName)
5151
}
5252

5353
func TestGetRepositoryData(t *testing.T) {

0 commit comments

Comments
 (0)