Skip to content

Commit

Permalink
[Server] Fix operation name mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarciani committed Dec 1, 2024
1 parent 0fb3894 commit 8bd45e8
Showing 1 changed file with 46 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
package com.yawa.server.utils

import org.springframework.http.HttpMethod
import org.springframework.http.HttpMethod.*
import org.springframework.stereotype.Component

@Component
class OperationNameProvider {

val OPERATIONS = mapOf(
// Admin - Server
Pair(HttpMethod.GET.name(), "/manage/configprops") to "GetConfig",
Pair(HttpMethod.GET.name(), "/manage/env") to "GetEnv",
Pair(HttpMethod.GET.name(), "/manage/health") to "GetHealth",
Pair(HttpMethod.GET.name(), "/manage/info") to "GetInfo",
Pair(HttpMethod.GET.name(), "/manage/mappings") to "GetMappings",
Pair(HttpMethod.GET.name(), "/manage/metrics") to "GetMetrics",
Pair(HttpMethod.GET.name(), "/manage/prometheus") to "GetPrometheus",
Pair(HttpMethod.POST.name(), "/manage/shutdown") to "ManageShutdown",
OperationQualifier(GET, "/manage/configprops") to "GetConfig",
OperationQualifier(GET, "/manage/env") to "GetEnv",
OperationQualifier(GET, "/manage/health") to "GetHealth",
OperationQualifier(GET, "/manage/info") to "GetInfo",
OperationQualifier(GET, "/manage/mappings") to "GetMappings",
OperationQualifier(GET, "/manage/metrics/.*") to "GetMetrics",
OperationQualifier(GET, "/manage/prometheus") to "GetPrometheus",
OperationQualifier(POST, "/manage/shutdown") to "ManageShutdown",

// Admin - Email
Pair(HttpMethod.POST.name(), "/admin/mail") to "SendMail",
OperationQualifier(POST, "/admin/mail") to "SendMail",

// Documentation
Pair(HttpMethod.GET.name(), "/manage/openapi") to "GetOpenApi",
Pair(HttpMethod.GET.name(), "/manage/swagger-ui") to "GetSwaggerUi",
OperationQualifier(GET, "/docs/openapi") to "GetOpenApi",
OperationQualifier(GET, "/docs/openapi/swagger-config") to "GetSwaggerConfig",
OperationQualifier(GET, "/docs/openapi-ui") to "GetSwaggerUi",
OperationQualifier(GET, "/docs/swagger-ui/.*") to "GetSwaggerUiAssets",

// Authentication
Pair(HttpMethod.POST.name(), "/auth/login") to "Login",
Pair(HttpMethod.POST.name(), "/auth/logout") to "Logout",
Pair(HttpMethod.POST.name(), "/auth/admin/tokens") to "RefreshAuthentication",
OperationQualifier(POST, "/auth/login") to "Login",
OperationQualifier(POST, "/auth/logout") to "Logout",
OperationQualifier(POST, "/auth/.*/tokens") to "RefreshAuthentication",

// Users - Creation
Pair(HttpMethod.POST.name(), "/users/{username}") to "CreateUser",
Pair(HttpMethod.POST.name(), "/users/{username}/activation") to "ActivateUser",
Pair(HttpMethod.GET.name(), "/users/{username}/tokens/activation") to "SendUserActivationToken",
OperationQualifier(POST, "/users/.*") to "CreateUser",
OperationQualifier(POST, "/users/.*/activation") to "ActivateUser",
OperationQualifier(GET, "/users/.*/tokens/activation") to "SendUserActivationToken",

// Users - Deletion
Pair(HttpMethod.DELETE.name(), "/users/{username}") to "DeleteUser",
Pair(HttpMethod.GET.name(), "/users/{username}/tokens/deletion") to "SendUserDeletionToken",
OperationQualifier(DELETE, "/users/.*") to "DeleteUser",
OperationQualifier(GET, "/users/.*/tokens/deletion") to "SendUserDeletionToken",

// Users - Password
Pair(HttpMethod.PATCH.name(), "/users/{username}/password") to "ResetPassword",
Pair(HttpMethod.GET.name(), "/users/{username}/tokens/password") to "SendPasswordResetToken",
OperationQualifier(PATCH, "/users/.*/password") to "ResetPassword",
OperationQualifier(GET, "/users/.*/tokens/password") to "SendPasswordResetToken",

// Users - Profile
Pair(HttpMethod.GET.name(), "/users/{username}/profile") to "GetUserProfile",
Pair(HttpMethod.PATCH.name(), "/users/{username}/profile") to "UpdateUserProfile",
Pair(HttpMethod.DELETE.name(), "/users/{username}/picture") to "DeleteUserPicture",
Pair(HttpMethod.PATCH.name(), "/users/{username}/picture") to "UpdateUserPicture",
OperationQualifier(GET, "/users/.*/profile") to "GetUserProfile",
OperationQualifier(PATCH, "/users/.*/profile") to "UpdateUserProfile",
OperationQualifier(DELETE, "/users/.*/picture") to "DeleteUserPicture",
OperationQualifier(PATCH, "/users/.*/picture") to "UpdateUserPicture",

// Users - Settings
Pair(HttpMethod.GET.name(), "/users/{username}/setting") to "GetUserSettings",
Pair(HttpMethod.PATCH.name(), "/users/{username}/settings") to "UpdateUserSettings",
OperationQualifier(GET, "/users/.*/setting") to "GetUserSettings",
OperationQualifier(PATCH, "/users/.*/settings") to "UpdateUserSettings",

// Simple
Pair(HttpMethod.GET.name(), "/simple/outcome") to "GetOutcome",
Pair(HttpMethod.GET.name(), "/simple/greetings") to "GetGreetings",
OperationQualifier(GET, "/simple/outcome") to "GetOutcome",
OperationQualifier(GET, "/simple/greetings") to "GetGreetings",
)

fun getOperationName(method: String, uri: String): String {
return OPERATIONS.getOrDefault(
Pair(method, uri),
getDefaultOperationName(method = method, uri = uri),
)
OPERATIONS.forEach { (qualifier, operationName) ->
if (qualifier.matches(method = method, uri = uri)) {
return operationName
}
}
return getDefaultOperationName(method = method, uri = uri)
}

private fun getDefaultOperationName(method: String, uri: String): String = "$method:$uri"
}

class OperationQualifier(
val method: HttpMethod,
val uri: String) {
fun matches(method: String, uri: String): Boolean {
return this.method.name() == method && Regex(this.uri).matches(uri)
}
}

0 comments on commit 8bd45e8

Please sign in to comment.