Skip to content

Commit 88a90f3

Browse files
committed
chore: working base server from actix
1 parent 0251731 commit 88a90f3

File tree

47 files changed

+3722
-625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3722
-625
lines changed

.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

.envrc

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

.github/scripts/init-db.sh

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
####################################################################
2+
# Create a diretory for the generated artifacts
3+
####################################################################
4+
5+
mkdir .nix-shell
6+
export NIX_SHELL_DIR=$PWD/.nix-shell
7+
8+
####################################################################
9+
# Put the PostgreSQL databases in the project diretory.
10+
####################################################################
11+
12+
export PGDATA=$NIX_SHELL_DIR/db
13+
14+
####################################################################
15+
# Clean up after exiting the Nix shell using `trap`.
16+
# ------------------------------------------------------------------
17+
# Idea taken from
18+
# https://unix.stackexchange.com/questions/464106/killing-background-processes-started-in-nix-shell
19+
# and the answer provides a way more sophisticated solution.
20+
#
21+
# The main syntax is `trap ARG SIGNAL` where ARG are the commands to
22+
# be executed when SIGNAL crops up. See `trap --help` for more.
23+
####################################################################
24+
25+
trap \
26+
"
27+
######################################################
28+
# Stop PostgreSQL
29+
######################################################
30+
31+
pg_ctl -D $PGDATA stop
32+
33+
######################################################
34+
# Delete `.nix-shell` directory
35+
# ----------------------------------
36+
# The first step is going back to the project root,
37+
# otherwise `.nix-shell` won't get deleted. At least
38+
# it didn't for me when exiting in a subdirectory.
39+
######################################################
40+
41+
cd $PWD
42+
rm -rf "$NIX_SHELL_DIR"
43+
" \
44+
EXIT
45+
46+
####################################################################
47+
# If database is not initialized (i.e., $PGDATA directory does not
48+
# exist), then set it up. Seems superfulous given the cleanup step
49+
# above, but handy when one gets to force reboot the iron.
50+
####################################################################
51+
52+
if ! test -d $PGDATA
53+
then
54+
55+
######################################################
56+
# Init PostgreSQL
57+
######################################################
58+
59+
pg_ctl initdb -D $PGDATA
60+
61+
######################################################
62+
# PORT ALREADY IN USE
63+
######################################################
64+
# If another `nix-shell` is running with a PostgreSQL
65+
# instance, the logs will show complaints that the
66+
# default port 5432 is already in use. Edit the line
67+
# below with a different port number, uncomment it,
68+
# and try again.
69+
######################################################
70+
71+
# sed -i "s|^#port.*$|port = 5433|" $PGDATA/postgresql.conf
72+
73+
fi
74+
75+
####################################################################
76+
# Start PostgreSQL
77+
# ==================================================================
78+
# Setting all necessary configuration options via `pg_ctl` (which
79+
# is basically a wrapper around `postgres`) instead of editing
80+
# `postgresql.conf` directly with `sed`. See docs:
81+
#
82+
# + https://www.postgresql.org/docs/current/app-pg-ctl.html
83+
# + https://www.postgresql.org/docs/current/app-postgres.html
84+
#
85+
# See more on the caveats at
86+
# https://discourse.nixos.org/t/how-to-configure-postgresql-declaratively-nixos-and-non-nixos/4063/1
87+
# but recapping out of paranoia:
88+
#
89+
# > use `SHOW` commands to check the options because `postgres -C`
90+
# > "_returns values from postgresql.conf_" (which is not changed by
91+
# > supplying the configuration options on the command line) and
92+
# > "_it does not reflect parameters supplied when the cluster was
93+
# > started._"
94+
#
95+
# OPTION SUMMARY
96+
# --------------------------------------------------------------------
97+
#
98+
# + `unix_socket_directories`
99+
#
100+
# > PostgreSQL will attempt to create a pidfile in
101+
# > `/run/postgresql` by default, but it will fail as it
102+
# > doesn't exist. By changing the configuration option
103+
# > below, it will get created in $PGDATA.
104+
#
105+
# + `listen_addresses`
106+
#
107+
# > In tandem with edits in `pg_hba.conf` (see
108+
# > `HOST_COMMON` below), it configures PostgreSQL to
109+
# > allow remote connections (otherwise only `localhost`
110+
# > will get authenticated and the rest of the traffic
111+
# > discarded).
112+
# >
113+
# > NOTE: the edit to `pga_hba.conf` needs to come
114+
# > **before** `pg_ctl start` (or the service
115+
# > needs to be restarted otherwise), because then
116+
# > the changes are not being reloaded.
117+
# >
118+
# > More info on setting up and troubleshooting remote
119+
# > PosgreSQL connections (these are all mirrors of the
120+
# > same text; again, paranoia):
121+
# >
122+
# > + https://stackoverflow.com/questions/24504680/connect-to-postgres-server-on-google-compute-engine
123+
# > + https://stackoverflow.com/questions/47794979/connecting-to-postgres-server-on-google-compute-engine
124+
# > + https://medium.com/scientific-breakthrough-of-the-afternoon/configure-postgresql-to-allow-remote-connections-af5a1a392a38
125+
# > + https://gist.github.com/toraritte/f8c7fe001365c50294adfe8509080201#file-configure-postgres-to-allow-remote-connection-md
126+
127+
HOST_COMMON="host\s\+all\s\+all"
128+
sed -i "s|^$HOST_COMMON.*127.*$|host all all 0.0.0.0/0 trust|" $PGDATA/pg_hba.conf
129+
sed -i "s|^$HOST_COMMON.*::1.*$|host all all ::/0 trust|" $PGDATA/pg_hba.conf
130+
131+
# + `log*`
132+
#
133+
# > Setting up basic logging, to see remote connections
134+
# > for example.
135+
# >
136+
# > See the docs for more:
137+
# > https://www.postgresql.org/docs/current/runtime-config-logging.html
138+
139+
pg_ctl \
140+
-D $PGDATA \
141+
-l $PGDATA/postgres.log \
142+
-o "-c unix_socket_directories='$PGDATA'" \
143+
-o "-c listen_addresses='*'" \
144+
-o "-c log_destination='stderr'" \
145+
-o "-c logging_collector=on" \
146+
-o "-c log_directory='log'" \
147+
-o "-c log_filename='postgresql-%Y-%m-%d_%H%M%S.log'" \
148+
-o "-c log_min_messages=info" \
149+
-o "-c log_min_error_statement=info" \
150+
-o "-c log_connections=on" \
151+
start
152+
153+
####################################################################
154+
# Create necessary databases & users.
155+
# ------------------------------------------------------------------
156+
# Basically, for this config, I set all credentials to "temp",
157+
# you're welcomed to change it to what the fuckever as you wish.
158+
#
159+
# Also, before bootstrapping credentials, it creates database
160+
# for current running user as it doesn't get done by default.
161+
####################################################################
162+
163+
export DB_USERNAME=temp
164+
export DB_PASSWORD=temp
165+
export DB_DATABASE=temp
166+
167+
createdb $(whoami) --host=$PGDATA
168+
169+
createdb $DB_DATABASE --host=$PGDATA
170+
createuser $DB_USERNAME --host=$PGDATA
171+
psql --host=$PGDATA --username=$(whoami) --dbname=$(whoami) --port=5432 \
172+
-c "alter user $DB_USERNAME with encrypted password '$DB_PASSWORD';"
173+
psql --host=$PGDATA --username=$(whoami) --dbname=$(whoami) --port=5432 \
174+
-c "grant all privileges on database $DB_DATABASE to $DB_USERNAME;"
175+
176+
####################################################################
177+
# Prepare .env file for the server
178+
####################################################################
179+
180+
if [ ! -f .env ]; then
181+
182+
######################################################
183+
# Generate and warn user about defaulted .env file
184+
######################################################
185+
186+
touch .env
187+
echo "I'm setting temporary local dev settings for now."
188+
echo "If any changes wanted, feel free to change or do what the fuckever you want with .env file!";
189+
190+
######################################################
191+
# Write defaulted settings to .env file
192+
######################################################
193+
194+
echo "# Generated with shell.nix (L251 for more)" > .env;
195+
echo "URL=127.0.0.1" > .env;
196+
echo "PORT=8001" > .env;
197+
echo "DATABASE_URL=postgres://$(whoami):$(whoami)@localhost/temp" > .env;
198+
199+
else
200+
source .env;
201+
fi

