This compilation is gonna kill me
							parent
							
								
									a295237863
								
							
						
					
					
						commit
						3d024db2e9
					
				
							
								
								
									
										10
									
								
								Dockerfile
								
								
								
								
							
							
						
						
									
										10
									
								
								Dockerfile
								
								
								
								
							|  | @ -1,11 +1,19 @@ | |||
| FROM rust:1.54 | ||||
| 
 | ||||
| ENV PREFIX="/usr/src/out/prefix" \ | ||||
|     CC="musl-gcc -fPIC -pie -static" \ | ||||
|     LD_LIBRARY_PATH="$PREFIX" \ | ||||
|     PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" \ | ||||
|     PATH="/usr/local/bin:/root/.cargo/bin:$PATH" | ||||
| 
 | ||||
| RUN apt update && \ | ||||
|     apt install -y --no-install-recommends \ | ||||
|         musl-dev \ | ||||
|         musl-tools \ | ||||
|         libpq-dev \ | ||||
|         libssl-dev && \ | ||||
|     rustup target add x86_64-unknown-linux-musl | ||||
|     rustup target add x86_64-unknown-linux-musl && \ | ||||
|     mkdir "$PREFIX" && \ | ||||
|     echo "$PREFIX/lib" >> /etc/ld-musl-x86_64.path | ||||
| 
 | ||||
| WORKDIR /usr/src/app | ||||
|  |  | |||
							
								
								
									
										68
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										68
									
								
								Makefile
								
								
								
								
							|  | @ -22,9 +22,9 @@ CORES       != nproc | |||
| 
 | ||||
| 
 | ||||
| # =====ENVIRONMENT VARIABLES=====
 | ||||
| export CC=musl-gcc -fPIC -pie -static | ||||
| export LD_LIBRARY_PATH=$(PREFIX) | ||||
| export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig | ||||
| export CC := musl-gcc -fPIC -pie -static | ||||
| export LD_LIBRARY_PATH := $(PREFIX) | ||||
| export PKG_CONFIG_PATH := /usr/local/lib/pkgconfig | ||||
| export PATH := /usr/local/bin:/root/.cargo/bin:$(PATH) | ||||
| 
 | ||||
| 
 | ||||
|  | @ -34,18 +34,23 @@ export PATH := /usr/local/bin:/root/.cargo/bin:$(PATH) | |||
| $(shell mkdir -p "$(PREFIX)") | ||||
| 
 | ||||
| 
 | ||||
| # ====RECIPES====
 | ||||
| # =====BUILDING THE STATIC BINARY=====
 | ||||
| .PHONY: all | ||||
| all: build | ||||
| 
 | ||||
| .PHONY: builder | ||||
| builder: | ||||
| 	docker build \
 | ||||
| 		-t rusty-builder:latest - < docker/Dockerfile.builder | ||||
| 
 | ||||
| .PHONY: docker | ||||
| docker: | ||||
| docker: builder | ||||
| 	docker run \
 | ||||
| 		--rm \
 | ||||
| 		-v "$$PWD:/usr/src" \
 | ||||
| 		--workdir "/usr/src" \
 | ||||
| 		-it \
 | ||||
| 		rust:1.54 \
 | ||||
| 		rusty-builder:latest \
 | ||||
| 		bash build.sh | ||||
| 
 | ||||
| 
 | ||||
|  | @ -54,10 +59,9 @@ docker: | |||
| build: libpq | ||||
| 
 | ||||
| .PHONY: clean | ||||
| clean: | ||||
| 	echo "$$PATH" | ||||
| clean: clean-openssl clean-libpq clean-di | ||||
| 	@ echo "Note: this only cleans the C dependencies, not the Cargo cache." | ||||
| 	rm -rf "$(PQ_DIR)" "$(OPENSSL_DIR)" "$(DI_DIR)" "$(PREFIX)" | ||||
| 	rm -rf "$(PREFIX)" | ||||
| 
 | ||||
| # This is used inside the Dockerfile
 | ||||
