Skip to content

Commit

Permalink
chore: rename other structs
Browse files Browse the repository at this point in the history
Signed-off-by: guillaume <[email protected]>
  • Loading branch information
gruyaume committed Sep 20, 2024
1 parent 91cf667 commit d002f3b
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 82 deletions.
2 changes: 1 addition & 1 deletion internal/server/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAuthorization(t *testing.T) {
env := &server.Environment{}
env.DB = testdb
env.JWTSecret = []byte("secret")
ts := httptest.NewTLSServer(server.NewRouter(env))
ts := httptest.NewTLSServer(server.NewHandler(env))
defer ts.Close()

client := ts.Client()
Expand Down
50 changes: 25 additions & 25 deletions internal/server/handlers_certificate_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func GetCertificateRequests(env *Environment) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
certs, err := env.DB.RetrieveAllCSRs()
if err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
body, err := json.Marshal(certs)
if err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
if _, err := w.Write(body); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
Expand All @@ -36,25 +36,25 @@ func PostCertificateRequest(env *Environment) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
csr, err := io.ReadAll(r.Body)
if err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
id, err := env.DB.CreateCSR(string(csr))
if err != nil {
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
logErrorAndWriteResponse("given csr already recorded", http.StatusBadRequest, w)
writeError("given csr already recorded", http.StatusBadRequest, w)
return
}
if strings.Contains(err.Error(), "csr validation failed") {
logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w)
writeError(err.Error(), http.StatusBadRequest, w)
return
}
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
w.WriteHeader(http.StatusCreated)
if _, err := w.Write([]byte(strconv.FormatInt(id, 10))); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
Expand All @@ -67,19 +67,19 @@ func GetCertificateRequest(env *Environment) http.HandlerFunc {
cert, err := env.DB.RetrieveCSR(id)
if err != nil {
if errors.Is(err, db.ErrIdNotFound) {
logErrorAndWriteResponse(err.Error(), http.StatusNotFound, w)
writeError(err.Error(), http.StatusNotFound, w)
return
}
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
body, err := json.Marshal(cert)
if err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
if _, err := w.Write(body); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
Expand All @@ -92,15 +92,15 @@ func DeleteCertificateRequest(env *Environment) http.HandlerFunc {
insertId, err := env.DB.DeleteCSR(id)
if err != nil {
if errors.Is(err, db.ErrIdNotFound) {
logErrorAndWriteResponse(err.Error(), http.StatusNotFound, w)
writeError(err.Error(), http.StatusNotFound, w)
return
}
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(strconv.FormatInt(insertId, 10))); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
Expand All @@ -111,7 +111,7 @@ func PostCertificate(env *Environment) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cert, err := io.ReadAll(r.Body)
if err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w)
writeError(err.Error(), http.StatusBadRequest, w)
return
}
id := r.PathValue("id")
Expand All @@ -120,10 +120,10 @@ func PostCertificate(env *Environment) http.HandlerFunc {
if errors.Is(err, db.ErrIdNotFound) ||
err.Error() == "certificate does not match CSR" ||
strings.Contains(err.Error(), "cert validation failed") {
logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w)
writeError(err.Error(), http.StatusBadRequest, w)
return
}
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
insertIdStr := strconv.FormatInt(insertId, 10)
Expand All @@ -135,7 +135,7 @@ func PostCertificate(env *Environment) http.HandlerFunc {
}
w.WriteHeader(http.StatusCreated)
if _, err := w.Write([]byte(insertIdStr)); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
Expand All @@ -146,10 +146,10 @@ func RejectCertificate(env *Environment) http.HandlerFunc {
insertId, err := env.DB.UpdateCSR(id, "rejected")
if err != nil {
if errors.Is(err, db.ErrIdNotFound) {
logErrorAndWriteResponse(err.Error(), http.StatusNotFound, w)
writeError(err.Error(), http.StatusNotFound, w)
return
}
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
insertIdStr := strconv.FormatInt(insertId, 10)
Expand All @@ -161,7 +161,7 @@ func RejectCertificate(env *Environment) http.HandlerFunc {
}
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(insertIdStr)); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
Expand All @@ -174,10 +174,10 @@ func DeleteCertificate(env *Environment) http.HandlerFunc {
insertId, err := env.DB.UpdateCSR(id, "")
if err != nil {
if errors.Is(err, db.ErrIdNotFound) {
logErrorAndWriteResponse(err.Error(), http.StatusBadRequest, w)
writeError(err.Error(), http.StatusBadRequest, w)
return
}
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
insertIdStr := strconv.FormatInt(insertId, 10)
Expand All @@ -189,7 +189,7 @@ func DeleteCertificate(env *Environment) http.HandlerFunc {
}
w.WriteHeader(http.StatusAccepted)
if _, err := w.Write([]byte(insertIdStr)); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
2 changes: 1 addition & 1 deletion internal/server/handlers_certificate_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestNotaryCertificatesHandlers(t *testing.T) {
}
env := &server.Environment{}
env.DB = testdb
ts := httptest.NewTLSServer(server.NewRouter(env))
ts := httptest.NewTLSServer(server.NewHandler(env))
defer ts.Close()

client := ts.Client()
Expand Down
4 changes: 2 additions & 2 deletions internal/server/handlers_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ func HealthCheck(env *Environment) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
users, err := env.DB.RetrieveAllUsers()
if err != nil {
logErrorAndWriteResponse("couldn't generate status", http.StatusInternalServerError, w)
writeError("couldn't generate status", http.StatusInternalServerError, w)
return
}
response, err := json.Marshal(map[string]any{
"initialized": len(users) > 0,
})
if err != nil {
logErrorAndWriteResponse("couldn't generate status", http.StatusInternalServerError, w)
writeError("couldn't generate status", http.StatusInternalServerError, w)
return
}
w.Write(response) //nolint:errcheck
Expand Down
16 changes: 8 additions & 8 deletions internal/server/handlers_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,39 @@ func Login(env *Environment) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var userRequest db.User
if err := json.NewDecoder(r.Body).Decode(&userRequest); err != nil {
logErrorAndWriteResponse("Invalid JSON format", http.StatusBadRequest, w)
writeError("Invalid JSON format", http.StatusBadRequest, w)
return
}
if userRequest.Username == "" {
logErrorAndWriteResponse("Username is required", http.StatusBadRequest, w)
writeError("Username is required", http.StatusBadRequest, w)
return
}
if userRequest.Password == "" {
logErrorAndWriteResponse("Password is required", http.StatusBadRequest, w)
writeError("Password is required", http.StatusBadRequest, w)
return
}
userAccount, err := env.DB.RetrieveUserByUsername(userRequest.Username)
if err != nil {
status := http.StatusInternalServerError
if errors.Is(err, db.ErrIdNotFound) {
logErrorAndWriteResponse("The username or password is incorrect. Try again.", http.StatusUnauthorized, w)
writeError("The username or password is incorrect. Try again.", http.StatusUnauthorized, w)
return
}
logErrorAndWriteResponse(err.Error(), status, w)
writeError(err.Error(), status, w)
return
}
if err := bcrypt.CompareHashAndPassword([]byte(userAccount.Password), []byte(userRequest.Password)); err != nil {
logErrorAndWriteResponse("The username or password is incorrect. Try again.", http.StatusUnauthorized, w)
writeError("The username or password is incorrect. Try again.", http.StatusUnauthorized, w)
return
}
jwt, err := generateJWT(userAccount.ID, userAccount.Username, env.JWTSecret, userAccount.Permissions)
if err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
return
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(jwt)); err != nil {
logErrorAndWriteResponse(err.Error(), http.StatusInternalServerError, w)
writeError(err.Error(), http.StatusInternalServerError, w)
}
}
}
2 changes: 1 addition & 1 deletion internal/server/handlers_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestLogin(t *testing.T) {
env := &server.Environment{}
env.DB = testdb
env.JWTSecret = []byte("secret")
ts := httptest.NewTLSServer(server.NewRouter(env))
ts := httptest.NewTLSServer(server.NewHandler(env))
defer ts.Close()

client := ts.Client()
Expand Down
Loading

0 comments on commit d002f3b

Please sign in to comment.