aiiinotate 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,23 +8,16 @@ aiiinotate is a fast and lightweight annotation server for IIIF. It relies on `n
8
8
 
9
9
  ### Install
10
10
 
11
- TODO: install mongodb
11
+ 1. **Install mongodb**.
12
+ - see [dev installation script for help](./scripts/setup_mongodb.sh)
13
+ - checkout the [official installation guide](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/#std-label-install-mdb-community-ubuntu)
12
14
 
15
+ 2. **Install aiiinotate**
13
16
  ```bash
14
17
  npm install aiiinotate
15
18
  ```
16
19
 
17
- ### Usage
18
-
19
- The base command is:
20
-
21
- ```bash
22
- aiiinotate --env <path-to-your-env-file> -- <command>
23
- ```
24
-
25
- It will give full access to the CLI interface of Aiiinotate.
26
-
27
- #### Run the app
20
+ ### Setup the app
28
21
 
29
22
  0. **Setup your `.env`** file after [.env.template](./config/.env.template).
30
23
 
@@ -40,7 +33,11 @@ sudo systemctl start mongod
40
33
  aiiinotate --env <path-to-your-env-file> -- migrate apply
41
34
  ```
42
35
 
43
- 3. **Run**
36
+ ### Usage
37
+
38
+ All commands are accessible through a CLI (`./src/cli`).
39
+
40
+ #### Run the app
44
41
 
45
42
  ```bash
46
43
  aiiinotate --env <path-to-your-env-file> -- serve prod
@@ -48,9 +45,17 @@ aiiinotate --env <path-to-your-env-file> -- serve prod
48
45
  aiiinotate --env <path-to-your-env-file> -- serve dev
49
46
  ```
50
47
 
51
- #### Import data
48
+ #### Run administration commands
52
49
 
53
- TODO
50
+ The base command is:
51
+
52
+ ```bash
53
+ aiiinotate --env <path-to-your-env-file> -- <command>
54
+ ```
55
+
56
+ It will give full access to the CLI interface of Aiiinotate. Run `aiiinotate --help` for more info.
57
+
58
+ 1. Import data - TODO
54
59
 
55
60
  ---
56
61
 
@@ -59,12 +64,25 @@ TODO
59
64
  ### Install
60
65
 
61
66
  ```bash
62
- bash setup.sh
67
+ # clone the repo
68
+ git clone git@github.com:Aikon-platform/aiiinotate.git
69
+
70
+ # move inside it
71
+ cd aiiinotate
72
+
73
+ # install mongodb
74
+ bash ./scripts/setup_mongodb.sh
75
+
76
+ # install node
77
+ bash ./scripts/setup_node.sh
78
+
79
+ # install dependencies
80
+ npm i
63
81
  ```
64
82
 
65
- ### Usage
83
+ ### Setup
66
84
 
67
- #### First steps
85
+ After installing, some setup must be done
68
86
 
69
87
  0. **Setup your `.env`** file after [.env.template](./config/.env.template) and place it at `./config/.env`.
70
88
 
@@ -74,7 +92,15 @@ bash setup.sh
74
92
  sudo systemctl start mongod
75
93
  ```
76
94
 
77
- #### Run commands
95
+ 2. **Configure the database**
96
+
97
+ ```bash
98
+ npm run migrate apply
99
+ ```
100
+
101
+ ### Usage
102
+
103
+ Remember to have your `mongodb` service running: `sudo systemctl start mongod` !
78
104
 
79
105
  - **Start the app**
80
106
 
@@ -98,7 +124,7 @@ npm cli
98
124
 
99
125
  ```bash
100
126
  # create a new migration. NOTE: the `--` is necessary !
101
- npm run migrate make -- --migrate-name <your migration name>
127
+ npm run migrate make -- --migration-name <your migration name>
102
128
 
103
129
  # apply all pending migrations
104
130
  npm run migrate apply
@@ -107,7 +133,7 @@ npm run migrate apply
107
133
  npm run migrate revert
108
134
 
109
135
  # revert all migrations
