Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elixir Chat Service #1621

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
73a68d9
nix shell for elixir
joshleecreates May 12, 2024
4eb3627
mix phx.new
joshleecreates May 12, 2024
1bc45e1
Adds OpenTelemetry to Phoenix
joshleecreates May 12, 2024
a705e95
docker compose for development postgres
joshleecreates May 12, 2024
f7a06ea
rename Chatservice to ChatService
joshleecreates May 12, 2024
fa930ac
mix phx.gen.channel
joshleecreates May 12, 2024
8d39847
cleaning up nix shell
joshleecreates May 21, 2024
265ccfa
listen on any interface
joshleecreates May 21, 2024
6162218
working towards user-based chats with session ids
joshleecreates May 21, 2024
c1f5c5e
persist chats
joshleecreates Jul 7, 2024
000f403
fix rel gitignore for chat service
joshleecreates Jul 8, 2024
a205b8d
adds chat service and db to docker-compose
joshleecreates Jul 9, 2024
f7e1d53
who needs primary keys?
joshleecreates Jul 9, 2024
7f603ad
Add chat endpoint to envoy
julianocosta89 Jul 9, 2024
19a9c77
Lint
julianocosta89 Jul 9, 2024
4028c50
Remove link
julianocosta89 Jul 9, 2024
40f7778
markdown links
julianocosta89 Jul 9, 2024
2d30f0b
Merge branch 'main' into elixir-chat-service
julianocosta89 Jul 9, 2024
8795397
fully removes tailwind so assets can be compiled
joshleecreates Jul 15, 2024
73f2833
Merge branch 'main' into elixir-chat-service
julianocosta89 Jul 31, 2024
14678aa
fix opentelemetry ecto config
joshleecreates Aug 3, 2024
1ece0e5
render messages
joshleecreates Aug 3, 2024
aea8787
adds manual spans for entire send message path. TODO: make these mess…
joshleecreates Aug 3, 2024
6a7631e
connected span context through to ecto
joshleecreates Aug 4, 2024
8d46b60
consumer span attributes
joshleecreates Aug 4, 2024
bf572f6
Merge branch 'main' into elixir-chat-service
joshleecreates Aug 14, 2024
b2c25f7
add copyrights
joshleecreates Aug 18, 2024
8811d4c
more copyrights
joshleecreates Aug 18, 2024
d925322
Merge branch 'main' into elixir-chat-service
julianocosta89 Aug 27, 2024
ffbe7b3
Merge branch 'main' into elixir-chat-service
joshleecreates Sep 4, 2024
fbd9619
Merge branch 'main' into elixir-chat-service
julianocosta89 Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ CART_SERVICE_PORT=7070
CART_SERVICE_ADDR=cartservice:${CART_SERVICE_PORT}
CART_SERVICE_DOCKERFILE=./src/cartservice/src/Dockerfile

# Chat Service
CHAT_SERVICE_PORT=4000
CHAT_SERVICE_HOST=chatservice
CHAT_SERVICE_DOCKERFILE=./src/chatservice/Dockerfile


# Checkout Service
CHECKOUT_SERVICE_PORT=5050
CHECKOUT_SERVICE_ADDR=checkoutservice:${CHECKOUT_SERVICE_PORT}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ docker-compose.override.yml
.settings

bin/
!src/chatservice/rel/overlays/bin
obj/
.vs/
.vscode
Expand Down
72 changes: 62 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ services:
condition: service_started
logging: *logging

# Cart service
chatservice:
image: ${IMAGE_NAME}:${DEMO_VERSION}-chatservice
container_name: chat-service
build:
context: ./
dockerfile: ${CHAT_SERVICE_DOCKERFILE}
cache_from:
- ${IMAGE_NAME}:${IMAGE_VERSION}-chatservice
deploy:
resources:
limits:
memory: 360M
restart: unless-stopped
ports:
- "${CHAT_SERVICE_PORT}:${CHAT_SERVICE_PORT}"
environment:
- CHAT_SERVICE_PORT
- OTEL_EXPORTER_OTLP_ENDPOINT=http://${OTEL_COLLECTOR_HOST}:${OTEL_COLLECTOR_PORT_HTTP}
- OTEL_RESOURCE_ATTRIBUTES
- OTEL_SERVICE_NAME=chatservice
- DATABASE_URL=ecto://chat:chat@chat-db:5432/chats
depends_on:
otelcol:
condition: service_started
logging: *logging

# Checkout service
checkoutservice:
image: ${IMAGE_NAME}:${DEMO_VERSION}-checkoutservice
Expand Down Expand Up @@ -323,23 +350,25 @@ services:
- "${ENVOY_PORT}:${ENVOY_PORT}"
- 10000:10000
environment:
- FRONTEND_PORT
- CHAT_SERVICE_HOST
- CHAT_SERVICE_PORT
- ENVOY_PORT
- FLAGD_HOST
- FLAGD_PORT
- FRONTEND_HOST
- LOCUST_WEB_HOST
- LOCUST_WEB_PORT
- GRAFANA_SERVICE_PORT
- FRONTEND_PORT
- GRAFANA_SERVICE_HOST
- JAEGER_SERVICE_PORT
- JAEGER_SERVICE_HOST
- OTEL_COLLECTOR_HOST
- GRAFANA_SERVICE_PORT
- IMAGE_PROVIDER_HOST
- IMAGE_PROVIDER_PORT
- JAEGER_SERVICE_HOST
- JAEGER_SERVICE_PORT
- LOCUST_WEB_HOST
- LOCUST_WEB_PORT
- OTEL_COLLECTOR_HOST
- OTEL_COLLECTOR_PORT_GRPC
- OTEL_COLLECTOR_PORT_HTTP
- OTEL_RESOURCE_ATTRIBUTES
- ENVOY_PORT
- FLAGD_HOST
- FLAGD_PORT
depends_on:
frontend:
condition: service_started
Expand Down Expand Up @@ -597,6 +626,29 @@ services:
logging:
*logging