| .PHONY: pathfile | ||||
|  | @ -65,13 +69,13 @@ pathfile: | |||
| 	echo "$(PREFIX)/lib" >> /etc/ld-musl-x86_64.path | ||||
| 
 | ||||
| 
 | ||||
| # =====OPENSSL=====
 | ||||
| ## =====OPENSSL=====
 | ||||
| # Download the source code & configure the project
 | ||||
| $(OPENSSL_DIR)/Configure: | ||||
| 	curl -sSL "https://www.openssl.org/source/openssl-$(SSL_VER).tar.gz" | \
 | ||||
| 		tar -C "$(OUT_DIR)" -xz | ||||
| 		tar -xzC "$(OUT_DIR)" | ||||
| 	cd "$(OPENSSL_DIR)" && \
 | ||||
| 		CC="$$CC -idirafter /usr/include -idirafter /usr/include/x86_64-linux-gnu/" ./Configure \
 | ||||
| 		CC="$(CC) -idirafter /usr/include -idirafter /usr/include/x86_64-linux-gnu/" ./Configure \
 | ||||
| 			no-zlib \
 | ||||
| 			no-shared \
 | ||||
| 			--prefix="$(PREFIX)" \
 | ||||
|  | @ -81,16 +85,20 @@ $(OPENSSL_DIR)/Configure: | |||
| # Build OpenSSL
 | ||||
| .PHONY: openssl | ||||
| openssl: $(OPENSSL_DIR)/Configure | ||||
| 	env C_INCLUDE_PATH="$(PREFIX)/include" $(MAKE) -C "$(OPENSSL_DIR)" depend | ||||
| 	$(MAKE) -C "$(OPENSSL_DIR)" -j$(CORES) | ||||
| 	$(MAKE) -C "$(OPENSSL_DIR)" install_sw | ||||
| 	cd "$(OPENSSL_DIR)" && env C_INCLUDE_PATH="$(PREFIX)/include" $(MAKE) depend 2> /dev/null | ||||
| 	cd "$(OPENSSL_DIR)" && $(MAKE) -j$(CORES) | ||||
| 	cd "$(OPENSSL_DIR)" && $(MAKE) install_sw | ||||
| 
 | ||||
| .PHONY: clean-openssl | ||||
| clean-openssl: | ||||
| 	rm -rf "$(OPENSSL_DIR)" | ||||
| 
 | ||||
| 
 | ||||
| # =====LIBPQ=====
 | ||||
| ## =====LIBPQ=====
 | ||||
| # Download the source code & configure the project
 | ||||
| $(PQ_DIR)/configure: | ||||
| 	curl -sSL "https://ftp.postgresql.org/pub/source/v$(PQ_VER)/postgresql-$(PQ_VER).tar.gz" | \
 | ||||
| 		tar -C "$(OUT_DIR)" -xz | ||||
| 		tar -xzC "$(OUT_DIR)" | ||||
| 	cd "$(PQ_DIR)" && \
 | ||||
| 		LDFLAGS="-L$(PREFIX)/lib" CFLAGS="-I$(PREFIX)/include" ./configure \
 | ||||
| 			--without-readline \
 | ||||
|  | @ -101,10 +109,14 @@ $(PQ_DIR)/configure: | |||
| 
 | ||||
| .PHONY: libpq | ||||
| libpq: openssl $(PQ_DIR)/configure | ||||
| 	make -C "$(PQ_DIR)/src/interfaces/libpq" -j$(CORES) all-static-lib | ||||
| 	make -C "$(PQ_DIR)/src/interfaces/libpq" install install-lib-static | ||||
| 	make -C "$(PQ_DIR)/src/bin/pg_config" -j $(CORES) | ||||
| 	make -C "$(PQ_DIR)/src/bin/pg_config" install | ||||
| 	cd "$(PQ_DIR)/src/interfaces/libpq" && $(MAKE) -j$(CORES) all-static-lib | ||||
| 	cd "$(PQ_DIR)/src/interfaces/libpq" && $(MAKE) install install-lib-static | ||||
| 	cd "$(PQ_DIR)/src/bin/pg_config" && $(MAKE) -j$(CORES) | ||||
| 	cd "$(PQ_DIR)/src/bin/pg_config" && $(MAKE) install | ||||
| 
 | ||||
