Compare commits
2
Commits
5e21734fb6
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6143633a45
|
||
|
|
df0858c6f2
|
+15
-10
@@ -7,13 +7,16 @@
|
||||
################################################################################
|
||||
# Create a stage for building the application.
|
||||
|
||||
ARG RUST_VERSION=1.89.0
|
||||
FROM rust:${RUST_VERSION}-slim-bullseye AS build
|
||||
ARG RUST_VERSION=1
|
||||
FROM rust:${RUST_VERSION}-slim AS build
|
||||
ARG TARGET=release
|
||||
ARG APP_NAME=urls-txt
|
||||
WORKDIR /app
|
||||
|
||||
RUN rm /var/lib/dpkg/info/libc-bin.* && apt-get clean && apt-get update && apt install libc-bin
|
||||
RUN rm /var/lib/dpkg/info/libc-bin.* \
|
||||
&& apt-get clean \
|
||||
&& apt-get update \
|
||||
&& apt-get install -y libc-bin
|
||||
|
||||
# Build the application.
|
||||
# Leverage a cache mount to /usr/local/cargo/registry/
|
||||
@@ -46,9 +49,13 @@ EOF
|
||||
# most recent version of that tag when you build your Dockerfile. If
|
||||
# reproducability is important, consider using a digest
|
||||
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
|
||||
FROM debian:bullseye-slim AS final
|
||||
RUN rm /var/lib/dpkg/info/libc-bin.* && apt-get clean && apt-get update && apt install libc-bin
|
||||
ENV URLS_TXT_BIND_ADDRESS=0.0.0.0:3000
|
||||
FROM debian:trixie-slim AS final
|
||||
|
||||
RUN rm /var/lib/dpkg/info/libc-bin.* \
|
||||
&& apt-get clean \
|
||||
&& apt-get update \
|
||||
&& apt-get upgrade -y \
|
||||
&& apt-get install -y libc-bin adduser
|
||||
|
||||
# Create a non-privileged user that the app will run under.
|
||||
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user
|
||||
@@ -63,11 +70,9 @@ RUN adduser \
|
||||
appuser
|
||||
USER appuser
|
||||
|
||||
# Copy the executable from the "build" stage.
|
||||
COPY --from=build /bin/server /bin/
|
||||
|
||||
# Expose the port that the application listens on.
|
||||
EXPOSE 8000
|
||||
EXPOSE 3000
|
||||
|
||||
# What the container should run when it is started.
|
||||
ENV URLS_TXT_BIND_ADDRESS=0.0.0.0:3000
|
||||
CMD ["/bin/server", "/urls.txt"]
|
||||
|
||||
@@ -5,5 +5,7 @@ services:
|
||||
target: final
|
||||
ports:
|
||||
- 3000:3000
|
||||
environment:
|
||||
URLS_TXT_IGNORE_QUERY_PARAMS: true # change to `false` to include query params
|
||||
volumes:
|
||||
- "./urls.txt:/urls.txt"
|
||||
|
||||
@@ -63,9 +63,11 @@ for example, for the link <http://pls.cx/🤬> the following line is in my urls.
|
||||
|
||||
trailing slashes are ignored.
|
||||
|
||||
### query parameters are ignored
|
||||
### query parameters are ignored by default
|
||||
|
||||
anything after a question mark character (`?`) are ignored.
|
||||
anything after a question mark character (`?`) are ignored by default.
|
||||
|
||||
to disable this, set the environment variable `URLS_TXT_IGNORE_QUERY_PARAMS` to `false`.
|
||||
|
||||
## why?
|
||||
|
||||
|
||||
+31
-10
@@ -13,13 +13,32 @@ enum ResponseCode {
|
||||
NotFound,
|
||||
}
|
||||
|
||||
/// Remove query parameters and trailing slashes.
|
||||
/// Makes all paths lowercase.
|
||||
fn normalise_path(path: &str) -> String {
|
||||
let mut path = path
|
||||
#[derive(Clone, Copy)]
|
||||
struct Config {
|
||||
ignore_query_params: bool,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ignore_query_params: match env::var("URLS_TXT_IGNORE_QUERY_PARAMS") {
|
||||
Ok(val) => val != "false",
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn normalise_path(path: &str, config: &Config) -> String {
|
||||
let mut path = path;
|
||||
|
||||
if config.ignore_query_params {
|
||||
path = path
|
||||
.split('?')
|
||||
.next()
|
||||
.expect("split operation returns at least one substring");
|
||||
}
|
||||
|
||||
while path.len() > 1
|
||||
&& let Some('/') = path.chars().next_back()
|
||||
{
|
||||
@@ -49,7 +68,7 @@ async fn simple_response(mut socket: BufStream<TcpStream>, rc: ResponseCode) {
|
||||
};
|
||||
}
|
||||
|
||||
async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<String, String>>) {
|
||||
async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<String, String>>, config: &Config) {
|
||||
let mut request_line = String::default();
|
||||
if let Err(e) = socket.read_line(&mut request_line).await {
|
||||
println!("process_request: failed to read line: {e}");
|
||||
@@ -63,7 +82,7 @@ async fn process_socket(mut socket: BufStream<TcpStream>, urlmap: Arc<HashMap<St
|
||||
return;
|
||||
};
|
||||
|
||||
let path = normalise_path(path);
|
||||
let path = normalise_path(path, config);
|
||||
|
||||
if http_method != "GET" {
|
||||
println!("process_request: forbidden method: http_method={http_method}");
|
||||
@@ -110,9 +129,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
};
|
||||
|
||||
let config = spawn_blocking(move || std::fs::read_to_string(urls_filename)).await??;
|
||||
let config = Config::default();
|
||||
|
||||
let urls_file = spawn_blocking(move || std::fs::read_to_string(urls_filename)).await??;
|
||||
let mut urlmap = HashMap::new();
|
||||
for url_pair in config.split('\n') {
|
||||
for url_pair in urls_file.split('\n') {
|
||||
if url_pair.is_empty() {
|
||||
continue;
|
||||
}
|
||||
@@ -132,7 +153,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Err(format!("Unable to parse line: '{}'", url_pair))?
|
||||
};
|
||||
|
||||
urlmap.insert(normalise_path(slug), redirect.to_string());
|
||||
urlmap.insert(normalise_path(slug, &config), redirect.to_string());
|
||||
}
|
||||
|
||||
let urlmap = Arc::new(urlmap);
|
||||
@@ -143,7 +164,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let (socket, _addr) = listener.accept().await?;
|
||||
let urlmap = urlmap.clone();
|
||||
tokio::spawn(async move {
|
||||
process_socket(BufStream::new(socket), urlmap).await;
|
||||
process_socket(BufStream::new(socket), urlmap, &config).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user