we're getting close to MVP

This commit is contained in:
Anish Lakhwara 2022-11-21 01:37:22 +10:00
parent 585caaf327
commit a083ca364b
4 changed files with 97 additions and 28 deletions

View file

@ -3,56 +3,43 @@
# TODO Hacking around the Qt problems # TODO Hacking around the Qt problems
# TODO PyPI package not in nixpkgs # TODO PyPI package not in nixpkgs
{ {
description = "Python development environment"; description = "Python development environment";
inputs = { inputs = {
# Version pinning is managed in flake.lock. Upgrading can be done with
# something like
#
# nix flake lock --update-input nixpkgs
nixpkgs .url = "github:nixos/nixpkgs/nixos-22.05"; nixpkgs .url = "github:nixos/nixpkgs/nixos-22.05";
flake-utils .url = "github:numtide/flake-utils"; flake-utils .url = "github:numtide/flake-utils";
flake-compat = { flake-compat = {
url = "github:edolstra/flake-compat"; url = "github:edolstra/flake-compat";
flake = false; flake = false;
}; };
}; };
outputs = { self, nixpkgs, flake-utils, ... }: outputs = { self, nixpkgs, flake-utils, ... }:
# Option 1: try to support each default system # Option 1: try to support each default system
flake-utils.lib.eachDefaultSystem # NB Some packages in nixpkgs are not supported on some systems flake-utils.lib.eachDefaultSystem # NB Some packages in nixpkgs are not supported on some systems
# Option 2: try to support selected systems # Option 2: try to support selected systems
# flake-utils.lib.eachSystem ["x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin"] # flake-utils.lib.eachSystem ["x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin"]
(system: (system:
let let
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
# Any overlays you need can go here # Any overlays you need can go here
overlays = [ ]; overlays = [ ];
}; };
# ----- A Python interpreter with the packages that interest us ------- # ----- A Python interpreter with the packages that interest us -------
python-with-all-my-packages = (python: python-with-all-my-packages = (python:
(python.withPackages (ps: [ (python.withPackages (ps: with ps; [
ps.pytest pytest
ps.fastapi fastapi
uvicorn
sqlalchemy
psycopg2
]))); ])));
in in
rec { rec {
#devShell = self.devShells.${ system }.python310; # does not need `rec` #devShell = self.devShells.${ system }.python310; # does not need `rec`
devShell = devShells.python310; devShell = devShells.python310;
devShells = devShells =
builtins.listToAttrs ( builtins.listToAttrs (
builtins.map builtins.map
@ -62,24 +49,23 @@
value = pkgs.mkShell { value = pkgs.mkShell {
buildInputs = [ buildInputs = [
(python-with-all-my-packages pkgs.${ pythonVersion }) (python-with-all-my-packages pkgs.${ pythonVersion })
pkgs.just
pkgs.cowsay
]; ];
packages = [ packages = [
pkgs.python3Packages.python-lsp-server pkgs.python3Packages.python-lsp-server
pkgs.lolcat pkgs.postgresql
pkgs.exa
]; ];
shellHook = shellHook =
'' ''
export PS1="${pythonVersion} devshell> " export PGDATA=$PWD/postgres_data
alias foo='cowsay Foo' export PGHOST=$PWD/postgres
alias bar='exa -l | lolcat' export LOG_PATH=$PWD/postgres/LOG
alias baz='cowsay What is the difference between buildIntputs and packages? | lolcat' export PGDATABASE=dustforce
export DATABASE_URL="postgresql:///postgres?host=$PGHOST"
./start.sh
''; '';
}; };
} }
) [ "python37" "python38" "python39" "python310" ] ) [ "python310" ]
); );
} }
); );

50
main.py Normal file
View file

@ -0,0 +1,50 @@
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
from sqlalchemy import create_engine
from dataclasses import dataclass
app = FastAPI()
engine = create_engine('postgresql://postgres?host=/home/anish/usr/df-web/postgres/')
conn = engine.connect()
class Vector3(BaseModel):
x: float
y: float
z: float
class Route(BaseModel):
id: str
creatorName: str
# dustPoints: List[Vector3]
checkpoints: List[Vector3]
startLocation: Vector3
# endLocation: Vector3
# creationDate: str
@app.get("/route")
async def get_route():
select = "SELECT * FROM routes"
result = engine.execute(select)
for route in result:
print(route)
return {'message': "Woodley's world"}
@app.post("/route")
async def post_route(route: Route):
as_dict = route.dict()
columns = ", ".join(list(as_dict.keys()))
# TODO this is fucked lol
# was trying to turn it into a string i could use to insert
# but that's not going so great because we have nested arrays
# but it basically means I need to add "ARRAY" before array values
# or use the ORM as god intended...
# Ugh, oop strikes again lol
rows = str(list(as_dict.values()))[1:-1]
insert = f"INSERT INTO routes ({columns}) VALUES ({rows}))"
engine.execute(insert)
return

23
setup-postgres.sh Executable file
View file

@ -0,0 +1,23 @@
#! /usr/bin/env bash
# Run on the first time configuring database
if [ ! -d $PGHOST ]; then
mkdir -p $PGHOST
fi
if [ ! -d $PGDATA ]; then
echo 'Initializing postgresql database...'
initdb $PGDATA --auth=trust >/dev/null
fi
pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
# TODO move to alembic migrations
psql $PGDATABASE << EOF
CREATE TABLE routes ( \
creatorName text,
dustPoints float[],
checkPoints float[],
startLocation float,
endLocation float,
createDate text
);

10
start.sh Executable file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
if [ ! -f "$PGHOST/.s.PGSQL.*" ]; then
pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
fi
if [ $? != 0 ]; then
echo 'You need to run ./setup-postgres.sh'
fi
# uvicorn main:app --reload --host 0.0.0.0 &