Initial commit

This commit is contained in:
Xnoe 2022-05-18 18:14:36 +01:00
commit a32d668ebd
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
12 changed files with 334 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
elm-stuff/

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2022 Xnoe
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# The Xnoe Blog
This is a highly work-in-progress blog frontend and backend written in Haskell and Elm-lang, using PostgreSQL for the database, it is being developed with a Docker-first mindset to make development a breeze.
Currently the only functionality that exists is providing a really bad card view of the posts that exist.
You will need to create a table called posts manually if you want to use this, the following is necessary
```sql
CREATE TABLE posts(title varchar(128), subtext varchar(128), category varchar(128));
```
And you will need to add any entries manually.
Exposed on port 80

5
backend/CHANGELOG.md Normal file
View File

@ -0,0 +1,5 @@
# Revision history for backend
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

10
backend/Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM haskell:8
WORKDIR /opt/backend
RUN apt-get update
RUN yes | apt-get install postgresql libpq-dev
RUN cabal update
COPY ./backend.cabal /opt/backend
RUN cabal build --only-dependencies -j4
COPY . /opt/backend
RUN cabal install
CMD ["backend"]

31
backend/app/Main.hs Normal file
View File

@ -0,0 +1,31 @@
module Main where
import Happstack.Server
import Control.Monad
import Control.Exception
import Database.HDBC
import Database.HDBC.PostgreSQL (connectPostgreSQL)
import Text.JSON
config = Conf {
port = 80,
validator = Nothing,
logAccess = Just logMAccess,
timeout = 30,
threadGroup = Nothing
}
getPostListing c = do
select <- prepare c "SELECT * FROM posts;"
execute select []
result <- fetchAllRows select
let posts = JSArray $ (map (\(t:s:c:[]) -> let (title, subtext, category) = (fromSql t, fromSql s, fromSql c) in showJSON $ toJSObject [("title", toJSString title), ("subtext", toJSString subtext), ("category", toJSString category)]) result) in
return $ Just $ encode posts
main :: IO ()
main = do
c <- connectPostgreSQL "host=database user=blog password=root dbname=blog"
simpleHTTP config $
msum [ dir "v1" $ path $ \s -> case s of {"posts" -> require (getPostListing c) $ \posts -> ok posts ; _ -> notFound "Endpoint does not exist"}
, notFound "Endpoint does not exist"
]

34
backend/backend.cabal Normal file
View File

@ -0,0 +1,34 @@
cabal-version: 2.4
name: backend
version: 0.1.0.0
-- A short (one-line) description of the package.
-- synopsis:
-- A longer description of the package.
-- description:
-- A URL where users can report bugs.
-- bug-reports:
-- The license under which the package is released.
-- license:
author: Xnoe
maintainer: xnoe@xnoe.moe
-- A copyright notice.
-- copyright:
-- category:
extra-source-files: CHANGELOG.md
executable backend
main-is: Main.hs
-- Modules included in this executable, other than Main.
-- other-modules:
-- 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
hs-source-dirs: app
default-language: Haskell2010

22
docker-compose.yml Normal file
View File

@ -0,0 +1,22 @@
version: '3.0'
services:
frontend:
build: ./frontend
backend:
build: ./backend
nginx:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
database:
image: postgres
restart: unless-stopped
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=blog

14
frontend/Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM alpine as alpine
RUN apk --no-cache add curl
RUN curl -L -o elm.gz https://github.com/elm/compiler/releases/download/0.19.1/binary-for-linux-64-bit.gz
RUN gunzip elm.gz
RUN chmod +x elm
RUN mv elm /usr/local/bin
WORKDIR /opt/frontend
COPY . /opt/frontend
RUN elm make src/Main.elm
FROM nginx:alpine
COPY --from=alpine /opt/frontend/index.html /usr/share/nginx/html/index.html

27
frontend/elm.json Normal file
View File

@ -0,0 +1,27 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/url": "1.0.0"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}

