diff --git a/flake.nix b/flake.nix index aaabde3..8814592 100644 --- a/flake.nix +++ b/flake.nix @@ -3,56 +3,43 @@ # TODO Hacking around the Qt problems # TODO PyPI package not in nixpkgs - - { description = "Python development environment"; - 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"; flake-utils .url = "github:numtide/flake-utils"; flake-compat = { url = "github:edolstra/flake-compat"; flake = false; }; - }; outputs = { self, nixpkgs, flake-utils, ... }: - # Option 1: try to support each default system flake-utils.lib.eachDefaultSystem # NB Some packages in nixpkgs are not supported on some systems - # Option 2: try to support selected systems # flake-utils.lib.eachSystem ["x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin"] (system: - let pkgs = import nixpkgs { inherit system; # Any overlays you need can go here overlays = [ ]; }; - # ----- A Python interpreter with the packages that interest us ------- python-with-all-my-packages = (python: - (python.withPackages (ps: [ - ps.pytest - ps.fastapi + (python.withPackages (ps: with ps; [ + pytest + fastapi + uvicorn + sqlalchemy + psycopg2 ]))); - in rec { #devShell = self.devShells.${ system }.python310; # does not need `rec` devShell = devShells.python310; - devShells = builtins.listToAttrs ( builtins.map @@ -62,24 +49,23 @@ value = pkgs.mkShell { buildInputs = [ (python-with-all-my-packages pkgs.${ pythonVersion }) - pkgs.just - pkgs.cowsay ]; packages = [ pkgs.python3Packages.python-lsp-server - pkgs.lolcat - pkgs.exa + pkgs.postgresql ]; shellHook = '' - export PS1="${pythonVersion} devshell> " - alias foo='cowsay Foo' - alias bar='exa -l | lolcat' - alias baz='cowsay What is the difference between buildIntputs and packages? | lolcat' + export PGDATA=$PWD/postgres_data + export PGHOST=$PWD/postgres + export LOG_PATH=$PWD/postgres/LOG + export PGDATABASE=dustforce + export DATABASE_URL="postgresql:///postgres?host=$PGHOST" + ./start.sh ''; }; } - ) [ "python37" "python38" "python39" "python310" ] + ) [ "python310" ] ); } ); diff --git a/main.py b/main.py new file mode 100644 index 0000000..23a129d --- /dev/null +++ b/main.py @@ -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 diff --git a/setup-postgres.sh b/setup-postgres.sh new file mode 100755 index 0000000..1c7278e --- /dev/null +++ b/setup-postgres.sh @@ -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 +); diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..9c13419 --- /dev/null +++ b/start.sh @@ -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 &