.github/scripts/init-service.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
####################################################################
2+
# Clean up after exiting the Nix shell using `trap`.
3+
# ------------------------------------------------------------------
4+
# Idea taken from
5+
# https://unix.stackexchange.com/questions/464106/killing-background-processes-started-in-nix-shell
6+
# and the answer provides a way more sophisticated solution.
7+
#
8+
# The main syntax is `trap ARG SIGNAL` where ARG are the commands to
9+
# be executed when SIGNAL crops up. See `trap --help` for more.
10+
####################################################################
11+
12+
trap \
13+
"
14+
######################################################
15+
# Kill the running instance before exiting shell
16+
######################################################
17+
# kill $CARGO_WATCH_PID
18+
" \
19+
EXIT
20+
21+
####################################################################
22+
# Starting server with auto hot-reload for further debugging
23+
# ==================================================================
24+
# Change and uncomment necessary settings from configurations
25+
# provided below.
26+
####################################################################
27+
28+
######################################################
29+
# Binding everything to cargo-watch
30+
######################################################
31+
# cargo watch -x "run --bin "cli" &
32+
33+
######################################################
34+
# Store the PID of the background process
35+
######################################################
36+
# CARGO_WATCH_PID=$!

.github/workflows/test.yml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
name: "Test CI"
1+
name: Test CI
22