# Postgres used by chat service
chat_postgres:
image: ${POSTGRES_IMAGE}
container_name: chat-db
user: postgres
deploy:
resources:
limits:
memory: 120M
restart: unless-stopped
environment:
- POSTGRES_USER=chat
- POSTGRES_DB=chats
- POSTGRES_PASSWORD=chat
volumes:
- ./src/chat_postgres/10-chat_schema.sql:/docker-entrypoint-initdb.d/10-chat_schema.sql
healthcheck:
test: ["CMD-SHELL", "pg_isready -d chats -U chat"]
interval: 10s
timeout: 5s
retries: 5
logging: *logging

# Kafka used by Checkout, Accounting, and Fraud Detection services
kafka:
image: ${IMAGE_NAME}:${DEMO_VERSION}-kafka
Expand Down
12 changes: 12 additions & 0 deletions src/chat_postgres/10-chat_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Copyright The OpenTelemetry Authors
-- SPDX-License-Identifier: Apache-2.0

CREATE TABLE public.messages (
topic character varying(255),
name character varying(255),
message character varying(255),
sent_at timestamp(0) without time zone,
inserted_at timestamp(0) without time zone NOT NULL,
updated_at timestamp(0) without time zone NOT NULL
);

21 changes: 21 additions & 0 deletions src/chatservice/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[
import_deps: [:ecto, :ecto_sql, :phoenix],
subdirectories: ["priv/*/migrations"],
plugins: [Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/*/seeds.exs"]
]
40 changes: 40 additions & 0 deletions src/chatservice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Temporary files, for example, from tests.
/tmp/

# Ignore package tarball (built via "mix hex.build").
chatservice-*.tar

# Ignore assets that are produced by build tools.
/priv/static/assets/

# Ignore digested assets cache.
/priv/static/cache_manifest.json

# In case you use Node.js/npm, you want to ignore these.
npm-debug.log
/assets/node_modules/

.nix-*

.elixir_ls
107 changes: 107 additions & 0 deletions src/chatservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian instead of
# Alpine to avoid DNS resolution issues in production.
#
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
# https://hub.docker.com/_/ubuntu?tab=tags
#
#
# This file is based on these images:
#
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20210902-slim - for the release image
# - https://pkgs.org/ - resource for finding needed packages
# - Ex: hexpm/elixir:1.13.3-erlang-25.0-debian-bullseye-20210902-slim
#
ARG ELIXIR_VERSION=1.14.5
ARG OTP_VERSION=23.0
ARG DEBIAN_VERSION=bullseye-20210902-slim

ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"

FROM ${BUILDER_IMAGE} as builder

# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
WORKDIR /app

# install hex + rebar
RUN mix local.hex --force --verbose
RUN mix local.rebar --force --verbose

# set build ENV
ENV MIX_ENV="prod"

# install mix dependencies
COPY ./src/chatservice/mix.exs ./src/chatservice/mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY ./src/chatservice/config/config.exs ./src/chatservice/config/${MIX_ENV}.exs config/
RUN mix deps.compile

COPY ./src/chatservice/lib lib

COPY ./src/chatservice/assets assets

# Compile assets
RUN mix assets.deploy

# Compile the release
RUN mix compile

# Changes to config/runtime.exs don't require recompiling the code
COPY ./src/chatservice/config/runtime.exs config/

COPY ./src/chatservice/rel rel
RUN mix release

# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}

RUN apt-get update -y && apt-get install -y libstdc++6 openssl libncurses5 locales \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR "/app"
RUN chown nobody /app

# set runner ENV
ENV MIX_ENV="prod"
ENV SECRET_KEY_BASE="mNhoOKKxgyvBIwbtw0P23waQcvUOmusb2U1moG2I7JQ3Bt6+MlGb5ZTrHwqbqy7j"

# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/chatservice ./

USER nobody

ENTRYPOINT ["/app/bin/server"]
18 changes: 18 additions & 0 deletions src/chatservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ChatService

To start your Phoenix server:

* Run `mix setup` to install and setup dependencies
* Start Phoenix endpoint with `mix phx.server` or inside IEx with `iex -S mix phx.server`

Now you can visit `http://localhost:4000` from your browser.

Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html).

## Learn more

* Official website: <https://www.phoenixframework.org/>
* Guides: <https://hexdocs.pm/phoenix/overview.html>
* Docs: <https://hexdocs.pm/phoenix>
* Forum: <https://elixirforum.com/c/phoenix-forum>
* Source: <https://github.com/phoenixframework/phoenix>
17 changes: 17 additions & 0 deletions src/chatservice/assets/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/****
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
****/
Loading
Loading