Compare commits

...

3 Commits

Author SHA1 Message Date
5848867423 bare bones haskell 2023-08-09 23:05:56 +02:00
bc45ec5fe0 build haskal 2023-08-09 23:05:03 +02:00
f1a25c6d3f schema.sql 2023-08-09 22:41:11 +02:00
7 changed files with 86 additions and 31 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
matabas-db/ matabas-db/
artifacts/

12
backend/DBConfig.hs Normal file
View File

@ -0,0 +1,12 @@
{-# LANGUAGE OverloadedStrings #-}
module DBConfig where
import qualified Database.PostgreSQL.Typed as PG
import Network.Socket (SockAddr(SockAddrUnix))
myPGDatabase :: PG.PGDatabase
myPGDatabase = PG.defaultPGDatabase
{ PG.pgDBAddr = Left ("localhost", "2137")
, PG.pgDBUser = "matabasare"
, PG.pgDBName = "matabas"
}

24
backend/Main.hs Normal file
View File

@ -0,0 +1,24 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.Exception (bracket)
import Control.Monad (void, unless)
import Data.Int (Int32)
import Data.Maybe (listToMaybe)
import qualified Database.PostgreSQL.Typed as PG
import DBConfig
PG.useTPGDatabase myPGDatabase
data Ingredient = Ingredient Int32
deriving (Eq)
createThing :: PG.PGConnection -> Ingredient -> IO ()
createThing pg (Ingredient product_id) =
void $ PG.pgExecute pg [PG.pgSQL|INSERT INTO ingredient (product_id) VALUES (${product_id})|]
main = bracket (PG.pgConnect myPGDatabase) PG.pgDisconnect $ \pg -> do
let ing = Ingredient 10
createThing pg ing

14
buildScripts/build.sh Executable file
View File

@ -0,0 +1,14 @@
set -e
mkdir -p artifacts
log "Kompilerar backend..."
find backend -type f -and -name '*.hs' -print0 \
| xargs -0 ghc -o artifacts/Main \
-odir artifacts \
-hidir artifacts \
-tmpdir artifacts
log "Klar" "!"
set +e

View File

@ -1,5 +1,9 @@
set -e set -e
export PGPORT=2137
export PGDATABASE=matabas
export PGUSER=matabasare
log() { log() {
printf "\e[1m\e[38;5;87m==>\e[0m\e[1m %s%s\e[0m\n" "$1" "${2:-.}" printf "\e[1m\e[38;5;87m==>\e[0m\e[1m %s%s\e[0m\n" "$1" "${2:-.}"
} }
@ -12,7 +16,7 @@ exitHook() {
log "Letar efter existerande brevekorresmapp" log "Letar efter existerande brevekorresmapp"
[ -d ./matabas-db ] || { [ -d ./matabas-db ] || {
log "Finns ej, skapar brevekorresmapp (./matabas-db)" log "Finns ej, skapar brevekorresmapp (./matabas-db)"
pg_ctl initdb --pgdata=./matabas-db pg_ctl initdb --pgdata=./matabas-db -o "--username=$PGUSER"
} }
[ -f matabas-db/matabas.log ] && { [ -f matabas-db/matabas.log ] && {
@ -22,39 +26,24 @@ log "Letar efter existerande brevekorresmapp"
} }
log "Startar brevekorren" log "Startar brevekorren"
pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p 2137 -k /tmp" pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p $PGPORT -k /tmp"
trap exitHook EXIT trap exitHook EXIT
log "Skapar matabas" log "Skapar matabas"
createdb -p 2137 matabas 2>>./matabas-db/matabas.log || log "Matabasen finns, skriver ej över" createdb -p "$PGPORT" matabas 2>>./matabas-db/matabas.log || log "Matabasen finns, skriver ej över"
log "Försvensknar miljön" log "Ställer in schema.sql"
_cargo() { psql -p "$PGPORT" "$PBDATABASE" < schema.sql
cuh="$1"
shift P="$PWD"
case "$cuh" in clean() {
kör|spring) log "Rensar byggnad"
cargo run "$@" rm -rf artifacts
;; }
ny)
cargo new "$@" build() {
;; source "$P/buildScripts/build.sh"
pröva)
cargo test "$@"
;;
bygg)
cargo build "$@"
;;
enna)
cargo init "$@"
;;
*)
echo "vafan gö du"
;;
esac
} }
alias 'last=_cargo'
alias cargo=exit
log "Redo" "!" log "Redo" "!"

View File

@ -14,8 +14,12 @@
default = pkgs.hello; default = pkgs.hello;
}; };
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
packages = [ packages.default pkgs.postgresql_15_jit pkgs.cargo ]; packages = [
shellHook = builtins.readFile ./build/devShellHook.sh ; packages.default
pkgs.postgresql_15_jit
( pkgs.ghc.withPackages (ps: with ps; [ scotty postgresql-typed ]) )
];
shellHook = builtins.readFile ./buildScripts/devShellHook.sh ;
}; };
} }
) // { ) // {

11
schema.sql Normal file
View File

@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS ingredient
( product_id INTEGER NOT NULL PRIMARY KEY -- https://handlaprivatkund.ica.se/stores/1004247/api/v4/products/bop?retailerProductId=...
);
CREATE TABLE IF NOT EXISTS ingredient_data
( product_id INTEGER REFERENCES ingredient(product_id)
, name TEXT NOT NULL
, cost_per_kg REAL NOT NULL
, cost_per_unit REAL NOT NULL
, image BYTEA NOT NULL
);