Add environment variable support to backend
This commit is contained in:
parent
491b9c268b
commit
e3c27c6609
16
README.md
16
README.md
@ -9,3 +9,19 @@ The blog has the following functionality
|
|||||||
- Deleting Posts
|
- Deleting Posts
|
||||||
|
|
||||||
Additionally, it tracks the category that posts are in, allowing filtering posts by category.
|
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
|
@ -12,6 +12,7 @@ import Data.ByteString.Lazy.Char8 as L (pack)
|
|||||||
import Data.ByteString as B (unpack, ByteString)
|
import Data.ByteString as B (unpack, ByteString)
|
||||||
import Text.Printf
|
import Text.Printf
|
||||||
import System.Random (newStdGen, randomRs)
|
import System.Random (newStdGen, randomRs)
|
||||||
|
import System.ReadEnvVar (lookupEnv)
|
||||||
|
|
||||||
import Crypto.Hash.SHA256 as SHA256
|
import Crypto.Hash.SHA256 as SHA256
|
||||||
|
|
||||||
@ -317,7 +318,34 @@ handleWhoami c = do
|
|||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
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);"
|
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));"
|
createCategoriesTable <- prepare c "CREATE TABLE IF NOT EXISTS categories(catid serial primary key, name varchar(128));"
|
||||||
@ -327,6 +355,22 @@ main = do
|
|||||||
execute createUsersTable []
|
execute createUsersTable []
|
||||||
commit c
|
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
|
simpleHTTP config $ do
|
||||||
decodeBody (defaultBodyPolicy "/tmp/" 0 4096 4096)
|
decodeBody (defaultBodyPolicy "/tmp/" 0 4096 4096)
|
||||||
msum [ dir "v1" $ dir "post" $ path $ \pid -> require (getPostContent c pid) $ \post -> ok $ toResponse post
|
msum [ dir "v1" $ dir "post" $ path $ \pid -> require (getPostContent c pid) $ \post -> ok $ toResponse post
|
||||||
|
@ -29,6 +29,6 @@ executable backend
|
|||||||
|
|
||||||
-- LANGUAGE extensions used by modules in this package.
|
-- LANGUAGE extensions used by modules in this package.
|
||||||
-- other-extensions:
|
-- 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
|
hs-source-dirs: app
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
@ -2,22 +2,29 @@ version: '3.0'
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
frontend:
|
frontend:
|
||||||
|
restart: unless-stopped
|
||||||
build: ./frontend
|
build: ./frontend
|
||||||
|
image: registry.xnopyt.com/xnoeblog-frontend:latest
|
||||||
|
|
||||||
backend:
|
backend:
|
||||||
|
restart: unless-stopped
|
||||||
build: ./backend
|
build: ./backend
|
||||||
|
image: registry.xnopyt.com/xnoeblog-backend:latest
|
||||||
depends_on:
|
depends_on:
|
||||||
- database
|
- database
|
||||||
|
|
||||||
nginx:
|
nginx:
|
||||||
|
restart: unless-stopped
|
||||||
image: nginx:alpine
|
image: nginx:alpine
|
||||||
ports:
|
ports:
|
||||||
- 80:80
|
- 80:80
|
||||||
volumes:
|
volumes:
|
||||||
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
|
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
|
||||||
|
|
||||||
database:
|
db:
|
||||||
|
restart: unless-stopped
|
||||||
image: postgres
|
image: postgres
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_PASSWORD=root
|
- POSTGRES_PASSWORD=password
|
||||||
- POSTGRES_USER=blog
|
- POSTGRES_USER=root
|
||||||
|
- POSTGRES_DB=xnoeblog
|
Loading…
x
Reference in New Issue
Block a user