| .PHONY: clean-libpq | ||||
| clean-libpq: | ||||
| 	rm -rf "$(PQ_DIR)" | ||||
| 
 | ||||
| 
 | ||||
| # =====DUMB-INIT=====
 | ||||
|  | @ -113,6 +125,16 @@ $(DI_DIR)/Makefile: | |||
| 	curl -sSL "https://github.com/Yelp/dumb-init/archive/refs/tags/v$(DI_VER).tar.gz" | \
 | ||||
| 		tar -C "$(OUT_DIR)" -xz | ||||
| 
 | ||||
| .PHONY: dumb-init | ||||
| dumb-init: $(DI_DIR)/Makefile | ||||
| .PHONY: di | ||||
| di: $(DI_DIR)/Makefile | ||||
| 	make -C "$(DI_DIR)" build | ||||
| 
 | ||||
| .PHONY: clean-di | ||||
| clean-di: | ||||
| 	rm -rf "$(DI_DIR)" | ||||
| 
 | ||||
| 
 | ||||
| # ====UTILITIES FOR DEVELOPMENT=====
 | ||||
| ## The tests require a database, so we run them like this
 | ||||
| test: | ||||
| 	docker-compose -f docker-compose.test.yml -p rb_test up | ||||
|  |  | |||
							
								
								
									
										12
									
								
								Rb.yaml
								
								
								
								
							
							
						
						
									
										12
									
								
								Rb.yaml
								
								
								
								
							|  | @ -21,3 +21,15 @@ debug: | |||
|   databases: | ||||
|     postgres_rb: | ||||
|       url: "postgres://rb:rb@localhost:5432/rb" | ||||
| 
 | ||||
| release: | ||||
|   admin_user: "admin" | ||||
|   admin_pass: "password" | ||||
|   jwt: | ||||
|     key: "secret" | ||||
|     refresh_token_size: 64 | ||||
|     refresh_token_expire: 86400 | ||||
| 
 | ||||
|   databases: | ||||
|     postgres_rb: | ||||
|       url: "postgres://rb:rb@db:5432/rb" | ||||
|  |  | |||
							
								
								
									
										15
									
								
								build.sh
								
								
								
								
							
							
						
						
									
										15
									
								
								build.sh
								
								
								
								
							|  | @ -3,13 +3,12 @@ | |||
| set -e | ||||
| 
 | ||||
| # Install build dependencies | ||||
| apt update | ||||
| apt install \ | ||||
|     -y --no-install-recommends \ | ||||
|     musl-dev \ | ||||
|     musl-tools \ | ||||
|     libssl-dev \ | ||||
|     libpq-dev | ||||
| # apt update | ||||
| # apt install \ | ||||
| #     -y --no-install-recommends \ | ||||
| #     musl-dev \ | ||||
| #     musl-tools \ | ||||
| #     libssl-dev \ | ||||
| #     libpq-dev | ||||
| 
 | ||||
| make pathfile | ||||
| make | ||||
|  |  | |||
|  | @ -0,0 +1,27 @@ | |||
| version: '3' | ||||
| 
 | ||||
| services: | ||||
|   app: | ||||
|     build: | ||||
|       context: '.' | ||||
|       dockerfile: 'docker/test/Dockerfile' | ||||
| 
 | ||||
|     image: 'rb-builder:1.54' | ||||
|     command: "${CMD}" | ||||
| 
 | ||||
|     working_dir: "/usr/src/app" | ||||
| 
 | ||||
|     volumes: | ||||
|       - '$PWD:/usr/src/app' | ||||
|       - 'cache:/usr/src/app/out' | ||||
| 
 | ||||
