-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_microservice.sh
181 lines (165 loc) · 6.87 KB
/
create_microservice.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
# options to display
options=("FastApi (Python)" "Express (NodeJS)" "NestJS (NodeJS)" "Actix (Rust)" "Cancel\n")
RED='\033[31m'
YELLOW='\033[33m'
GREEN='\033[32m'
NC='\033[0m'
PROMT_MESSAGE='echo -e \n"\033[0mCHOOSE THE BACKEND ENVIRONMENT: "\n'
printf "\033[?25l\033[s"
# Prompt the user for a microservice name
echo -n "Enter a name for your microservice: "
read microservice_name
selected=0
# Display options
while true; do
printf "\033[u\n"
$PROMT_MESSAGE
for i in "${!options[@]}"; do
if [ $i -eq $selected ]; then
printf "${GREEN}➤ ${options[$i]}${NC}\n"
else
printf " ${options[$i]}\n"
fi
done
# read user input
read -s -n1 input
# uptade selected option
case $input in
A) # Up Arrow
if [ $selected -gt 0 ]; then
selected=$((selected - 1))
fi
;;
B) # Down Arrow
if [ $selected -lt $((${#options[@]} - 1)) ]; then
selected=$((selected + 1))
fi
;;
"") # Enter
option="${options[$selected]}"
break
;;
*) ;;
esac
done
printf "\033[u\033[?25h"
clear
function move_to_microservice_folder() {
if [ ! -d "api/services" ]; then
mkdir api/services
fi
cd api/services &&
mkdir $microservice_name &&
cd $microservice_name
}
function update_docker_compose() {
docker_compose_route="../../docker-compose.yml"
if ! grep -q "[^[:space:]]" "$docker_compose_route"; then
docker_compose_new_content=$(cat ../../../templates/infraestructure/docker/docker-compose-new-template.yml)
new_docker_compose_content=${docker_compose_new_content//microservice_name/$microservice_name}
echo "$new_docker_compose_content" > "$docker_compose_route"
else
current_port=$(cat "../../../templates/infraestructure/docker/docker_current_port.txt")
new_port=$((current_port + 1))
echo "$new_port" > "../../../templates/infraestructure/docker/docker_current_port.txt"
docker_compose_add_content=$(cat ../../../templates/infraestructure/docker/docker-compose-add-container.txt)
new_port_docker_compose=${docker_compose_add_content//8000/$new_port}
new_docker_compose_add_content=${new_port_docker_compose//microservice_name/$microservice_name}
echo "$new_docker_compose_add_content" >> "$docker_compose_route"
# update dockerfile
dockerfile_content=$(cat dockerfile)
new_dockerfile_content=${dockerfile_content//$current_port/$new_port}
echo "$new_dockerfile_content" > dockerfile
fi
}
# Execute selected option
case $option in
"${options[0]}")
echo "Creating FastApi (Python) microservice $microservice_name ..."
move_to_microservice_folder
# Create a virtual environment & install dependencies
python3 -m venv venv &&
source venv/bin/activate &&
pip install --upgrade pip &&
pip install fastapi uvicorn httpx pytest
pip freeze > requirements.txt
# Copying the template and creating the files
file_content=$(cat ../../../templates/python/fastapi/basic_server.py)
test_content=$(cat ../../../templates/python/fastapi/test_basic_server.py)
dockerfile_content=$(cat ../../../templates/infraestructure/docker/fastapi_dockerfile)
# Replacing the microservice_name string with the name of the microservice
new_content=${file_content//microservice_name/$microservice_name}
# this line get the same result that the line above. It's just a different way to do it
new_test_content=${test_content//microservice_name/$(echo "$microservice_name")}
# Creating the files
echo "$new_content" > main.py
echo "$new_test_content" > test_main.py
echo "$dockerfile_content" > dockerfile
update_docker_compose
printf "\n${GREEN} ${microservice_name} was succesfully created!\n"
;;
"${options[1]}")
echo "Creating Express (NodeJS) microservice $microservice_name ..."
move_to_microservice_folder
# Initialize package.json and install dependencies
npm init -y &&
npm install express &&
npm install --save-dev nodemon
# Copying the template and creating the files
file_content=$(cat ../../../templates/javascript/express/basic_server.js)
dockerfile_content=$(cat ../../../templates/infraestructure/docker/express_dockerfile)
# Replacing the microservice_name string with the name of the microservice
new_content=${file_content//microservice_name/$microservice_name}
# Creating the files
echo "$new_content" > index.js
echo "$dockerfile_content" > dockerfile
# Add "start" script to package.json
sed -i 's/"scripts": {/"scripts": {\n "start": "nodemon index.js",/' package.json
update_docker_compose
printf "\n${GREEN} ${microservice_name} was succesfully created!\n"
;;
"${options[2]}")
echo "Creating NestJS (NodeJS) microservice $microservice_name ..."
move_to_microservice_folder
# Initialize package.json and install dependencies
npm init -y &&
npm install --global @nestjs/cli &&
nest new $microservice_name --skip-install &&
cd $microservice_name &&
npm install &&
npm install --save-dev nodemon
# Copying the template and creating the files
dockerfile_content=$(cat ../../../../templates/infraestructure/docker/express_dockerfile)
# Creating the files
echo "$dockerfile_content" > dockerfile
# Add "start" script to package.json
sed -i 's/"scripts": {/"scripts": {\n "start": "nodemon src/main.ts",/' package.json
update_docker_compose
printf "\n${GREEN} ${microservice_name} was succesfully created!\n"
;;
"${options[3]}")
echo "Creating Rust (Actix) microservice $microservice_name ..."
move_to_microservice_folder
# Install Rust and create a new project
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
cargo new $microservice_name
cd $microservice_name
# Add Actix dependencies to Cargo.toml
echo 'actix-web = "4"' >> Cargo.toml
echo 'actix-rt = "2"' >> Cargo.toml
echo 'serde = { version = "1", features = ["derive"] }' >> Cargo.toml
# Replace src/main.rs with a basic Actix Web app
echo 'use actix_web::{web, App, HttpResponse, HttpServer, Responder};' > src/main.rs
echo 'use serde::Serialize;' >> src/main.rs
# Build the project
cargo build
update_docker_compose
printf "\n${GREEN} ${microservice_name} was succesfully created!\n"
;;
"${options[4]}")
echo "Exiting..."
;;
*) echo "Invalid Option";;
esac