Skip to content

Commit

Permalink
redirect to https and normalize on server
Browse files Browse the repository at this point in the history
  • Loading branch information
avan1235 committed Feb 19, 2024
1 parent a5f5d81 commit 66381c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
Expand All @@ -28,41 +27,42 @@ import androidx.compose.ui.unit.sp
import applyIf

@Composable
internal fun ShortenResponse(shortenedUrl: String?) {
internal fun ShortenResponse(
shortenedUrl: String?,
) {
AnimatedVisibility(
visible = shortenedUrl != null,
enter = expandVertically(expandFrom = Alignment.Top),
exit = shrinkVertically(shrinkTowards = Alignment.Top),
) {
shortenedUrl?.let {
shortenedUrl?.let { url ->
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp, alignment = Alignment.CenterVertically)
) {
val uri by remember(it) { mutableStateOf(it.normalizeAsHttpUrl()) }
Spacer(Modifier.height(16.dp))
val color = MaterialTheme.colorScheme.primary
val annotatedString = remember(uri, color) {
val annotatedString = remember(url, color) {
buildAnnotatedString {
append(uri)
append(url)
addStyle(
style = SpanStyle(
color = color,
fontSize = 18.sp,
),
start = 0,
end = uri.length,
end = url.length,
)
addStyle(
style = ParagraphStyle(textAlign = TextAlign.Center),
start = 0,
end = uri.length,
end = url.length,
)
addStringAnnotation(
tag = URL_TAG,
annotation = uri,
annotation = url,
start = 0,
end = uri.length
end = url.length
)
}
}
Expand All @@ -81,15 +81,15 @@ internal fun ShortenResponse(shortenedUrl: String?) {
.getStringAnnotations(URL_TAG, position, position)
.single()
.let {
clipboardManager.setText(AnnotatedString(uri))
localUriHandler.openUri(uri)
clipboardManager.setText(AnnotatedString(url))
localUriHandler.openUri(url)
}
}
)
Spacer(Modifier.height(16.dp))
QrCode(uri)
QrCode(url)
OutlinedButton(
onClick = { clipboardManager.setText(AnnotatedString(uri)) },
onClick = { clipboardManager.setText(AnnotatedString(url)) },
) {
Text("Copy to Clipboard")
}
Expand All @@ -99,6 +99,4 @@ internal fun ShortenResponse(shortenedUrl: String?) {
}
}

private fun String.normalizeAsHttpUrl() = applyIf(!contains("://")) { "http://$this" }

private const val URL_TAG: String = "SHORT_URL_TAG"
2 changes: 1 addition & 1 deletion server/src/main/kotlin/in/procyk/shin/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal fun Application.installRoutes(): Routing = routing {
val redirectBaseUrl = dotenv.env<String>("REDIRECT_BASE_URL")
post<Shorten> {
val shortId = service.findOrCreateShortenedId(it.url)
if (shortId != null) call.respond(HttpStatusCode.OK, "$redirectBaseUrl$shortId")
if (shortId != null) call.respond(HttpStatusCode.OK, "https://$redirectBaseUrl$shortId")
else call.respond(HttpStatusCode.BadRequest)
}
get<Decode> {
Expand Down

0 comments on commit 66381c8

Please sign in to comment.