Skip to content

Commit

Permalink
feat: add main.rs and lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Sep 30, 2024
1 parent be32a09 commit 7a72f95
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 161 deletions.
2 changes: 1 addition & 1 deletion template/rs/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"project_name": "My Awesome Project",
"project_slug": "{{ cookiecutter.project_name | slugify }}",
"package_name": "{{ cookiecutter.project_slug | replace('-', '_') }}",
"project_desc": "A nice python project",
"project_desc": "A nice rust project",
"project_version": "0.0.1",
"email": "[email protected]",
"full_name": "Firstname Lastname",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ on:
- '[v]?[0-9]+.[0-9]+.[0-9]+'

jobs:

{% if cookiecutter.crate_type == "bin" %}
{%- if cookiecutter.crate_type == "bin" %}
publish:
name: Publishing for {{ "${{ matrix.job.os }}" }}
runs-on: {{ "${{ matrix.job.os }}" }}
Expand Down Expand Up @@ -51,25 +50,23 @@ jobs:
- name: Package final binary
run: |
cd target/{{ "${{ matrix.job.target }}" }}/release
BINARY_NAME={{project-name}}{{ "${{ matrix.job.binary-postfix }}" }}
BINARY_NAME={{cookiecutter.project_slug}}{{ "${{ matrix.job.binary-postfix }}" }}
GCC_PREFIX=$( [ "{{ "${{ matrix.job.target }}" }}" == "aarch64-unknown-linux-gnu" ] && echo "aarch64-linux-gnu-" || echo "" )
"$GCC_PREFIX"strip $BINARY_NAME
RELEASE_NAME={{project-name}}-${GITHUB_REF/refs\/tags\//}-{{ "${{ matrix.job.os-name }}" }}-{{ "${{ matrix.job.architecture }}" }}
RELEASE_NAME={{cookiecutter.project_slug}}-${GITHUB_REF/refs\/tags\//}-{{ "${{ matrix.job.os-name }}" }}-{{ "${{ matrix.job.architecture }}" }}
tar czvf $RELEASE_NAME.tar.gz $BINARY_NAME
shasum -a 256 $RELEASE_NAME.tar.gz > $RELEASE_NAME.sha256
- name: Release assets
uses: softprops/action-gh-release@v1
with:
files: |
target/{{ "${{ matrix.job.target }}" }}/release/{{project-name}}-*.tar.gz
target/{{ "${{ matrix.job.target }}" }}/release/{{project-name}}-*.sha256
target/{{ "${{ matrix.job.target }}" }}/release/{{cookiecutter.project_slug}}-*.tar.gz
target/{{ "${{ matrix.job.target }}" }}/release/{{cookiecutter.project_slug}}-*.sha256
env:
GITHUB_TOKEN: {{ "${{ secrets.GITHUB_TOKEN }}" }}

{% endif %}

{%- endif %}
publish-cargo:
name: Publishing to Cargo
runs-on: ubuntu-latest
Expand Down
53 changes: 21 additions & 32 deletions template/rs/{{cookiecutter.project_slug}}/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
# Base image for building the virtual environment
FROM python:{{cookiecutter.python_version}}-bookworm AS builder
# Stage 1: Build the Rust project using the official Rust image
FROM rust:1-bookworm as builder

ENV PATH="/root/.cargo/bin:$PATH" \
UV_INDEX_URL="https://mirrors.cernet.edu.cn/pypi/web/simple" \
PIP_INDEX_URL="https://mirrors.cernet.edu.cn/pypi/web/simple"

# Install uv and tools
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
# Install required dependencies for building
RUN apt-get update && apt-get install -y \
pkg-config \
libssl-dev \
build-essential \
&& rm -rf /var/lib/apt/lists/*

# Set the working directory inside the container
WORKDIR /app

COPY pyproject.toml .

# Create and install dependencies in the virtual environment
RUN uv sync

# Separate stage for validation (build and test)
FROM builder AS validator

WORKDIR /app
# Copy the project files into the container
COPY . .

# Run build and test as part of the validation
RUN make build && make test

# Final image for running the application
FROM python:{{cookiecutter.python_version}}-slim-bookworm

LABEL author="{{cookiecutter.full_name}}"
# Build the project in release mode
RUN cargo build --release

ENV PATH="/app/.venv/bin:$PATH" \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Stage 2: Create a minimal image using distroless for running the app
FROM gcr.io/distroless/cc-debian12

# Create app directory
WORKDIR /app

# Copy the virtual environment and application code
COPY --from=builder /app/.venv /app/.venv
COPY src ./src
# Copy the built executable from the builder stage
COPY --from=builder /app/target/release/{{ cookiecutter.project_slug }} /app/

HEALTHCHECK --start-period=30s CMD python -c "import requests; requests.get('http://localhost:8000', timeout=2)"
# Expose the port (for example, 8080)
EXPOSE 8080

CMD ["python", "src/{{cookiecutter.package_name}}/app.py"]
# Run the binary
CMD ["./{{ cookiecutter.project_slug }}"]
30 changes: 14 additions & 16 deletions template/rs/{{cookiecutter.project_slug}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@

## Table of Contents

- [{{project\_name}}](#project_name)
- [Table of Contents](#table-of-contents)
- [Overview](#overview)
- [Features](#features)
- [Quick Start](#quick-start)
- [Installation](#installation)
- [Requirements](#requirements)
- [User Installation](#user-installation)
- [Usage](#usage)
- [Development](#development)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [License](#license)
- [Changelog](#changelog)
- [Contact](#contact)
- [Acknowledgements](#acknowledgements)
- [Overview](#overview)
- [Features](#features)
- [Quick Start](#quick-start)
- [Installation](#installation)
- [Requirements](#requirements)
- [User Installation](#user-installation)
- [Usage](#usage)
- [Development](#development)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [License](#license)
- [Changelog](#changelog)
- [Contact](#contact)
- [Acknowledgements](#acknowledgements)

## Overview

Expand Down
14 changes: 14 additions & 0 deletions template/rs/{{cookiecutter.project_slug}}/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
3 changes: 3 additions & 0 deletions template/rs/{{cookiecutter.project_slug}}/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

This file was deleted.

This file was deleted.

This file was deleted.

63 changes: 0 additions & 63 deletions template/rs/{{cookiecutter.project_slug}}/tests/test_example.py

This file was deleted.

0 comments on commit 7a72f95

Please sign in to comment.