Skip to content

Commit

Permalink
finish endpoint tag/videos
Browse files Browse the repository at this point in the history
  • Loading branch information
WeissP committed Apr 16, 2024
1 parent 718f45a commit 6f58a3d
Show file tree
Hide file tree
Showing 58 changed files with 3,803 additions and 90 deletions.
4 changes: 2 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nix_direnv_manual_reload
watch_file *.cabal
use flake
watch_file *.cabal, env_config/*
use flake .#backend --impure
23 changes: 23 additions & 0 deletions api/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Main where

import Api
import Api.Tag (mockTag)
import MyPrelude
import Network.Wai
import Network.Wai.Handler.Warp
import Servant
import Types

main :: IO ()
main = do
logOptions <- logOptionsHandle stderr True
portStr <- getEnv "SERVER_PORT"
let port =
fromMaybe
(error $ "Could not parse port: " <> portStr)
(readMaybe portStr)
withLogFunc logOptions $ \lf -> do
env <- appEnv lf
runRIO env initEnv
runRIO env mockTag
run port (serve appAPI $ hoistServer appAPI (handleApiM env) appServer)
3 changes: 2 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AppServer (runServer)
import MyPrelude

main :: IO ()
main = runSimpleApp $ logInfo "Hello World!"
main = runSimpleApp $ liftIO runServer
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
packages:
./

package subtube
tests: True
29 changes: 28 additions & 1 deletion devshell.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
[[env]]
name = "API_JS_PATH"
eval = "$PRJ_ROOT/frontend/api.js"

[[env]]
name = "HOOGLE_PORT"
value = "9800"
value = "9500"

[[env]]
name = "API_SPEC_PATH"
eval = "${PRJ_ROOT}/api-spec.json"

[[commands]]
package = "sqitchPg"
name = "sqitch"
category = "DB"

[[commands]]
package = "openapi-generator-cli"
category = "openapi"

[[commands]]
name = "gen-ts"
help = "generate typescript client"
command = '''
rm -rf $PRJ_ROOT/frontend/genstub
openapi-generator-cli generate -i "$API_SPEC_PATH" -g typescript-axios -o "$PRJ_ROOT/frontend/genstub"
'''
category = "openapi"

[[commands]]
name = "doc"
Expand All @@ -9,3 +35,4 @@ echo http://127.0.0.1:$HOOGLE_PORT
hoogle serve -p $HOOGLE_PORT --local
'''
help = "run hoogle server locally for documentation"
category = "Haskell tools"
12 changes: 12 additions & 0 deletions env_config/backend.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[env]]
name = "HOOGLE_PORT"
value = "9500"

[[commands]]
name = "doc"
command = '''
echo http://127.0.0.1:$HOOGLE_PORT
hoogle serve -p $HOOGLE_PORT --local
'''
help = "run hoogle server locally for documentation"
category = "Haskell tools"
79 changes: 79 additions & 0 deletions env_config/common.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
imports = ["postgres.nix"]

[services.postgres]
package = "pkgs.postgresql_15"
setupPostgresOnStartup = true
listenAddresses = "*"

[[env]]
name = "SERVER_PORT"
value = "9892"

[[env]]
name = "INVIDIOUS_ROOT"
value = "https://invidious.jing.rocks/api/"

[[env]]
name = "INVIDIOUS_ROOT_FALLBACK"
value = "https://yewtu.be/api/"

[[env]]
name = "DBNAME"
value = "subtube"

[[env]]
name = "PGPORT"
value = "9891"

[[env]]
name = "ST_PGHOST"
value = "localhost"

[[env]]
name = "PGUSER"
eval = "${USER:-$(id -nu)}"

[[env]]
name = "PSQL_URL"
eval = "postgresql://${ST_PGHOST}:${PGPORT}/${DBNAME}"

[[env]]
name = "PSQL_MOCK_URL"
eval = "postgresql://${ST_PGHOST}:${PGPORT}/${DBNAME}_mock"

[[env]]
name = "ST_DATABASE_MIGRATION_DIR"
value = "psql"

[[env]]
name = "ST_DATABASE_MIGRATION_MOCK_DIR"
eval = 'mockdata/db'

[[commands]]
name = "setup-db"
command = '''
createdb -E UTF8 -T template0 -h $PGDATA -p $PGPORT $DBNAME
createdb -E UTF8 -T template0 -h $PGDATA -p $PGPORT ${DBNAME}_mock
pushd $PRJ_ROOT/migration/psql
sqitch deploy dev
sqitch deploy mock
'''
category = "DB"

[[commands]]
name = "reset-db"
command = '''
rm -rf $PGDATA
setup-postgres
postgres
'''
category = "DB"

[[commands]]
name = "rebase-mock-db"
command = '''
pushd $PRJ_ROOT/migration/psql
yes | sqitch rebase mock
'''
category = "DB"

21 changes: 21 additions & 0 deletions env_config/frontend.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[[env]]
name = "API_SPEC_PATH"
eval = "$PRJ_ROOT/api.js"

[[env]]
name = "API_GENSTUB_PATH"
eval = "$PRJ_ROOT/frontend/src/genstub"

[[commands]]
package = "openapi-generator-cli"
category = "openapi"

[[commands]]
name = "gen-ts"
help = "generate typescript client"
command = '''
rm -rf "$API_GENSTUB_PATH"
openapi-generator-cli generate -i "http://127.0.0.1:$SERVER_PORT/swagger.json" -g typescript-axios -o "$API_GENSTUB_PATH"
'''
category = "openapi"

102 changes: 102 additions & 0 deletions env_config/postgres.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# This module automatically configures postgres when the user enters the
# devshell.
#
# To start the server, invoke `postgres` in one devshell. Then start a second
# devshell to run the clients.
{ lib, pkgs, config, ... }:
with lib;
let
devshellSrc = fetchTarball
"https://github.com/numtide/devshell/archive/cd4e2fda3150dd2f689caeac07b7f47df5197c31.tar.gz";

# Because we want to be able to push pure JSON-like data into the
# environment.
strOrPackage =
import (devshellSrc + "/nix/strOrPackage.nix") { inherit lib pkgs; };

cfg = config.services.postgres;
createDB = optionalString cfg.createUserDB ''
echo "CREATE DATABASE ''${USER:-$(id -nu)};" | postgres --single -E postgres
'';

setup-postgres = pkgs.writeShellScriptBin "setup-postgres" ''
set -euo pipefail
export PATH=${cfg.package}/bin:${pkgs.coreutils}/bin
# Abort if the data dir already exists
[[ ! -d "$PGDATA" ]] || exit 0
initdb ${concatStringsSep " " cfg.initdbArgs}
cat >> "$PGDATA/postgresql.conf" <<EOF
listen_addresses = '${cfg.listenAddresses}'
unix_socket_directories = '$PGHOST'
EOF
${createDB}
'';

start-postgres = pkgs.writeShellScriptBin "start-postgres" ''
set -euo pipefail
${setup-postgres}/bin/setup-postgres
exec ${cfg.package}/bin/postgres
'';
in {
options.services.postgres = {
package = mkOption {
type = strOrPackage;
description = "Which version of postgres to use";
default = pkgs.postgresql;
defaultText = "pkgs.postgresql";
};

setupPostgresOnStartup = mkEnableOption "call setup-postgres on startup";

createUserDB = mkOption {
type = types.bool;
default = true;
description = ''
Create a database named like current user on startup.
This option only makes sense when `setupPostgresOnStartup` is true.
'';
};

listenAddresses = mkOption {
type = types.str;
default = "";
description = ''
Specifies the TCP/IP address(es) on which the server is to listen for connections from client applications
'';
};

initdbArgs = mkOption {
type = with types; listOf str;
default = [ "--no-locale" ];
example = [ "--data-checksums" "--allow-group-access" ];
description = ''
Additional arguments passed to <literal>initdb</literal> during data dir
initialisation.
'';
};

};
config = {
packages = [ cfg.package setup-postgres start-postgres ];

env = [
{
name = "PGDATA";
eval = "$PRJ_DATA_DIR/postgres";
}
{
name = "PGHOST";
eval = "$PGDATA";
}
];

devshell.startup.setup-postgres.text =
lib.optionalString cfg.setupPostgresOnStartup ''
${setup-postgres}/bin/setup-postgres
'';
};
}
Loading

0 comments on commit 6f58a3d

Please sign in to comment.