Skip to content

Commit 709f73c

Browse files
committed
unified dockerfiles
0 parents  commit 709f73c

File tree

7 files changed

+902
-0
lines changed

7 files changed

+902
-0
lines changed

.github/workflows/deploy.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- pr-test/*
8+
9+
concurrency:
10+
group: deploy
11+
cancel-in-progress: false
12+
13+
jobs:
14+
deploy:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
id-token: write
19+
20+
steps:
21+
- name: Checkout Repo
22+
uses: actions/checkout@v4
23+
24+
- name: Install defang
25+
run: . <(curl -Ls https://s.defang.io/install)
26+
27+
- name: Login to Defang
28+
run: defang login
29+
30+
- name: Deploy
31+
run: defang compose up -v --detach

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Node.js & OpenAI
2+
3+
## Setup
4+
5+
This sample requires an API key to access the OpenAI API. The name of the config value is referenced in the compose.yaml file.
6+
To provide a value for it, you can use the Defang CLI like this:
7+
8+
```
9+
10+
defang config set --name OPENAI_KEY
11+
```
12+
13+
and then enter the value when prompted.
14+
15+
## Testing
16+
17+
```
18+
echo "Hello" | curl -H "Content-Type: text/plain" -d @- https://xxxxxxxx/prompt
19+
```
20+
21+
or
22+
23+
```
24+
cat prompt.txt | curl -H "Content-Type: application/text" -d @- https://xxxxxxxx/prompt
25+
```
26+
27+
---
28+
29+
Title: Node.js & OpenAI
30+
31+
Short Description: A simple Node.js application that interacts with the OpenAI API.
32+
33+
Tags: Node.js, OpenAI, API, JavaScript
34+
35+
Languages: nodejs

app/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Use the slim version of Debian Bookworm as the base image
2+
FROM debian:bookworm-slim
3+
4+
# Set environment variables to avoid interactive prompts during package installation
5+
ENV DEBIAN_FRONTEND=noninteractive
6+
7+
# Update the package list and install Node.js, npm, and any other dependencies
8+
RUN apt-get update && apt-get install -y \
9+
curl \
10+
&& curl -sL https://deb.nodesource.com/setup_20.x | bash - \
11+
&& apt-get install -y nodejs \
12+
&& apt-get clean \
13+
&& rm -rf /var/lib/apt/lists/*
14+
15+
# Set the working directory to /app
16+
WORKDIR /app
17+
18+
# Copy package.json and package-lock.json into the container at /app
19+
COPY package*.json ./
20+
21+
# Install any needed packages specified in package.json
22+
RUN npm ci
23+
24+
# Copy the current directory contents into the container at /app
25+
COPY . .
26+
27+
# Make port 3000 available to the world outside this container
28+
EXPOSE 3000
29+
30+
# Run the app when the container launches
31+
ENTRYPOINT ["node", "main.js"]

app/main.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const express = require('express');
2+
const axios = require('axios');
3+
const bodyParser = require('body-parser');
4+
5+
const app = express();
6+
app.use(bodyParser.text());
7+
8+
app.get('/', (req, res) => {
9+
res.json({status: 'ok'});
10+
});
11+
12+
app.post('/prompt', async (req, res) => {
13+
const promptText = req.body;
14+
15+
const messages = [
16+
// {role: 'system', content: 'You are an experienced software engineer.'},
17+
{role: 'user', content: promptText},
18+
];
19+
20+
const apiKey = process.env.OPENAI_KEY;
21+
const url = 'https://api.openai.com/v1/chat/completions';
22+
23+
const postBody = {
24+
model: 'gpt-3.5-turbo',
25+
messages: messages,
26+
};
27+
28+
const headers = {
29+
'Content-Type': 'application/json',
30+
'Authorization': `Bearer ${apiKey}`
31+
};
32+
33+
try {
34+
const response = await axios.post(url, postBody, {headers: headers});
35+
res.json({status: 'ok', response: response.data});
36+
} catch (error) {
37+
console.error(error);
38+
res.status(500).json({status: 'error', message: error.message});
39+
}
40+
});
41+
42+
app.listen(3000, () => {
43+
console.log('Server running on port 3000');
44+
});

0 commit comments

Comments
 (0)