33
on: [push, pull_request]
44

55
jobs:
6-
tests:
6+
test-flake:
77
runs-on: ubuntu-latest
8+
89
steps:
9-
- name: Clone the repository
10+
- name: Check out the repository
1011
uses: actions/checkout@v4
1112

1213
- name: Install Nix
@@ -15,20 +16,24 @@ jobs:
1516
- name: Cache Nix store
1617
uses: DeterminateSystems/magic-nix-cache-action@main
1718

18-
- name: Check for flake configurations
19+
- name: Check up flake configuration
1920
run: nix flake check --all-systems --show-trace
2021

21-
- name: Build the project
22-
run: nix-shell --run "cargo build --release --verbose"
22+
test-project:
23+
runs-on: ubuntu-latest
2324

24-
- name: Run lint
25-
run: nix-shell --run "cargo clippy --verbose"
25+
steps:
26+
- name: Check out the repository
27+
uses: actions/checkout@v4
2628

27-
- name: Run format check
28-
run: nix-shell --run "cargo fmt --all -- --check"
29+
- name: Install Nix
30+
uses: DeterminateSystems/nix-installer-action@main
31+
32+
- name: Cache Nix store
33+
uses: DeterminateSystems/magic-nix-cache-action@main
2934

30-
- name: Run tests
31-
run: nix-shell --run "cargo test --verbose"
35+
- name: Build the project with Nix
36+
run: nix build
3237

33-
- name: Build nix package
34-
run: nix build .
38+
- name: Run all cargo tests
39+
run: nix develop --command -- cargo test

.gitignore

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
# Generated by Cargo
2-
# will have compiled files and executables
3-
debug/
4-
target/
1+
# Nix builds
2+
/result
53

6-
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7-
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8-
# Cargo.lock
4+
# Cargo builds
5+
/target
96

10-
# These are backup files generated by rustfmt
11-
**/*.rs.bk
7+
# Environmental variables
8+
.env
129

13-
# MSVC Windows builds of rustc generate these, which store debugging information
14-
*.pdb
10+
# Production config file
11+
config.toml
1512

16-
# RustRover
17-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
18-
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
19-
# and can be added to the global gitignore or merged into this file. For a more nuclear
20-
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
21-
#.idea/
22-
23-
# Nix shell generated artifacts
13+
# Shell temporary files
2414
.nix-shell
15+
16+
# Postgresql logfiles
17+
logfile

0 commit comments

Comments
 (0)