Skip to content

Commit

Permalink
Merge pull request #83 from raydak-labs/feat/migrate-ky-alpine
Browse files Browse the repository at this point in the history
refactor: change api to ky and switch image to alpine
  • Loading branch information
BlackDark authored Oct 11, 2024
2 parents 2135c9e + a60c0b8 commit 8ca2d07
Show file tree
Hide file tree
Showing 39 changed files with 13,082 additions and 13,185 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ jobs:
- name: Lint
run: pnpm lint

- name: Typecheck
run: pnpm typecheck

- name: Test
run: pnpm test
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
# https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
image: tonistiigi/binfmt:qemu-v8.1.5 # more recent

# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
id: buildx
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ secrets.yml
test*.json
repos/
dockerrepos
.DS_Store
11 changes: 3 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md
# TODO because multiarch build has problems with QEMU and Node we cannot use alpine here: https://github.com/nodejs/docker-node/issues/1798
FROM node:22.9.0-slim AS base
WORKDIR /app

Expand Down Expand Up @@ -35,16 +36,10 @@ ENV SECRETS_LOCATION=/app/config/secrets.yml
CMD [ "pnpm", "start" ]

# https://github.com/evanw/esbuild/issues/1921
# TODO not using alpine for now because different error messages for example for requests.
FROM node:22.9.0-slim AS prod
FROM node:22.9.0-alpine AS prod
WORKDIR /app

#RUN apk add --no-cache libstdc++ dumb-init git