151
frontend/src/Main.elm Normal file
View File

@ -0,0 +1,151 @@
module Main exposing (..)
import Http
import Json.Decode exposing (Decoder, field, string, map3)
import Browser
import Browser.Navigation exposing (..)
import Url
import Html exposing (..)
import Html.Attributes exposing (..)
main =
Browser.application {
init = init,
subscriptions = (\_ -> Sub.none),
update = update,
view = view,
onUrlRequest = LinkClinked,
onUrlChange = UrlChanged
}
type alias GridItem = {
category: String,
title: String,
subtext: String
}
type alias Model = {
header: Maybe (Html Msg),
sidebar: Maybe (Html Msg),
pinnedPosts: List GridItem,
posts: List GridItem,
url: Url.Url,
key: Browser.Navigation.Key,
errMessage: Maybe (String)
}
init : () -> Url.Url -> Browser.Navigation.Key -> (Model, Cmd Msg)
init _ url key =
({
header = Nothing, sidebar = Nothing, pinnedPosts = [], posts = [],
url = url, key = key, errMessage = Nothing
},
Http.get {url = "v1/posts", expect = Http.expectJson GotPosts processPosts}
)
type Msg
= LinkClinked Browser.UrlRequest
| UrlChanged Url.Url
| GotPosts (Result Http.Error (List GridItem))
processPosts : Decoder (List GridItem)
processPosts =
Json.Decode.list (
map3 GridItem (field "category" string) (field "title" string) (field "subtext" string)
)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
LinkClinked req ->
case req of
Browser.Internal url -> (model, Browser.Navigation.pushUrl model.key (Url.toString url))
Browser.External url -> (model, Browser.Navigation.load url)
UrlChanged req ->
(
{ model | url = req }
, Cmd.none
)
GotPosts (Ok l) -> ({model | posts = l}, Cmd.none)
GotPosts (Err _) -> ({model | errMessage = Just "Failed to load posts!"}, Cmd.none)
type alias Document msg = {
title: String,
body: List (Html msg)
}
renderGridItem : GridItem -> Html Msg
renderGridItem griditem =
div [
style "width" "25%",
style "height" "auto",
style "box-sizing" "border-box",
style "padding" "15px"
] [
div [
style "flex-direction" "column",
style "border-radius" "5px",
style "display" "flex",
style "height" "100%"
] [
header [
style "background-color" "#404040",
style "color" "white",
style "height" "75px",
style "display" "flex",
style "flex-direction" "column",
style "align-items" "center",
style "box-shadow" "none",
style "box-sizing" "border-box",
style "font-size" "20px",
style "font-weight" "bold",
style "border-radius" "10px 10px 0 0"
] [
text (griditem.title)
],
div [
style "color" "white",
style "flex-grow" "1",
style "background-color" "#505050",
style "padding" "10px"
] [
text (griditem.subtext)
],
footer [
style "background-color" "#404040",
style "color" "white",
style "height" "50px",
style "display" "flex",
style "flex-direction" "row",
style "justify-content" "space-around",
style "box-shadow" "none",
style "box-sizing" "border-box",
style "padding" "10px",
style "border-radius" "0 0 10px 10px"
] [
text (griditem.category)
]
]
]
renderModel : Model -> Html Msg
renderModel model =
div [
style "display" "flex",
style "flex-flow" "row wrap"
]
(case (model.errMessage) of
Nothing -> (List.map renderGridItem model.posts)
Just e -> [h1 [] [text (e)]]
)
view : Model -> Document Msg
view model =
{title = "Xnopyt.com", body = [htmlView model]}
htmlView : Model -> Html Msg
htmlView model =
renderModel model

17
nginx/nginx.conf Normal file
View File

@ -0,0 +1,17 @@
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name _;
location /v1 {
proxy_pass http://backend:80;
}
location / {
proxy_pass http://frontend:80;
}
}
}