Skip to content

Commit

Permalink
migrate backend to google cloud run & AI to Gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
Lujia-Cheng committed Mar 20, 2024
1 parent a0deca0 commit dc58f98
Show file tree
Hide file tree
Showing 19 changed files with 1,579 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**.env

# Logs
logs
*.log
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions api-express/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.next
**/.cache
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/build
**/dist
LICENSE
README.md
13 changes: 13 additions & 0 deletions api-express/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20.10.0

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

CMD ["npm", "start"]
22 changes: 22 additions & 0 deletions api-express/README.Docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### Building and running your application

When you're ready, start your application by running:
`docker compose up --build`.

Your application will be available at http://localhost:4000.

### Deploying your application to the cloud

First, build your image, e.g.: `docker build -t myapp .`.
If your cloud uses a different CPU architecture than your development
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
you'll want to build the image for that platform, e.g.:
`docker build --platform=linux/amd64 -t myapp .`.

Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.

Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
docs for more detail on building and pushing.

### References
* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/)
51 changes: 51 additions & 0 deletions api-express/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker Compose reference guide at
# https://docs.docker.com/go/compose-spec-reference/

# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
server:
build:
context: .
environment:
NODE_ENV: production
ports:
- 4000:4000

# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker-compose up`.
# depends_on:
# db:
# condition: service_healthy
# db:
# image: postgres
# restart: always
# user: postgres
# secrets:
# - db-password
# volumes:
# - db-data:/var/lib/postgresql/data
# environment:
# - POSTGRES_DB=example
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
# expose:
# - 5432
# healthcheck:
# test: [ "CMD", "pg_isready" ]
# interval: 10s
# timeout: 5s
# retries: 5
# volumes:
# db-data:
# secrets:
# db-password:
# file: db/password.txt

58 changes: 58 additions & 0 deletions api-express/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const express = require("express");
const cors = require("cors");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

// Import the rate-limiting middleware.
const rateLimit = require('express-rate-limit')


const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
// store: ... , // Redis, Memcached, etc. See below.
})

//
const app = express();
app.use(limiter)
app.use(cors());
app.use(express.json());

app.get("/", (req, res) => {
res.status(200).send("hello world!");
});

app.post("/api/chat", async (req, res) => {
if (!req.body || !req.body.message) {
return res.status(400).json({ message: "No message provided" });
}
const { message, history = [] } = req.body;

const model = genAI.getGenerativeModel({ model: "gemini-pro" });
console.log(JSON.stringify(history));
const chat = model.startChat({
history: history.map((msg) => ({
role: msg.role === "user" ? "user" : "model",
parts: [{ text: msg.text || "" }],
})),
});

// console.log(msg);
const result = await chat.sendMessage(message);
const response = await result.response;
const text = response.text();
console.log(text);
res.json({ message: text });
});

// Handle 404
app.use(function (request, response) {
response.status(404).json({ message: "Undefined API routes" });
});

const listener = app.listen(process.env.PORT || 4000, function () {
console.log("Your app is listening on port " + listener.address().port);
});
Loading

0 comments on commit dc58f98

Please sign in to comment.