- Node Js (
v18.16.0 LTS
) - NPM (
9.5.1
) - MySQL or MariaDB
- We use vscode prettier extention
It is up to you to run it on local or cloud
- git clone this repository
- Configure your environment variable for the local environment
cp .env.example .env
After setup, you are good to go for deployment
- Install depedencies
npm install
- Run migration
npx sequelize-cli db:migrate
- Run the app
npm run dev
- Enable the neccesary GCP API for cloud sql, cloud run, secret manager, and cloud storage bucket
- Setup your cloud SQL (MySQL)
- You can use any method to initiate the DB like using transfer file form navicat, etc. But if you want to initiate it on mysql shell, follow the step number 3 until 5. Enter the MySQL shell :
gcloud sql connect db_name --user=root --quiet
-
Create and use the database
-
Run this query to initiate the structure
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for RefreshTokens
-- ----------------------------
DROP TABLE IF EXISTS `RefreshTokens`;
CREATE TABLE `RefreshTokens` (
`id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`hashedToken` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`userId` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `userId`(`userId`) USING BTREE,
CONSTRAINT `RefreshTokens_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `Users` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for SequelizeMeta
-- ----------------------------
DROP TABLE IF EXISTS `SequelizeMeta`;
CREATE TABLE `SequelizeMeta` (
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`name`) USING BTREE,
UNIQUE INDEX `name`(`name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for UserRecords
-- ----------------------------
DROP TABLE IF EXISTS `UserRecords`;
CREATE TABLE `UserRecords` (
`id` int NOT NULL AUTO_INCREMENT,
`prediction` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`img` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`userId` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `userId`(`userId`) USING BTREE,
CONSTRAINT `UserRecords_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `Users` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for Users
-- ----------------------------
DROP TABLE IF EXISTS `Users`;
CREATE TABLE `Users` (
`id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`gender` enum('male','female') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `email`(`email`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
- Set your GCP Bucket
- Make the bucket that are private to store your model
- Make the bucket that are serve as static file storage that are open to public
The file structure of static file storage from Hair-Fit :
static-img-hairfit
|-|haircut-recomendation-v2
|--|male
|---|Oblong
|----|img1.jpg
|--|female
|---|Oblong
|----|img2.jpg
so on the contorllers/bucket/getImg.js
, you can change this :
https://storage.cloud.google.com/static-img-hairfit/
into this :
https://storage.cloud.google.com/your-static-file-bucket/
and also this :
haircut-recomendation-v2/${gender}/${shape}/
into this
your-img-folder/${gender}/${shape}/
The hair cut recommendation can be downloaded here
- Give the authorization, run it on cloud shell (the example is using asia-southeast1)
gcloud auth configure-docker asia-southeast1.pkg.dev
gcloud auth configure-docker asia-southeast1-docker.pkg.dev
- Make the artifact repository, remember the name of it
- Build the image on cloud shell
docker build -t asia-southeast1-docker.pkg.dev/project-id/artifact-repository/image-name:tag .
- Push the image on cloud shell
docker push asia-southeast1-docker.pkg.dev/project-id/artifact-repository/image-name:tag
- Create and configure service in GCP cloud run page
- Make sure the region is same like artifact repository
- Add the environment variable (you can see the reference on
.example.env
)
The documentation of the API done in this document