Compare commits
3 Commits
27b8036ff6
...
5848867423
Author | SHA1 | Date | |
---|---|---|---|
5848867423 | |||
bc45ec5fe0 | |||
f1a25c6d3f |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
matabas-db/
|
||||
artifacts/
|
12
backend/DBConfig.hs
Normal file
12
backend/DBConfig.hs
Normal 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
24
backend/Main.hs
Normal 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
14
buildScripts/build.sh
Executable 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
|
|
@ -1,5 +1,9 @@
|
|||
set -e
|
||||
|
||||
export PGPORT=2137
|
||||
export PGDATABASE=matabas
|
||||
export PGUSER=matabasare
|
||||
|
||||
log() {
|
||||
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"
|
||||
[ -d ./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 ] && {
|
||||
|
@ -22,39 +26,24 @@ log "Letar efter existerande brevekorresmapp"
|
|||
}
|
||||
|
||||
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
|
||||
|
||||
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"
|
||||
_cargo() {
|
||||
cuh="$1"
|
||||
shift
|
||||
case "$cuh" in
|
||||
kör|spring)
|
||||
cargo run "$@"
|
||||
;;
|
||||
ny)
|
||||
cargo new "$@"
|
||||
;;
|
||||
pröva)
|
||||
cargo test "$@"
|
||||
;;
|
||||
bygg)
|
||||
cargo build "$@"
|
||||
;;
|
||||
enna)
|
||||
cargo init "$@"
|
||||
;;
|
||||
*)
|
||||
echo "vafan gö du"
|
||||
;;
|
||||
esac
|
||||
log "Ställer in schema.sql"
|
||||
psql -p "$PGPORT" "$PBDATABASE" < schema.sql
|
||||
|
||||
P="$PWD"
|
||||
clean() {
|
||||
log "Rensar byggnad"
|
||||
rm -rf artifacts
|
||||
}
|
||||
|
||||
build() {
|
||||
source "$P/buildScripts/build.sh"
|
||||
}
|
||||
alias 'last=_cargo'
|
||||
alias cargo=exit
|
||||
|
||||
log "Redo" "!"
|
||||
|
|
@ -14,8 +14,12 @@
|
|||
default = pkgs.hello;
|
||||
};
|
||||
devShells.default = pkgs.mkShell {
|
||||
packages = [ packages.default pkgs.postgresql_15_jit pkgs.cargo ];
|
||||
shellHook = builtins.readFile ./build/devShellHook.sh ;
|
||||
packages = [
|
||||
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
11
schema.sql
Normal 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
|
||||
);
|
Loading…
Reference in New Issue
Block a user