Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 1ae57bf

Browse files
committed
First version that works in a real project
1 parent 980d32c commit 1ae57bf

File tree

11 files changed

+117
-47
lines changed

11 files changed

+117
-47
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

CHANGES_FROM_WS4SQLITE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
- URL to contact for POST is now `http://<host>:<port>/<db_name&gt/exec` (note the `/exec/`)
44
- Read-only mode is performed via the `query_only` PRAGMA
55
- Even if the database is read only, it's possible to perform init macros
6+
- YAML file, when explicitly given, is separated with a "::" from the db file/name (to solve ambiguities in windows)
67
- Stored Statements are now prefixed with `^`, not `#`, because it can be used in macros that are defined in the YAML file, where `#` would be a comment
78
- ScheduledTasks are replaced with the new macro and backup subsystems
89
- Avoid use of scheduler logic, as it is very language-dependent and may be different from cron, with which the user is probably familiar
910
- The '%s' in the backup template is optional
1011
- The backup files' pattern is specified as a backup directory, being intended that all the files in it are subject to deletion
1112
- HTTP authentication is now named HTTP_BASIC (possible future JWT implementation)
12-
- disableWALMode is now a more generic journalMode, for when WAL2 will be released
13+
- disableWALMode is now a more generic journalMode, for when WAL2 will be released
14+
- it is possible to specify the index file when serving a directory

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# FROM rust:latest as build
2+
#
3+
# WORKDIR /app
4+
# COPY . .
5+
#
6+
# RUN cargo build
7+
# RUN pwd
8+
# RUN ls -al target/debug
9+
# --release
10+
11+
FROM alpine:latest as build
12+
13+
RUN apk add --no-cache rust cargo sqlite-libs sqlite-dev
14+
15+
COPY . /build
16+
WORKDIR /build
17+
RUN ["cargo", "build", "--release"]
18+
19+
# Now copy it into our base image.
20+
FROM alpine:latest
21+
22+
COPY --from=build /build/target/release/sqliterg /
23+
24+
# TODO why libgcc?
25+
RUN apk add --no-cache sqlite-libs libgcc
26+
27+
EXPOSE 12321
28+
VOLUME /data
29+
30+
CMD ["/sqliterg"]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright (c) 2023-, Germano Rizzo <oss /AT/ germanorizzo /DOT/ it>
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ update:
2727

2828
lint:
2929
cargo clippy 2> clippy_results.txt
30+
31+
docker:
32+
docker buildx build . --no-cache -t germanorizzo/sqliterg:latest --push

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Manage VACUUM
12
- Complete startup messages (for macros at init, ecc.)
23
- Comments
34
- Documentation
@@ -19,3 +20,4 @@
1920
# Test
2021

2122
- CORS
23+
- ~

src/commandline.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ pub struct AppConfig {
2929
help = "The host to bind"
3030
)]
3131
pub bind_host: String,
32-
#[arg(long, value_name = "DB_PATH", help = "Repeatable; paths of file-based databases", num_args = 0..)]
32+
#[arg(long, value_name = "DB_PATH", help = "Repeatable; paths of file-based databases [format: \"dbFilePath[::configFilePath]\"]", num_args = 0..)]
3333
pub db: Vec<String>,
34-
#[arg(long, value_name = "MEM_DB", help = "Repeatable; config for memory-based databases (format: ID[:configFilePath])", num_args = 0..)]
34+
#[arg(long, value_name = "MEM_DB", help = "Repeatable; config for memory-based databases [format: \"ID[::configFilePath]\"]", num_args = 0..)]
3535
pub mem_db: Vec<String>,
3636
#[arg(
3737
short,
@@ -47,6 +47,13 @@ pub struct AppConfig {
4747
help = "A directory to serve with builtin HTTP server"
4848
)]
4949
pub serve_dir: Option<String>,
50+
#[arg(
51+
long,
52+
value_name = "FILE",
53+
help = "If --serve-dir is configured, the file to treat as index.",
54+
default_value = "index.html"
55+
)]
56+
pub index_file: String,
5057
}
5158

