From c77cd88ca8a0206c0f48528ed565b327c97f2e38 Mon Sep 17 00:00:00 2001 From: MatthieuCoder Date: Wed, 18 Jan 2023 16:03:49 +0400 Subject: [PATCH] base --- .circleci/config.yml | 34 ----------------------------- Dockerfile | 46 ++++++++++++++++++++------------------- Makefile | 14 ++---------- exes/cache/src/main.rs | 8 ++++--- exes/gateway/src/lib.rs | 7 ++++-- exes/ratelimit/src/lib.rs | 9 ++++++-- exes/webhook/src/lib.rs | 8 +++++-- libs/shared/src/nats.rs | 15 ++++--------- libs/shared/src/redis.rs | 16 +++++--------- 9 files changed, 59 insertions(+), 98 deletions(-) delete mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 0754c1c..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,34 +0,0 @@ -version: 2.1 - -# Define the jobs we want to run for this project -jobs: - build: - machine: - image: ubuntu - environment: - DOCKER_BUILDKIT: 1 - BUILDX_PLATFORMS: linux/amd64,linux/arm64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6 - steps: - - checkout - - run: - name: Unit Tests - command: make test - - run: - name: Log in to docker hub - command: | - docker login -u $DOCKER_USER -p $DOCKER_PASS - - run: - name: Build from dockerfile - command: | - TAG=edge make build - - run: - name: Push to docker hub - command: | - TAG=edge make push - -# Orchestrate our job run sequence -workflows: - build_and_test: - jobs: - - build - - test \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 88525e9..3eba9a0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,31 +1,33 @@ -# syntax=docker/dockerfile:1 -FROM --platform=$BUILDPLATFORM tonistiigi/xx:master AS xx -FROM --platform=$BUILDPLATFORM rust:alpine as rbuild -RUN apk add clang lld protobuf protobuf-dev git build-base mingw-w64-gcc -COPY . . -COPY --from=xx / / +FROM rust AS chef +USER root +COPY .cargo .cargo +RUN cargo install cargo-chef +RUN apt-get update && apt-get install -y protobuf-compiler +WORKDIR /app -ARG TARGETPLATFORM -RUN xx-cargo build --release --target-dir ./build +# Planning install +FROM chef AS planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json -FROM --platform=$BUILDPLATFORM alpine as passwd -RUN addgroup -S nova && adduser -S nova -G nova +# Building all targets +FROM chef AS builder +COPY --from=planner /app/recipe.json recipe.json -FROM --platform=$BUILDPLATFORM golang:alpine as gbuild -RUN apk add clang lld -COPY --from=xx / / -ARG TARGETPLATFORM -COPY --from=rbuild /build/release/liball_in_one.a ./build/lib/liball_in_one.a +# Notice that we are specifying the --target flag! +RUN cargo chef cook --release --recipe-path recipe.json COPY . . -RUN go build -a -ldflags '-s' -o build/bin/nova cmd/nova/nova.go +RUN cargo build --release +# Base os +FROM debian:latest AS runtime-base +# RUN addgroup -S nova && adduser -S nova -G nova +RUN apt-get update && apt-get install ca-certificates -y -FROM scratch as component -COPY --from=passwd /etc/passwd /etc/passwd +# Final os +FROM runtime-base AS runtime ARG COMPONENT ENV COMPONENT=${COMPONENT} -COPY --from=rbuild /build/release/${COMPONENT} /usr/local/bin/ -USER nova +COPY --from=builder /app/target/release/${COMPONENT} /usr/local/bin/ +# USER nova ENTRYPOINT /usr/local/bin/${COMPONENT} - -FROM scratch as all_in_one diff --git a/Makefile b/Makefile index c1adb77..4257bd6 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,6 @@ ifeq ($(OS),Windows_NT) EXTENSION += .exe endif PROJECTS = $(shell find exes/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n') -PLATFORMS := -ifdef BUILDX_PLATFORMS - PLATFORMS += --platform ${BUILDX_PLATFORMS} -endif # Static libraries target/release/lib%.a: libs/% @@ -31,19 +27,13 @@ build/bin/nova$(EXTENSION): build/lib/liball_in_one.a @mkdir -p build/bin go build -a -ldflags '-s' -o build/bin/nova cmd/nova/nova.go -docker-%: - - -docker: $(PROJECTS:%=docker-%) BINS=$(PROJECTS:%=build/bin/%$(EXTENSION)) -bins: $(BINS) build/bin/nova$(EXTENSION) - -all: docker bins +all: $(BINS) build/bin/nova$(EXTENSION) clean: rm -rf build rm -rf $(PROJECTS:%=target/release/%$(EXTENSION)) - rm -rf target/release/liball_in_one.a + rm -rf target/release/liball_in_one.a test: cargo test diff --git a/exes/cache/src/main.rs b/exes/cache/src/main.rs index 5d43a46..c0dff6d 100644 --- a/exes/cache/src/main.rs +++ b/exes/cache/src/main.rs @@ -1,6 +1,6 @@ -use std::{error::Error, future::IntoFuture}; +use std::{error::Error, future::Future, pin::Pin}; -use async_nats::Subscriber; +use async_nats::{Client, Subscriber}; use managers::{ automoderation::Automoderation, bans::Bans, channels::Channels, guild_schedules::GuildSchedules, guilds::Guilds, integrations::Integrations, invites::Invites, @@ -42,7 +42,9 @@ struct Cache { async fn main() -> Result<(), Box> { let settings: Settings = Settings::new("cache").unwrap(); info!("loaded configuration: {:?}", settings); - let nats = settings.nats.into_future().await?; + let nats = + Into::> + Send>>>::into(settings.nats) + .await?; let mut cache = Cache::default(); diff --git a/exes/gateway/src/lib.rs b/exes/gateway/src/lib.rs index 1f41e50..ddc4bbd 100644 --- a/exes/gateway/src/lib.rs +++ b/exes/gateway/src/lib.rs @@ -18,7 +18,7 @@ use shared::{ config::Settings, payloads::{CachePayload, DispatchEventTagged}, }; -use std::{convert::TryFrom, future::IntoFuture, str::FromStr}; +use std::{convert::TryFrom, future::Future, pin::Pin, str::FromStr}; use tokio::{select, sync::oneshot}; use tokio_stream::StreamExt; use tracing_opentelemetry::OpenTelemetrySpanExt; @@ -51,7 +51,10 @@ impl Component for GatewayServer { .shard(settings.shard, settings.shard_total)? .build(); - let nats = settings.nats.into_future().await?; + let nats = Into::> + Send>>>::into( + settings.nats, + ) + .await?; shard.start().await?; loop { diff --git a/exes/ratelimit/src/lib.rs b/exes/ratelimit/src/lib.rs index 773d564..d1bd6e0 100644 --- a/exes/ratelimit/src/lib.rs +++ b/exes/ratelimit/src/lib.rs @@ -15,8 +15,10 @@ use config::Ratelimit; use grpc::RLServer; use leash::{AnyhowResultFuture, Component}; use proto::nova::ratelimit::ratelimiter::ratelimiter_server::RatelimiterServer; +use redis::aio::MultiplexedConnection; use shared::config::Settings; -use std::future::IntoFuture; +use std::future::Future; +use std::pin::Pin; use tokio::sync::oneshot; use tonic::transport::Server; @@ -36,7 +38,10 @@ impl Component for RatelimiterServerComponent { ) -> AnyhowResultFuture<()> { Box::pin(async move { let listening_address = settings.server.listening_adress; - let redis = settings.redis.into_future().await?; + let redis = Into::< + Pin> + Send>>, + >::into(settings.redis) + .await?; let server = RLServer::new(RedisLock::new(redis)); diff --git a/exes/webhook/src/lib.rs b/exes/webhook/src/lib.rs index 2f0bb7c..933f38e 100644 --- a/exes/webhook/src/lib.rs +++ b/exes/webhook/src/lib.rs @@ -12,12 +12,13 @@ mod config; mod handler; -use std::future::IntoFuture; +use std::{future::Future, pin::Pin}; use crate::{ config::Webhook, handler::{make_service::MakeSvc, WebhookService}, }; +use async_nats::Client; use hyper::Server; use leash::{AnyhowResultFuture, Component}; use shared::config::Settings; @@ -40,7 +41,10 @@ impl Component for WebhookServer { let bind = settings.server.listening_adress; info!("Nats connected!"); - let nats = settings.nats.into_future().await?; + let nats = Into::> + Send>>>::into( + settings.nats, + ) + .await?; let make_service = MakeSvc::new(WebhookService { config: settings.config, diff --git a/libs/shared/src/nats.rs b/libs/shared/src/nats.rs index 1d835d3..7d4d3d1 100644 --- a/libs/shared/src/nats.rs +++ b/libs/shared/src/nats.rs @@ -1,7 +1,4 @@ -use std::{ - future::{Future, IntoFuture}, - pin::Pin, -}; +use std::{future::Future, pin::Pin}; use async_nats::Client; use serde::Deserialize; @@ -11,12 +8,8 @@ pub struct Configuration { pub host: String, } -impl IntoFuture for Configuration { - type Output = anyhow::Result; - - type IntoFuture = Pin + Send>>; - - fn into_future(self) -> Self::IntoFuture { - Box::pin(async move { Ok(async_nats::connect(self.host).await?) }) +impl From for Pin> + Send>> { + fn from(value: Configuration) -> Self { + Box::pin(async move { Ok(async_nats::connect(value.host).await?) }) } } diff --git a/libs/shared/src/redis.rs b/libs/shared/src/redis.rs index 5cae869..77aa97f 100644 --- a/libs/shared/src/redis.rs +++ b/libs/shared/src/redis.rs @@ -1,22 +1,18 @@ use redis::{aio::MultiplexedConnection, Client}; use serde::Deserialize; -use std::{ - future::{Future, IntoFuture}, - pin::Pin, -}; +use std::{future::Future, pin::Pin}; #[derive(Clone, Debug, Deserialize)] pub struct Configuration { pub url: String, } -impl IntoFuture for Configuration { - type Output = anyhow::Result; - type IntoFuture = Pin + Send>>; - - fn into_future(self) -> Self::IntoFuture { +impl From + for Pin> + Send>> +{ + fn from(value: Configuration) -> Self { Box::pin(async move { - let con = Client::open(self.url)?; + let con = Client::open(value.url)?; let (multiplex, ready) = con.create_multiplexed_tokio_connection().await?; tokio::spawn(ready); -- 2.39.5