110
- npm run migrate revert-all)
136
+ npm run migrate revert-all
111
137
  ```
112
138
 
113
139
  ---
@@ -6,7 +6,10 @@ import { MongoClient } from "mongodb";
6
6
  */
7
7
  function loadMongoClient() {
8
8
  try {
9
- const client = new MongoClient(process.env.MONGODB_CONNSTRING);
9
+ const client = new MongoClient(
10
+ process.env.MONGODB_CONNSTRING,
11
+ { useUnifiedTopology: false, useNewUrlParser: false }
12
+ );
10
13
  client.db(process.env.MONGODB_DB);
11
14
  return client;
12
15
  } catch (err) {
@@ -0,0 +1,23 @@
1
+ # syntax=docker/dockerfile:1
2
+ FROM node:23.11 AS aiiinotate
3
+
4
+ # aiiinotate port
5
+ ARG PORT=4444
6
+ ENV PORT=${PORT}
7
+
8
+ # set up environment
9
+ ENV TERM=linux
10
+ SHELL ["/bin/bash", "-c"]
11
+
12
+ # root of the app in the docker container
13
+ WORKDIR /aiiinotate
14
+ # copy the .env in the docker container
15
+ COPY ./config/.env /aiiinotate/.env
16
+ # install the app as an NPM library
17
+ RUN npm i -g aiiinotate
18
+ # migrate the database
19
+ RUN aiiinotate --env=/aiiinotate/.env -- migrate apply
20
+ # expose the used port
21
+ EXPOSE $PORT
22
+ # start the app
23
+ CMD aiiinotate --env=/aiiinotate/.env -- serve prod
@@ -0,0 +1,21 @@
1
+ services:
2
+ mongo:
3
+ image: mongo:8
4
+ restart: always
5
+ # environment:
6
+ # MONGO_INITDB_ROOT_USERNAME: root
7
+ # MONGO_INITDB_ROOT_PASSWORD: example
8
+
9
+ web:
10
+ build:
11
+ context: ..
12
+ dockerfile: docker/Dockerfile
13
+ args:
14
+ PORT: ${APP_PORT}
15
+ env_file:
16
+ - ../config/.env
17
+ ports:
18
+ - ${APP_PORT}:${APP_PORT}
19
+ depends_on:
20
+ - mongo
21
+ restart: always
@@ -24,8 +24,8 @@ export default (config) => ({
24
24
  databaseName: config.dbName,
25
25
 
26
26
  options: {
27
- useNewUrlParser: true, // removes a deprecation warning when connecting
28
- useUnifiedTopology: true, // removes a deprecating warning when connecting
27
+ // useNewUrlParser: true, // removes a deprecation warning when connecting
28
+ // useUnifiedTopology: true, // removes a deprecating warning when connecting
29
29
  // connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
30
30
  // socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
31
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiiinotate",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "a fast IIIF-compliant annotation server",
5
5
  "main": "./cli/index.js",
6
6
  "type": "module",
package/_run.sh DELETED
@@ -1,67 +0,0 @@
1
- #!/bin/env bash
2
-
3
- source "./scripts/utils.sh";
4
-
5
- SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6
- ENV_FILE="$SCRIPT_DIR/config/.env";
7
-
8
- print_usage() {
9
- cat<<EOF
10
-
11
- USAGE bash run.sh [-s, -d, -p, -t, -c, -h]
12
-
13
- (use from the scripts defined in 'package.json': 'npm start')
14
-
15
- -s: setup the app
16
- -t: test the app
17
- -d: run the app in dev mode
18
- -p: run the app in prod mode
19
- -c: run the command line interface
20
- -h: print help and exit
21
-
22
- EOF
23
- }
24
-
25
- start () {
26
- local mode="$1"
27
-
28
- if [ ! -f "$ENV_FILE" ];
29
- then echo -e "\nERROR: .env file not found at '$ENV_FILE'. exiting..." && exit 1;
30
- fi;
31
-
32
- start_mongod
33
-
34
- if [ "$mode" = "dev" ]; then
35
- dotenvx run -f "$ENV_FILE" -- \
36
- node --watch "$SCRIPT_DIR/src/server.js";
37
- elif [ "$mode" = "test" ]; then
38
- dotenvx run -f "$ENV_FILE" -- \
39
- node --test --test-isolation=none;
40
- elif [ "$mode" = "cli" ] ; then
41
- dotenvx run -f "$ENV_FILE" -- \
42
- node "$SCRIPT_DIR/cli/index.js";
43
- else echo -e "\nERROR: mode not implemented: '$mode'\n"; print_usage; exit 1;
44
- fi;
45
- }
46
-
47
- while getopts 'hdptcs' mode_flag; do
48
- case "${mode_flag}" in
49
- s) MODE="setup"
50
- break;;
51
- d) MODE="dev"
52
- break;;
53
- p) MODE="prod"
54
- break;;
55
- t) MODE="test"
56
- break;;
57
- c) MODE="cli"
58
- break;;
59
- h) print_usage
60
- exit 0;;
61
- *) print_usage
62
- exit 1;;
63
- esac
64
- done
65
-
66
- start "$MODE";
67
-
@@ -1,79 +0,0 @@
1
- #!/bin/env bash
2
-
3
- # use through package.json scripts.
4
-
5
- # these sripts are used to manage all migrations in parallel
6
- # on both the main database and the test database.
7
- #NOTE migrate-init is not implemented.
8
-
9
- SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
10
-
11
- source "$SCRIPT_DIR/utils.sh" || exit 1;
12
-
13
- MIGRATIONS_CONFIG_MAIN="$MIGRATIONS_DIR/migrate-mongo-config-main.js";
14
- MIGRATIONS_CONFIG_TEST="$MIGRATIONS_DIR/migrate-mongo-config-test.js";
15
- MIGRATIONS_CONFIGS=("$MIGRATIONS_CONFIG_MAIN" "$MIGRATIONS_CONFIG_TEST");
16
-
17
- ##################################################
18
- # functions
19
-
20
- # create a migration
21
- # NOTE `migrate-mongo create` generates a timestamped file and, since this script is run twice
22
- # (once per config file), 2 migration files are created. so what we do is mimic migrate-make by
23
- # copying a blank template file to the `migrationScripts` directory.
24
- migrate_make() {
25
- config_fp=$1; # path to config file
26
- migration_name="$2"; # name of migration to create
27
-
28
- if [ -z "$migration_name" ];
29
- then echo "ERROR. a migration name must be given."; exit 1;
30
- fi;
31
-
32
- cp "$MIGRATIONS_DIR/migrationTemplate.js" "$MIGRATIONS_DIR/migrationScripts/$(date +'%Y%m%d%H%M%S')-$migration_name.js";
33
- }
34
-
35
- # apply migrations
36
- migrate_apply() {
37
- config_fp=$1; # path to config file
38
- dotenvx run -f "$ENV_FILE" -- npx migrate-mongo up -f "$config_fp";
39
- }
40
-
41
- # rvert the last migration
42
- migrate_revert() {
43
- config_fp=$1; # path to config file
44
- dotenvx run -f "$ENV_FILE" -- npx migrate-mongo down -f "$config_fp";
45
- }
46
-
47
- # undo all migrations
48
- migrate_revert_all() {
49
- config_fp=$1; # path to config file
50
-
51
- for _ in "$MIGRATIONS_DIR"/migrationScripts/*;
52
- do dotenvx run -f "$ENV_FILE" -- npx migrate-mongo down -f "$config_fp";
53
- done;
54
- }
55
-
56
-
57
- ##################################################
58
- # cli
59
-
60
- OP=$1
61
- MIGRATION_NAME=$2 # only used by `migrate_make`
62
-
63
- case "$OP" in
64
- make) FUNC=migrate_make;;
65
- apply) FUNC=migrate_apply;;
66
- revert) FUNC=migrate_revert;;
67
- revertAll) FUNC=migrate_revert_all;;
68
- esac;
69
-
70
- if [ -z "$FUNC" ];
71
- then echo "ERROR: unrecognized keyword '$OP'. please use one of 'make', 'apply', 'revert', 'revertAll'. exiting..."; exit 1;
72
- fi;
73
-
74
- start_mongod
75
-
76
- for config_fp in "${MIGRATIONS_CONFIGS[@]}"; do
77
- $FUNC "$config_fp" "$MIGRATION_NAME";
78
- done
79
-
package/scripts/_setup.js DELETED
@@ -1,31 +0,0 @@
1
- #!usr/bin/env node
2
- import path from "node:path";
3
- import { execSync } from "node:child_process";
4
- import { fileURLToPath } from "node:url";
5
-
6
- import { MongoClient } from "mongodb";
7
-
8
- const
9
- // path to dirctory of curent file
10
- dirScripts = path.dirname(fileURLToPath(import.meta.url)),
11
- dirRoot = path.resolve(dirScripts, ".."),
12
- dirMigrations = path.resolve(dirRoot, "migrations");
13
-
14
- const connString = process.env.MONGODB_CONNSTRING;
15
-
16
- (async () => {
17
- let client;
18
- try {
19
- client = new MongoClient(connString);
20
- await client.connect();
21
- console.log("CONNECTED !")
22
- console.log(dirScripts);
23
- console.log(dirRoot);
24
- console.log(dirMigrations);
25
- } finally {
26
- if ( client != null ) {
27
- client.close()
28
- }
29
- }
30
- }
31
- )();
@@ -1,17 +0,0 @@
1
- #!/bin/env bash
2
-
3
- #NOTE : this setup creates and populates our db WITHOUT users or authentication.
4
- # it is possible to add users and auth to a mongodb instance, but
5
- # - it is a bit convoluted in itself
6
- # - it is difficult to automate: we would need to
7
- # - create a root user + a user for the app
8
- # - create this app user by generating mongosh scripts from the user's .env files (so use Python to write custom JS)
9
- # - update the mongodb conf file so that the systemd mongodb service uses auth login, which would need to parse YAML, so to use python
10
-
11
- SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
12
- ROOT_DIR="$SCRIPT_DIR/../"
13
-
14
- cd "$ROOT_DIR"
15
-
16
- npm run migrate-apply
17
-
package/scripts/utils.sh DELETED
@@ -1,192 +0,0 @@
1
- #!/bin/env bash
2
-
3
- # directory of the current script
4
- SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
5
- # root of the app
6
- ROOT_DIR=$( dirname "$SCRIPT_DIR" )
7
- # src/ directory
8
- SRC_DIR="$ROOT_DIR/src"
9
- # migrations/ dir
10
- MIGRATIONS_DIR="$ROOT_DIR/migrations";
11
- # .env file
12
- ENV_FILE="$SRC_DIR/config/.env"
13
-
14
-
15
- color_echo() {
16
- Color_Off="\033[0m"
17
- Red="\033[1;91m" # Red
18
- Green="\033[1;92m" # Green
19
- Yellow="\033[1;93m" # Yellow
20
- Blue="\033[1;94m" # Blue
21
- Purple="\033[1;95m" # Purple
22
- Cyan="\033[1;96m" # Cyan
23
-
24
- case "$1" in
25
- "green") echo -e "$Green$2$Color_Off";;
26
- "red") echo -e "$Red$2$Color_Off";;
27
- "blue") echo -e "$Blue$2$Color_Off";;
28
- "yellow") echo -e "$Yellow$2$Color_Off";;
29
- "purple") echo -e "$Purple$2$Color_Off";;
30
- "cyan") echo -e "$Cyan$2$Color_Off";;
31
- *) echo "$2";;
32
- esac
33
- }
34
-
35
- echo_title(){
36
- sep_line="========================================"
37
- len_title=${#1}
38
-
39
- if [ "$len_title" -gt 40 ]; then
40
- sep_line=$(printf "%0.s=" $(seq 1 $len_title))
41
- title="$1"
42
- else
43
- diff=$((38 - len_title))
44
- half_diff=$((diff / 2))
45
- sep=$(printf "%0.s=" $(seq 1 $half_diff))
46
-
47
- if [ $((diff % 2)) -ne 0 ]; then
48
- title="$sep $1 $sep="
49
- else
50
- title="$sep $1 $sep"
51
- fi
52
- fi
53
-
54
- color_echo purple "\n\n$sep_line\n$title\n$sep_line"
55
- }
56
-
57
- get_os() {
58
- unameOut="$(uname -s)"
59
- case "${unameOut}" in
60
- Linux*) os=Linux;;
61
- Darwin*) os=Mac;;
62
- CYGWIN*) os=Cygwin;;
63
- MINGW*) os=MinGw;;
64
- MSYS_NT*) os=Git;;
65
- *) os="UNKNOWN:${unameOut}"
66
- esac
67
- echo "${os}"
68
- }
69
-
70
- export OS
71
- OS=$(get_os)
72
-
73
- # gets a password and validates it by running a dummy cmd.
74
- # parent process must call the function with `get_password || exit` to exit the script if `SUDO_PSW` is invalid
75
- get_password() {
76
- if [ -z "$SUDO_PSW" ]; then
77
- read -s -p "Enter your sudo password: " SUDO_PSW
78
- echo
79
- echo "$SUDO_PSW" | sudo -S whoami > /dev/null 2>&1
80
- if [ $? -ne 0 ]; then
81
- echo "Invalid sudo password. Exiting..."
82
- return 1
83
- fi
84
- return 0
85
- fi
86
- }
87
-
88
- # the sed at the end removes trailing non-alphanumeric chars.
89
- generate_random_string() {
90
- echo "$(openssl rand -base64 32 | tr -d '/\n' | sed -r -e "s/[^a-zA-Z0-9]+$//")"
91
- }
92
-
93
- get_env_value() {
94
- param=$1
95
- env_file=$2
96
- value=$(awk -F= -v param="$param" '/^[^#]/ && $1 == param {gsub(/"/, "", $2); print $2}' "$env_file")
97
- echo "$value"
98
- }
99
-
100
- get_env_desc() {
101
- current_line="$1"
102
- prev_line="$2"
103
- desc=""
104
- if [[ $prev_line =~ ^# ]]; then
105
- desc=$(echo "$prev_line" | sed 's/^#\s*//')
106
- fi
107
- echo "$desc"
108
- }
109
-
110
- ask() {
111
- options=("yes" "no")
112
- color_echo blue "$1"
113
- answer=$(printf "%s\n" "${options[@]}" | fzy)
114
- echo ""
115
- if [ "$answer" = "no" ]; then
116
- exit 1
117
- fi
118
- }
119
-
120
- # float arithmetic comparison is not supported by bash and we need to use `bc`
121
- # usage: if float_comparison "a >= b"; then... ; fi
122
- float_comparison () {
123
- expr="$1"
124
- (( $(echo "$expr" |bc -l) ));
125
- }
126
-
127
- # sed replacements in place
128
- # `sed -i` can't be used in the same way with Linux and Mac: it's `sed -i` on Linux, `sed -i ""` on Mac
129
- sed_repl_inplace() {
130
- sed_expr="$1"
131
- file="$2"
132
-
133
- if [ "$OS" = "Linux" ]; then
134
- sed -i -e "$sed_expr" "$file"
135
- else
136
- sed -i "" -e "$sed_expr" "$file"
137
- fi
138
- }
139
-
140
- # sudo does not inherit from bash functions so this is a copy of "sed_repl_inplace" with sudo privileges (see: https://stackoverflow.com/a/9448969)
141
- sudo_sed_repl_inplace() {
142
- sed_expr="$1"
143
- file="$2"
144
-
145
- if [ "$OS" = "Linux" ]; then
146
- [ -n "$SUDO_PSW" ] && echo "$SUDO_PSW" | sudo -S sed -i -e "$sed_expr" "$file" || sudo sed -i -e "$sed_expr" "$file"
147
- else
148
- [ -n "$SUDO_PSW" ] && echo "$SUDO_PSW" | sudo -S sed -i "" -e "$sed_expr" "$file" || sudo sed -i "" -e "$sed_expr" "$file"
149
- fi
150
- }
151
-
152
- # ed replacements to a new file
153
- sed_repl_newfile() {
154
- sed_expr="$1"
155
- infile="$2"
156
- outfile="$3"
157
-
158
- sed "$sed_expr" "$infile" | tee "$outfile" > /dev/null
159
- }
160
-
161
- sudo_sed_repl_newfile() {
162
- sed_expr="$1"
163
- infile="$2"
164
- outfile="$3"
165
-
166
- sudo sed "$sed_expr" "$infile" | sudo tee "$outfile" > /dev/null
167
- }
168
-
169
- run_script() {
170
- local script_name="$1"
171
- local description="$2"
172
- local SCRIPT_DIR=${3:-${SCRIPT_DIR}}
173
- options=("yes" "no")
174
-
175
- color_echo blue "Do you want to run $description?"
176
- answer=$(printf "%s\n" "${options[@]}" | fzy)
177
- echo ""
178
- if [ "$answer" = "yes" ]; then
179
- bash "$SCRIPT_DIR/$script_name" \
180
- && color_echo green "$description completed successfully" \
181
- || color_echo red "$description failed with exit code. Continuing..."
182
- else
183
- color_echo cyan "Skipping $description"
184
- fi
185
- echo ""
186
- }
187
-
188
- start_mongod() {
189
- if ! systemctl is-active --quiet mongod;
190
- then sudo systemctl start mongod;
191
- fi;
192
- }
package/setup.sh DELETED
@@ -1,20 +0,0 @@
1
- #!/bin/env bash
2
-
3
- source "./scripts/utils.sh";
4
-
5
- # SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6
-
7
- color_echo blue "\nInstalling prompt utility fzy..."
8
- if [ "$OS" = "Linux" ]; then
9
- sudo apt install fzy
10
- elif [ "$OS" = "Mac" ]; then
11
- brew install fzy
12
- else
13
- color_echo red "Unsupported OS: $OS"
14
- exit 1
15
- fi
16
-
17
- #NOTE node needs to be installed for mongodb to run, so order is important
18
- run_script "setup_node.sh" "Node and webapp packages installation"
19
- run_script "setup_mongodb.sh" "MongoDB installation"
20
- run_script "setup_mongodb_migrate.sh" "MongoDB database creation"