From a32d668ebdf2e5f8988553b8a6abe9b80bdeeb5d Mon Sep 17 00:00:00 2001 From: Xnoe Date: Wed, 18 May 2022 18:14:36 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + LICENSE | 7 ++ README.md | 15 +++++ backend/CHANGELOG.md | 5 ++ backend/Dockerfile | 10 +++ backend/app/Main.hs | 31 +++++++++ backend/backend.cabal | 34 ++++++++++ docker-compose.yml | 22 ++++++ frontend/Dockerfile | 14 ++++ frontend/elm.json | 27 ++++++++ frontend/src/Main.elm | 151 ++++++++++++++++++++++++++++++++++++++++++ nginx/nginx.conf | 17 +++++ 12 files changed, 334 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 backend/CHANGELOG.md create mode 100644 backend/Dockerfile create mode 100644 backend/app/Main.hs create mode 100644 backend/backend.cabal create mode 100644 docker-compose.yml create mode 100644 frontend/Dockerfile create mode 100644 frontend/elm.json create mode 100644 frontend/src/Main.elm create mode 100644 nginx/nginx.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e27a60e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +elm-stuff/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f6a5f18 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea11694 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/backend/CHANGELOG.md b/backend/CHANGELOG.md new file mode 100644 index 0000000..213dbd4 --- /dev/null +++ b/backend/CHANGELOG.md @@ -0,0 +1,5 @@ +# Revision history for backend + +## 0.1.0.0 -- YYYY-mm-dd + +* First version. Released on an unsuspecting world. diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..ba9ddf8 --- /dev/null +++ b/backend/Dockerfile @@ -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"] \ No newline at end of file diff --git a/backend/app/Main.hs b/backend/app/Main.hs new file mode 100644 index 0000000..2670098 --- /dev/null +++ b/backend/app/Main.hs @@ -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" + ] diff --git a/backend/backend.cabal b/backend/backend.cabal new file mode 100644 index 0000000..6f67b6f --- /dev/null +++ b/backend/backend.cabal @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..bac1251 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..bf1022e --- /dev/null +++ b/frontend/Dockerfile @@ -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 \ No newline at end of file diff --git a/frontend/elm.json b/frontend/elm.json new file mode 100644 index 0000000..64ee788 --- /dev/null +++ b/frontend/elm.json @@ -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": {} + } +} diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm new file mode 100644 index 0000000..7215f79 --- /dev/null +++ b/frontend/src/Main.elm @@ -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 \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..d2a699a --- /dev/null +++ b/nginx/nginx.conf @@ -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; + } + } +} \ No newline at end of file