From c56aafd75b7e7003b3852a667b8aea8495b5bf70 Mon Sep 17 00:00:00 2001 From: Juanjo Date: Wed, 21 May 2025 10:14:29 +0200 Subject: [PATCH] first commit --- .gitignore | 174 +++++++++++++++++++++++++++++++++++++ Dockerfile | 29 +++++++ dist/server.js | 13 +++ package.json | 14 +++ service/create-tmp-pkg.js | 25 ++++++ service/docker-compose.yml | 6 ++ 6 files changed, 261 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 dist/server.js create mode 100644 package.json create mode 100644 service/create-tmp-pkg.js create mode 100644 service/docker-compose.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c8942c --- /dev/null +++ b/.gitignore @@ -0,0 +1,174 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..af360c3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# PREPARATION + # Create temporary package.json where version is set to "0.0.0" + # This way the cache of the build step won't be invalidated if only the version changed. + FROM node:lts-alpine AS preparation + COPY package.json service/create-tmp-pkg.js ./ + RUN node create-tmp-pkg.js + +# BUILD + # Usa la imagen oficial de Node.js como base + FROM node:22-alpine AS build + + # Establece el directorio de trabajo dentro del contenedor + WORKDIR /usr/src/app + + # Copia e instala las dependencias + COPY --from=preparation package*.json ./ + RUN yarn install + + # Copia el resto del código fuente + COPY . . + + # Compila el código TypeScript a JavaScript + # RUN npm run build + + # Expone el puerto que la aplicación usa + EXPOSE 3000 + + # Define el comando para ejecutar la aplicación + CMD ["node", "dist/server.js"] diff --git a/dist/server.js b/dist/server.js new file mode 100644 index 0000000..2c04aa5 --- /dev/null +++ b/dist/server.js @@ -0,0 +1,13 @@ +import express from 'express'; + +const app = express(); +const PORT = parseInt(process.env.PORT ?? "3000"); +const HOST = process.env.HOST ?? '0.0.0.0'; + +app.get('/', (req, res) => { + console.log(`Request received: ${req.method} ${req.url}`); + res.send('Hello, World!'); +}); +app.listen(PORT, HOST, () => { + console.log(`Server is running at http://${HOST}:${PORT}`); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..973db4d --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "docker_test", + "type": "module", + "version": "1.0.0", + "scripts": { + "docker": "npm run dockerBuild && npm run dockerPush", + "dockerBuild": "docker build -t codetest.wundersolutions.es/juanjo/test_repo:$npm_package_version .", + "dockerPush": "docker push codetest.wundersolutions.es/juanjo/test_repo:$npm_package_version", + "": "" + }, + "dependencies": { + "express": "^5.1.0" + } +} \ No newline at end of file diff --git a/service/create-tmp-pkg.js b/service/create-tmp-pkg.js new file mode 100644 index 0000000..dfef921 --- /dev/null +++ b/service/create-tmp-pkg.js @@ -0,0 +1,25 @@ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ + +import { readFileSync, writeFileSync } from "fs"; + +try { + const pkg = JSON.parse(readFileSync("package.json", "utf-8")); + writeFileSync("package.json", JSON.stringify({ + dependencies: pkg.dependencies, + devDependencies: pkg.devDependencies, + })); +} catch (e) { + console.warn("Error al convertir package.json a JSON"); +} + +try { + const pkgLock = JSON.parse(readFileSync("package-lock.json", "utf-8")); + writeFileSync("package-lock.json", JSON.stringify({ + lockfileVersion: pkgLock.lockfileVersion, + requires: pkgLock.requires, + packages: pkgLock.packages, + })); +} catch (e) { + console.warn("Error al convertir package-lock.json a JSON"); +} diff --git a/service/docker-compose.yml b/service/docker-compose.yml new file mode 100644 index 0000000..fe60abb --- /dev/null +++ b/service/docker-compose.yml @@ -0,0 +1,6 @@ +services: + api-mysql: + image: code.wundersolutions.es/domusa/ic_apirest:2.0.0 + restart: unless-stopped + ports: + - 9002:3000 \ No newline at end of file