Skip to content

Commit

Permalink
Add docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Sideris committed May 12, 2020
1 parent f30bf3e commit de80796
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 12 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
68 changes: 68 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# Build the frontend.
#
FROM node:10 as dashboard-builder

WORKDIR /usr/src/app

COPY dashboard ./

RUN npm install && npm run prod

#
# Build the server.
#
FROM alpine:edge as server-builder

WORKDIR /build

RUN apk upgrade --update-cache --available && \
apk add ghc \
alpine-sdk \
gmp-dev \
libffi \
libffi-dev \
musl-dev \
zlib-dev \
ncurses-dev \
postgresql-dev \
cabal \
ca-certificates && \
cabal update

COPY UNLICENSE ./
COPY app/ ./app
COPY README.md ./
COPY hakatime.cabal ./
COPY src/ ./src
COPY test/ ./test

RUN cabal build -j3 -O2 hakatime && \
cabal install -j3 -O2 hakatime && \
mkdir -p /app/bin && \
cp ~/.cabal/bin/hakatime /app/bin/hakatime

FROM alpine:edge

RUN apk add --no-cache \
libffi-dev \
gmp-dev \
zlib-dev \
ncurses-dev \
postgresql-dev && \
# Remove files that we don't' need.
rm -rf /usr/lib/libLLVM* \
/usr/lib/libclang* \
/usr/lib/llvm10 \
/usr/lib/clang \
/usr/include

COPY --from=dashboard-builder /usr/src/app/dist /app/bin/dashboard
COPY --from=server-builder /app/bin/hakatime /app/bin/hakatime

EXPOSE 8080

ENV HAKA_PORT 8080
ENV HAKA_DASHBOARD_PATH /app/bin/dashboard

CMD ["/app/bin/hakatime", "run"]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ ui:

gen-nix:
cabal2nix --no-haddock --no-check . > default.nix

build-img:
docker build -t hakatime:latest .
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# hakatime

[![CircleCI](https://circleci.com/gh/mujx/hakatime.svg?style=svg)](https://circleci.com/gh/mujx/hakatime)
[![Docker build](https://img.shields.io/docker/cloud/build/mujx/hakatime)](https://hub.docker.com/r/mujx/hakatime/builds)
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/)

Hakatime is a server implementation of [Wakatime](https://wakatime.com/). It
Expand Down
49 changes: 38 additions & 11 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ where

import Control.Exception (try)
import Control.Monad.Trans.Except (ExceptT (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as Bs
import qualified Haka.Authentication as Auth
import qualified Haka.Cli as Cli
import qualified Haka.Heartbeats as Heartbeats
Expand All @@ -19,6 +21,7 @@ import Network.Wai.Logger (withStdoutLogger)
import Network.Wai.Middleware.Cors
import qualified Options.Applicative as Opt
import Servant
import System.Environment.MrEnv (envAsInt, envAsString)
import System.IO (stdout)

type Static = Raw
Expand All @@ -32,13 +35,13 @@ type HakaAPI =
:<|> Static

-- The API handlers should be presented in the same order as in the API type.
server :: ServerT HakaAPI AppM
server =
server :: ServerSettings -> ServerT HakaAPI AppM
server settings =
Heartbeats.server
:<|> Stats.server
:<|> Projects.server
:<|> Auth.server
:<|> serveDirectoryFileServer "./dashboard/dist/"
:<|> serveDirectoryFileServer (hakaDashboardPath settings)

api :: Proxy HakaAPI
api = Proxy
Expand All @@ -47,26 +50,26 @@ api = Proxy
nt :: AppCtx -> AppM a -> Handler a
nt ctx = Handler . ExceptT . try . runAppT ctx

app :: AppCtx -> Application
app conf =
app :: ServerSettings -> AppCtx -> Application
app settings conf =
cors (const $ Just policy)
$ serve api
$ hoistServer api (nt conf) server
$ hoistServer api (nt conf) (server settings)
where
policy =
simpleCorsResourcePolicy
{ corsRequestHeaders = ["content-type", "authorization"],
-- TODO: Make this list configurable.
corsOrigins = Just (["http://localhost:8080"], True)
corsOrigins = Just ([hakaCorsUrl settings], True)
}

-- TODO: Write method to initialize logging based on ENV variables.
-- Env
-- LogLevel
-- Verbosity

initApp :: (AppCtx -> Application) -> IO ()
initApp unApp = do
initApp :: ServerSettings -> (AppCtx -> Application) -> IO ()
initApp settings unApp = do
dbsettings <- Cli.getDbSettings
dbPool <- HasqlPool.acquire (10, 1, dbsettings)
let ns = Namespace {unNamespace = ["server"]}
Expand All @@ -79,7 +82,7 @@ initApp unApp = do
lsContext = mempty
}
withStdoutLogger $ \logger -> do
let conf = setPort 8080 $ setLogger logger defaultSettings
let conf = setPort (hakaPort settings) $ setLogger logger defaultSettings
runSettings
conf
( unApp
Expand All @@ -89,7 +92,31 @@ initApp unApp = do
}
)

-- | Server configuration settings.
data ServerSettings
= ServerSettings
{ -- | Where the service will listen to.
hakaPort :: Int,
-- | What domain to allow.
hakaCorsUrl :: ByteString,
-- | Where to look for dashboard's static files.
hakaDashboardPath :: FilePath
}

getServerSettings :: IO ServerSettings
getServerSettings = do
hPort <- envAsInt "HAKA_PORT" 8080
hCorsUrl <- Bs.pack <$> envAsString "HAKA_CORS_URL" "http://localhost:8080"
hDashboardPath <- envAsString "HAKA_DASHBOARD_PATH" "./dashboard/dist"
return $
ServerSettings
{ hakaPort = hPort,
hakaCorsUrl = hCorsUrl,
hakaDashboardPath = hDashboardPath
}

main :: IO ()
main = do
parsedOpts <- Opt.execParser Cli.opts
Cli.handleCommand (Cli.serverCmd parsedOpts) (initApp app)
srvSettings <- getServerSettings
Cli.handleCommand (Cli.serverCmd parsedOpts) (initApp srvSettings (app srvSettings))
1 change: 0 additions & 1 deletion hakatime.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ maintainer: [email protected]
category: Web, UI
build-type: Simple
extra-doc-files: README.md
, CHANGELOG.md
tested-with: GHC == 8.8.3

executable hakatime
Expand Down

0 comments on commit de80796

Please sign in to comment.