Skip to content

Commit

Permalink
feat: setup pong to be working as an external module and fix otlp ext…
Browse files Browse the repository at this point in the history
…ernal env var
  • Loading branch information
batleforc committed Sep 9, 2024
1 parent 996ca27 commit 23fd98b
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ npm-debug.log
yarn-error.log

trace.log
*.sarif
*.sarif
out/
12 changes: 11 additions & 1 deletion apps/back/src/api/media/get_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ pub async fn get_media(info: web::Query<QuerryMedia>, config: web::Data<Config>)
Some(kind) => HttpResponse::Ok()
.content_type(kind.mime_type().to_string())
.body(file),
None => HttpResponse::Ok().body(file),
None => {
let ext = path.extension().unwrap().to_str().unwrap();
match ext {
"js" => {
return HttpResponse::Ok()
.content_type("application/javascript")
.body(file);
}
_ => HttpResponse::Ok().body(file),
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions apps/front/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
Avatar: typeof import('primevue/avatar')['default']
Button: typeof import('primevue/button')['default']
Card: typeof import('primevue/card')['default']
Checkbox: typeof import('primevue/checkbox')['default']
Chip: typeof import('primevue/chip')['default']
Panel: typeof import('primevue/panel')['default']
ProgressSpinner: typeof import('primevue/progressspinner')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
Timeline: typeof import('primevue/timeline')['default']
Expand Down
52 changes: 52 additions & 0 deletions apps/front/src/stores/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { defineStore } from 'pinia';

export interface WasmState {
wasmLoading: boolean;
loadingError?: string;
wasm?: unknown;
moduleNames?: string;
}

const getUrl = (moduleName: string, extension: string) => {
const url = new URL('/api/media', import.meta.env.VITE_API_URL);
url.searchParams.append(
'path',
`${moduleName}/${moduleName}${extension === 'wasm' ? '_bg' : ''}.${extension}`,
);
return url.toString();
};

export const useWasmStore = defineStore({
id: 'wasm',
state: (): WasmState => ({
wasmLoading: false,
}),
actions: {
fetchWasm(moduleName: string) {
if (this.wasmLoading) return new Promise<void>((resolve) => resolve());
this.wasmLoading = true;
this.moduleNames = moduleName;
const jsModuleUrl = getUrl(moduleName, 'js');

return import(/* @vite-ignore */ jsModuleUrl)
.then((wasm) => {
this.wasm = wasm;
})
.catch((err) => {
console.error(err);
this.loadingError = 'Failed to load wasm';
this.moduleNames = '';
})
.finally(() => {
this.wasmLoading = false;
});
},
runWasm() {
if (this.wasmLoading) return new Promise<void>((resolve) => resolve());
this.wasmLoading = true;
const wasmModuleUrl = getUrl(`${this.moduleNames}`, 'wasm');
this.wasm.default({ module_or_path: wasmModuleUrl });
this.wasmLoading = false;
},
},
});
18 changes: 18 additions & 0 deletions apps/front/src/views/PongView.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
<script lang="ts" setup>
import { useWasmStore } from '../stores/wasm';
// Import from the backend the pong wasm game
const wasmStore = useWasmStore();
const loadPong = async () => {
if (!wasmStore.wasmLoading && wasmStore.moduleNames !== 'pong') {
await wasmStore.fetchWasm('pong');
wasmStore.runWasm();
}
};
</script>

<template>
<div class="min-w-full flex justify-center">
<div>
<Button v-if="wasmStore.moduleNames !== 'pong'" @click="loadPong" label="Load Pong" />
<ProgressSpinner v-if="wasmStore.wasmLoading" />
</div>
</div>
<canvas id="pong-bevy" width="800" height="600"></canvas>
</template>
1 change: 1 addition & 0 deletions apps/pong/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn main() {
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
canvas: Some("#pong-bevy".into()),
fit_canvas_to_parent: true,
..default()
}),
..default()
Expand Down
9 changes: 6 additions & 3 deletions build/back/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ RUN strip dist/target/release/server && \
FROM build_base AS build_wasm

RUN touch apps/pong/src/main.rs
RUN cargo build --release --bin pong --target wasm32-unknown-unknown
RUN wasm-opt -O -ol 100 -s 100 dist/target/wasm32-unknown-unknown/release/pong.wasm -o dist/target/wasm32-unknown-unknown/release/pong-opt.wasm
RUN cargo build --release --bin pong --target wasm32-unknown-unknown && \
wasm-bindgen --target web --out-dir ./out/ --out-name 'pong' ./dist/target/wasm32-unknown-unknown/release/pong.wasm && \
wasm-opt -O -ol 100 -s 100 ./out/pong_bg.wasm -o ./out/pong_bg.wasm

FROM gcr.io/distroless/cc-debian12
#FROM registry.hub.docker.com/library/debian:bullseye-slim
Expand All @@ -61,7 +62,9 @@ COPY --from=build_back /app/dist/target/release/swagger .

COPY ./folio_content/ .

COPY --from=build_wasm /app/dist/target/wasm32-unknown-unknown/release/pong-opt.wasm ./media/pong.wasm
RUN mkdir -p media/pong/

COPY --from=build_wasm /app/out/ ./media/pong/

EXPOSE 5437

Expand Down
1 change: 1 addition & 0 deletions folio_content/media/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pong
4 changes: 2 additions & 2 deletions libs/back/tool_tracing/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ pub fn init_tracing(tracing_config: Vec<Tracing>, name: String) {
};
let endpoint_from_env = env::var(format!(
"{}_OTEL_EXPORTER_OTLP_ENDPOINT",
name.to_uppercase()
config.name.to_uppercase()
))
.unwrap_or(endpoint);
let pod_name =
std::env::var("POD_NAME").unwrap_or_else(|_| "not_a_pod".to_string());
println!(
"Connecting to endpoint: {} with ENV {}",
endpoint_from_env.clone(),
format!("{}_OTEL_EXPORTER_OTLP_ENDPOINT", name.to_uppercase())
format!("{}_OTEL_EXPORTER_OTLP_ENDPOINT", config.name.to_uppercase())
);
let telemetry = opentelemetry_otlp::new_pipeline()
.tracing()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"vue:test": "vitest run --coverage",
"rust:api-lint": "spectral lint -f sarif -o ./spectral-report.sarif ./swagger.json",
"rust:pong": "cargo run --bin pong --target wasm32-unknown-unknown",
"rust:pong-build": "cargo build --release --bin pong --target wasm32-unknown-unknown && wasm-bindgen --target web --out-dir ./out/ --out-name 'pong' ./dist/target/wasm32-unknown-unknown/release/pong.wasm "
"rust:pong-build": "cargo build --release --bin pong --target wasm32-unknown-unknown && wasm-bindgen --target web --out-dir ./folio_content/media/pong --out-name 'pong' ./dist/target/wasm32-unknown-unknown/release/pong.wasm "
},
"private": true,
"dependencies": {
Expand Down

0 comments on commit 23fd98b

Please sign in to comment.