5259
pub fn parse_cli() -> AppConfig {

src/commons.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ pub fn resolve_tilde(p: &String) -> String {
139139
shellexpand::tilde(p).into_owned()
140140
}
141141

142-
pub fn split_on_first_colon(input: &str) -> (String, String) {
143-
let mut parts = input.splitn(2, ':');
142+
pub fn split_on_first_double_colon(input: &str) -> (String, String) {
143+
let mut parts = input.splitn(2, "::");
144144
let first_part = parts.next().unwrap_or_default().to_string();
145145
let second_part = parts.next().unwrap_or_default().to_string();
146146

src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ async fn main() -> std::io::Result<()> {
7070
let db_map = compose_db_map(&cli);
7171

7272
if let Some(sd) = &cli.serve_dir {
73-
println!(" - serving directory: {}", sd);
73+
println!("- serving directory: {}", sd);
74+
println!(" - with index file: {}", &cli.index_file);
7475
};
7576

7677
let app_lambda = move || {
7778
let dir = cli.serve_dir.to_owned();
79+
let index_file = cli.index_file.to_owned();
7880
let mut a = App::new();
7981
for (db_name, db_conf) in db_map.iter() {
8082
let scop: Scope = scope(format!("/{}", db_name.to_owned()).deref())
@@ -99,7 +101,7 @@ async fn main() -> std::io::Result<()> {
99101
}
100102

101103
if let Some(dir) = dir {
102-
a = a.service(Files::new("/", dir));
104+
a = a.service(Files::new("/", dir).index_file(index_file));
103105
};
104106
a
105107
};

src/main_config.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use rusqlite::Connection;
2121
use crate::backup::{bootstrap_backup, periodic_backup};
2222
use crate::commandline::AppConfig;
2323
use crate::commons::{
24-
abort, assert, file_exists, if_abort_rusqlite, is_dir, resolve_tilde, split_on_first_colon,
24+
abort, assert, file_exists, if_abort_rusqlite, is_dir, resolve_tilde,
25+
split_on_first_double_colon,
2526
};
2627
use crate::db_config::{parse_dbconf, DbConfig, Macro};
2728
use crate::macros::{bootstrap_db_macros, count_macros, periodic_macro, resolve_macros};
@@ -38,17 +39,22 @@ pub struct Db {
3839
pub macros: HashMap<String, Macro>,
3940
}
4041

41-
fn to_base_name(path: &String) -> String {
42-
let path = Path::new(&path);
43-
path.file_stem().unwrap().to_str().unwrap().to_string()
44-
}
42+
fn split_path(path: &str) -> (String, String, String) {
43+
// returns (db_path, yaml, db_name)
44+
let (mut db_path, mut yaml) = split_on_first_double_colon(path);
45+
db_path = resolve_tilde(&db_path);
46+
let path = Path::new(&db_path);
47+
if yaml.is_empty() {
48+
let file_stem = path.file_stem().unwrap().to_str().unwrap();
49+
let yaml_file_name = format!("{file_stem}.yaml");
50+
let yaml_path = path.with_file_name(yaml_file_name);
51+
yaml = yaml_path.to_str().unwrap().to_string();
52+
}
53+
let yaml = resolve_tilde(&yaml);
54+
55+
let db_name = path.file_stem().unwrap().to_str().unwrap().to_string();
4556

46-
fn to_yaml_path(path: &String) -> String {
47-
let path = Path::new(&path);
48-
let file_stem = path.file_stem().unwrap().to_str().unwrap();
49-
let yaml_file_name = format!("{file_stem}.yaml");
50-
let yaml_path = path.with_file_name(yaml_file_name);
51-
yaml_path.to_str().unwrap().to_string()
57+
(db_path, yaml, db_name)
5258
}
5359

5460
fn compose_single_db(
@@ -202,11 +208,9 @@ pub fn compose_db_map(cl: &AppConfig) -> HashMap<String, Db> {
202208
let mut db_map = HashMap::new();
203209
let mut mutexes = HashMap::new();
204210
for db_path in &cl.db {
205-
let db_path = resolve_tilde(db_path);
206-
let db_name = to_base_name(&db_path);
211+
let (db_path, yaml, db_name) = split_path(db_path);
207212
check_db_name(&db_name, &db_map);
208213

209-
let yaml = to_yaml_path(&db_path);
210214
let is_new_db = !file_exists(&db_path);
211215

212216
let (db_cfg, conn) =
@@ -216,7 +220,7 @@ pub fn compose_db_map(cl: &AppConfig) -> HashMap<String, Db> {
216220
mutexes.insert(db_name.to_owned(), Mutex::new(conn));
217221
}
218222
for db in &cl.mem_db {
219-
let (db_name, yaml) = split_on_first_colon(db);
223+
let (db_name, yaml) = split_on_first_double_colon(db);
220224
check_db_name(&db_name, &db_map);
221225

222226
let yaml = resolve_tilde(&yaml);

0 commit comments

Comments
 (0)