RUN apt-get update && apt-get install -y \
git \
dumb-init \
&& rm -rf /var/lib/apt/lists/*
RUN apk add --no-cache libstdc++ dumb-init git

# TODO maybe in future. Results in breaking change
#USER node
Expand Down
69 changes: 69 additions & 0 deletions Dockerfile-deno.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# https://github.com/denoland/deno_docker/blob/main/example/Dockerfile

FROM denoland/deno:alpine-2.0.0 AS base
WORKDIR /app

RUN apk add --no-cache libstdc++ dumb-init git

COPY package.json pnpm-lock.yaml ./

RUN deno install

FROM base AS builder
COPY src src/
COPY index.ts esbuild.ts ./

RUN deno --allow-env --allow-read --allow-run esbuild.ts

FROM base AS dev
ENV CONFIG_LOCATION=/app/config/config.yml
ENV SECRETS_LOCATION=/app/config/secrets.yml
ENV DENO_DIR=/app/.deno_cache
# manually mount src etc

CMD ["deno", "--allow-env", "--allow-read", "--allow-run", "--allow-net", "--allow-sys", "--unstable-sloppy-imports", "index.ts"]

# https://github.com/evanw/esbuild/issues/1921
FROM denoland/deno:alpine-2.0.0 AS prod
WORKDIR /app

RUN apk add --no-cache libstdc++ dumb-init git

# TODO maybe in future. Results in breaking change
#USER node

COPY --from=builder /app/bundle.cjs /app/index.cjs

ENV CONFIG_LOCATION=/app/config/config.yml
ENV SECRETS_LOCATION=/app/config/secrets.yml
ENV DENO_DIR=/app/.deno_cache

# Compile cache / modify for multi-user
RUN deno cache --unstable-sloppy-imports index.cjs || true
RUN chmod uga+rw -R ${DENO_DIR}

# Not sure about those options
#--cached-only
#--no-code-cache

# TODO not sure about this
# Run with dumb-init to not start node with PID=1, since Node.js was not designed to run as PID 1
CMD ["dumb-init", "deno", "--allow-env", "--allow-read", "--allow-run", "--allow-net", "--allow-sys", "--unstable-sloppy-imports", "index.cjs"]


# BUN Sample
# FROM oven/bun:1.1.30-alpine AS prod
# WORKDIR /app

# RUN apk add --no-cache libstdc++ dumb-init git

# # TODO maybe in future. Results in breaking change
# #USER node

# COPY --from=builder /app/bundle.cjs /app/index.cjs

# ENV CONFIG_LOCATION=/app/config/config.yml
# ENV SECRETS_LOCATION=/app/config/secrets.yml

# # Run with dumb-init to not start node with PID=1, since Node.js was not designed to run as PID 1
# CMD ["dumb-init", "bun", "index.cjs"]
28 changes: 21 additions & 7 deletions generate-api.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import path from "path";
import { unlinkSync } from "node:fs";
import path from "node:path";
import { generateApi } from "swagger-typescript-api";

const PATH_TO_OUTPUT_DIR = path.resolve(process.cwd(), "./src/__generated__");
const PATH_SONARR_DIR = path.resolve(PATH_TO_OUTPUT_DIR, "sonarr");
const PATH_RADARR_DIR = path.resolve(PATH_TO_OUTPUT_DIR, "radarr");

const main = async () => {
await generateApi({
name: "generated-sonarr-api.ts",
output: PATH_TO_OUTPUT_DIR,
output: PATH_SONARR_DIR,
url: "https://raw.githubusercontent.com/Sonarr/Sonarr/develop/src/Sonarr.Api.V3/openapi.json",
httpClientType: "axios",
modular: true,
singleHttpClient: true,
// little hack to have one single client (we are deleting the weird created file for the http-client)
fileNames: {
httpClient: "../ky-client",
},
});

await generateApi({
name: "generated-radarr-api.ts",
output: PATH_TO_OUTPUT_DIR,
output: PATH_RADARR_DIR,
url: "https://raw.githubusercontent.com/Radarr/Radarr/develop/src/Radarr.Api.V3/openapi.json",
httpClientType: "axios",
modular: true,
singleHttpClient: true,
// little hack to have one single client (we are deleting the weird created file for the http-client)
fileNames: {
httpClient: "../ky-client",
},
});

unlinkSync(path.resolve(PATH_SONARR_DIR, "..ts"));
unlinkSync(path.resolve(PATH_RADARR_DIR, "..ts"));
};

main();
12 changes: 6 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "dotenv/config";

import fs from "node:fs";
import { CustomFormatResource } from "./src/__generated__/generated-sonarr-api";
import { MergedCustomFormatResource } from "./src/__generated__/mergedTypes";
import { configureRadarrApi, configureSonarrApi, getArrApi, unsetApi } from "./src/api";
import { getConfig } from "./src/config";
import {
Expand Down Expand Up @@ -117,7 +117,7 @@ const pipeline = async (value: ConfigArrInstance, arrType: ArrType) => {
const serverCFMapping = serverCFs.reduce((p, c) => {
p.set(c.name!, c);
return p;
}, new Map<string, CustomFormatResource>());
}, new Map<string, MergedCustomFormatResource>());

await manageCf(mergedCFs, serverCFMapping, idsToManage);
logger.info(`CustomFormats synchronized`);
Expand Down Expand Up @@ -206,8 +206,8 @@ const pipeline = async (value: ConfigArrInstance, arrType: ArrType) => {
if (!IS_DRY_RUN) {
for (const element of create) {
try {
const newProfile = await api.v3QualityprofileCreate(element as any); // Ignore types
logger.info(`Created QualityProfile: ${newProfile.data.name}`);
const newProfile = await api.v3QualityprofileCreate(element as any).json(); // Ignore types
logger.info(`Created QualityProfile: ${newProfile.name}`);
} catch (error: any) {
let message;

Expand All @@ -232,8 +232,8 @@ const pipeline = async (value: ConfigArrInstance, arrType: ArrType) => {

for (const element of changedQPs) {
try {
const newProfile = await api.v3QualityprofileUpdate("" + element.id, element as any); // Ignore types
logger.info(`Updated QualityProfile: ${newProfile.data.name}`);
const newProfile = await api.v3QualityprofileUpdate("" + element.id, element as any).json(); // Ignore types
logger.info(`Updated QualityProfile: ${newProfile.name}`);
} catch (error: any) {
let message;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "1.7.7",
"dotenv": "16.4.5",
"ky": "1.7.2",
"node-ts": "6.1.3",
"pino": "9.4.0",
"pino-pretty": "11.2.2",
Expand Down
Loading

0 comments on commit 8ca2d07

Please sign in to comment.