diff --git a/README.md b/README.md index 5999bac..061ab19 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,20 @@ The blog has the following functionality - Editing Posts - Deleting Posts -Additionally, it tracks the category that posts are in, allowing filtering posts by category. \ No newline at end of file +Additionally, it tracks the category that posts are in, allowing filtering posts by category. + +Xnoeblog is intended to be used with Docker, an example compose file is provided within this repository. The backend container can be configured using the following environment variables + +- DB_HOST: Hostname of the database +- DB_USER: Database user +- DB_PASS: Database user password +- DB_NAME: Name of database to use +- XNOEBLOG_USER: Default user +- XNOEBLOG_PASS: Default password + +All of these values are optional. Xnoeblog backend defaults to creating no user and using the following values for database if no environment variables are provided + +- DB_HOST=db +- DB_USER=root +- DB_PASS=password +- DB_NAME=xnoeblog \ No newline at end of file diff --git a/backend/app/Main.hs b/backend/app/Main.hs index 56b9029..e1766ab 100644 --- a/backend/app/Main.hs +++ b/backend/app/Main.hs @@ -12,6 +12,7 @@ import Data.ByteString.Lazy.Char8 as L (pack) import Data.ByteString as B (unpack, ByteString) import Text.Printf import System.Random (newStdGen, randomRs) +import System.ReadEnvVar (lookupEnv) import Crypto.Hash.SHA256 as SHA256 @@ -317,7 +318,34 @@ handleWhoami c = do main :: IO () main = do - c <- connectPostgreSQL "host=database user=blog password=root dbname=blog" + dbhostvar <- lookupEnv "DB_HOST" + dbuservar <- lookupEnv "DB_USER" + dbpassvar <- lookupEnv "DB_PASS" + dbnamevar <- lookupEnv "DB_NAME" + uservar <- lookupEnv "XNOEBLOG_USER" + passvar <- lookupEnv "XNOEBLOG_PASS" + + let dbhost = case dbhostvar of + Just h -> h + Nothing -> "db" + let dbuser = case dbuservar of + Just u -> u + Nothing -> "root" + let dbpass = case dbpassvar of + Just p -> p + Nothing -> "password" + let dbname = case dbnamevar of + Just d -> d + Nothing -> "xnoeblog" + + let user = case uservar of + Just u -> u + Nothing -> "" + let pass = case passvar of + Just p -> p + Nothing -> "" + + c <- connectPostgreSQL ("host=" ++ dbhost ++ " user=" ++ dbuser ++ " password=" ++ dbpass ++ " dbname=" ++ dbname) createPostsTable <- prepare c "CREATE TABLE IF NOT EXISTS posts(postid serial primary key, catid int, title varchar(128), subtext varchar(128), content text);" createCategoriesTable <- prepare c "CREATE TABLE IF NOT EXISTS categories(catid serial primary key, name varchar(128));" @@ -326,7 +354,23 @@ main = do execute createCategoriesTable [] execute createUsersTable [] commit c - + + queryUser <- prepare c "SELECT * from users WHERE username like ?;" + execute queryUser [toSql user] + userExists <- fetchAllRows queryUser + if user /= "" then + case userExists of + [_] -> do return Nothing + [] -> do + let passwordHashBS = SHA256.hash (C.pack pass) + let passwordHash = hexOfBS passwordHashBS + createUser <- prepare c "INSERT INTO users(username, password) VALUES(?, ?);" + execute createUser [toSql user, toSql passwordHash] + commit c + return Nothing + else + do return Nothing + simpleHTTP config $ do decodeBody (defaultBodyPolicy "/tmp/" 0 4096 4096) msum [ dir "v1" $ dir "post" $ path $ \pid -> require (getPostContent c pid) $ \post -> ok $ toResponse post diff --git a/backend/backend.cabal b/backend/backend.cabal index e8cdc8a..a986278 100644 --- a/backend/backend.cabal +++ b/backend/backend.cabal @@ -29,6 +29,6 @@ executable backend -- LANGUAGE extensions used by modules in this package. -- other-extensions: - build-depends: base ^>=4.14.3.0, happstack-server ^>=7.7.2, HDBC ^>= 2.4.0.4, HDBC-postgresql ^>= 2.3.2.4, json ^>= 0.10, bytestring ^>= 0.10.12.0, cryptohash-sha256 ^>= 0.11.102.1, random ^>= 1.1, mtl ^>= 2.2.2 + build-depends: base ^>=4.14.3.0, happstack-server ^>=7.7.2, HDBC ^>= 2.4.0.4, HDBC-postgresql ^>= 2.3.2.4, json ^>= 0.10, bytestring ^>= 0.10.12.0, cryptohash-sha256 ^>= 0.11.102.1, random ^>= 1.1, mtl ^>= 2.2.2, read-env-var ^>= 1.0.0.0 hs-source-dirs: app default-language: Haskell2010 diff --git a/docker-compose.yml b/docker-compose.yml index d40387a..e0b4d73 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,22 +2,29 @@ version: '3.0' services: frontend: + restart: unless-stopped build: ./frontend + image: registry.xnopyt.com/xnoeblog-frontend:latest backend: + restart: unless-stopped build: ./backend + image: registry.xnopyt.com/xnoeblog-backend:latest depends_on: - database nginx: + restart: unless-stopped image: nginx:alpine ports: - 80:80 volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf - database: + db: + restart: unless-stopped image: postgres environment: - - POSTGRES_PASSWORD=root - - POSTGRES_USER=blog \ No newline at end of file + - POSTGRES_PASSWORD=password + - POSTGRES_USER=root + - POSTGRES_DB=xnoeblog \ No newline at end of file