|   db: | ||||
|     image: 'postgres:13-alpine' | ||||
| 
 | ||||
|     environment: | ||||
|       - 'POSTGRES_DB=rb' | ||||
|       - 'POSTGRES_USER=rb' | ||||
|       - 'POSTGRES_PASSWORD=rb' | ||||
| 
 | ||||
| volumes: | ||||
|   cache: | ||||
|  | @ -0,0 +1,28 @@ | |||
| # vim: ft=dockerfile | ||||
| FROM rust:1.54 | ||||
| 
 | ||||
| ENV PREFIX="/usr/src/out/prefix" \ | ||||
|     CC="musl-gcc -fPIC -pie -static" \ | ||||
|     LD_LIBRARY_PATH="$PREFIX" \ | ||||
|     PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" \ | ||||
|     PATH="/usr/local/bin:/root/.cargo/bin:$PATH" | ||||
| 
 | ||||
| WORKDIR /usr/src/app | ||||
| 
 | ||||
| RUN groupadd -g 1000 builder && \ | ||||
|     useradd -u 1000 -g 1000 builder && \ | ||||
|     mkdir -p "$PREFIX" && \ | ||||
|     chown -R builder:builder /usr/src/app && \ | ||||
|     apt update && \ | ||||
|     apt install -y --no-install-recommends \ | ||||
|         musl-dev \ | ||||
|         musl-tools \ | ||||
|         libpq-dev \ | ||||
|         libssl-dev && \ | ||||
|     rustup target add x86_64-unknown-linux-musl && \ | ||||
|     echo "$PREFIX/lib" >> /etc/ld-musl-x86_64.path | ||||
| 
 | ||||
| 
 | ||||
| USER builder | ||||
| 
 | ||||
| CMD ["cargo", "test"] | ||||
							
								
								
									
										15
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										15
									
								
								src/main.rs
								
								
								
								
							|  | @ -16,7 +16,7 @@ use rocket::{ | |||
|     fairing::AdHoc, | ||||
|     http::Status, | ||||
|     serde::json::{json, Value}, | ||||
|     Build, Request, Rocket, | ||||
|     Build, Request, Rocket, Orbit, | ||||
| }; | ||||
| use rocket_sync_db_pools::database; | ||||
| use serde::{Deserialize, Serialize}; | ||||
|  | @ -54,21 +54,20 @@ async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocke | |||
|     .await | ||||
| } | ||||
| 
 | ||||
| async fn create_admin_user(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> | ||||
| async fn create_admin_user<'a>(rocket: &'a Rocket<Orbit>) | ||||
| { | ||||
|     let admin_user = std::env::var("ADMIN_USER").unwrap_or(String::from("admin")); | ||||
|     let admin_password = std::env::var("ADMIN_PASSWORD").unwrap_or(String::from("password")); | ||||
|     let config = rocket.state::<RbConfig>().expect("RbConfig instance"); | ||||
|     let admin_user = config.admin_user.clone(); | ||||
|     let admin_pass = config.admin_pass.clone(); | ||||
| 
 | ||||
|     let conn = RbDbConn::get_one(&rocket) | ||||
|         .await | ||||
|         .expect("database connection"); | ||||
|     conn.run(move |c| { | ||||
|         admin::create_admin_user(c, &admin_user, &admin_password) | ||||
|         admin::create_admin_user(c, &admin_user, &admin_pass) | ||||
|             .expect("failed to create admin user") | ||||
|     }) | ||||
|     .await; | ||||
| 
 | ||||
|     Ok(rocket) | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, Deserialize, Serialize, Clone)] | ||||
|  | @ -100,7 +99,7 @@ fn rocket() -> _ | |||
|             "Run database migrations", | ||||
|             run_db_migrations, | ||||
|         )) | ||||
|         .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user)) | ||||
|         // .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
 | ||||
|         .attach(AdHoc::config::<RbConfig>()) | ||||
|         .register("/", catchers![default_catcher]) | ||||
|         .mount( | ||||
|  |  | |||
		Reference in New Issue