Skip to content

Commit

Permalink
chore(pay): change env variable pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Dec 5, 2023
1 parent 3fea78e commit c539c99
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 125 deletions.
68 changes: 29 additions & 39 deletions apps/pay/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
# Install dependencies only when needed
FROM node:20-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat python3 make g++
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn global add node-gyp \
&& yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build && yarn install --production --ignore-scripts --prefer-offline

# Production image, copy all the files and run next
FROM node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

USER nextjs

EXPOSE 3000

ARG BUILDTIME
FROM nixos/nix:latest AS builder
ARG APP=pay

COPY . /workdir
WORKDIR /workdir

RUN set -eux; \
nix \
--extra-experimental-features "nix-command flakes impure-derivations ca-derivations" \
--option filter-syscalls false \
build \
".#$APP";

RUN mkdir -p /tmp/nix-store-closure /tmp/local-bin
RUN cp -R $(nix-store --query --requisites result/) /tmp/nix-store-closure
RUN ln -snf $(nix-store --query result/)/bin/* /tmp/local-bin/

FROM gcr.io/distroless/static-debian11 AS final
ARG APP=pay

WORKDIR /app/$APP
COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/local-bin/* /usr/local/bin/

USER 1000

ARG COMMITHASH
ENV BUILDTIME ${BUILDTIME}
ENV COMMITHASH ${COMMITHASH}

CMD ["node_modules/.bin/next", "start"]
CMD [ \
"/usr/local/bin/run" \
]
21 changes: 9 additions & 12 deletions apps/pay/app/lnurlp/[username]/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { gql } from "@apollo/client"

import Redis from "ioredis"

import { URL_HOST_DOMAIN } from "../../../../config/config"
import { NOSTR_PUBKEY } from "../../../../lib/config"
import { env } from "../../../../env"
import {
AccountDefaultWalletDocument,
AccountDefaultWalletQuery,
Expand Down Expand Up @@ -42,29 +41,29 @@ gql`
}
`

const nostrEnabled = !!NOSTR_PUBKEY
const nostrEnabled = !!env.NOSTR_PUBKEY

let redis: Redis | null = null

if (nostrEnabled) {
const connectionObj = {
sentinelPassword: process.env.REDIS_PASSWORD,
sentinelPassword: env.REDIS_PASSWORD,
sentinels: [
{
host: `${process.env.REDIS_0_DNS}`,
host: `${env.REDIS_0_DNS}`,
port: 26379,
},
{
host: `${process.env.REDIS_1_DNS}`,
host: `${env.REDIS_1_DNS}`,
port: 26379,
},
{
host: `${process.env.REDIS_2_DNS}`,
host: `${env.REDIS_2_DNS}`,
port: 26379,
},
],
name: process.env.REDIS_MASTER_NAME ?? "mymaster",
password: process.env.REDIS_PASSWORD,
name: env.REDIS_MASTER_NAME ?? "mymaster",
password: env.REDIS_PASSWORD,
}

redis = new Redis(connectionObj)
Expand All @@ -76,8 +75,6 @@ export async function GET(
request: Request,
{ params }: { params: { username: string } },
) {
console.log(NOSTR_PUBKEY)

const { searchParams } = new URL(request.url)

const username = params.username
Expand Down Expand Up @@ -119,7 +116,7 @@ export async function GET(

const metadata = JSON.stringify([
["text/plain", `Payment to ${username}`],
["text/identifier", `${username}@${URL_HOST_DOMAIN}`],
["text/identifier", `${username}@${env.NEXT_PUBLIC_PAY_DOMAIN}`],
])

// lnurl generate invoice
Expand Down
4 changes: 2 additions & 2 deletions apps/pay/app/lnurlp/[username]/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InMemoryCache,
} from "@apollo/client"

import { GRAPHQL_URL_INTERNAL } from "../../../lib/config"
import { env } from "../../../env"

const ipForwardingMiddleware = new ApolloLink((operation, forward) => {
operation.setContext(({ headers = {} }) => ({
Expand All @@ -25,7 +25,7 @@ export const client = new ApolloClient({
link: concat(
ipForwardingMiddleware,
new HttpLink({
uri: GRAPHQL_URL_INTERNAL,
uri: env.CORE_GQL_URL_INTRANET,
fetchOptions: { cache: "no-store" },
}),
),
Expand Down
13 changes: 5 additions & 8 deletions apps/pay/app/lnurlp/[username]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NextResponse } from "next/server"

import { URL_HOST_DOMAIN } from "../../../config/config"
import { NOSTR_PUBKEY, PAY_SERVER } from "../../../lib/config"
import { env } from "../../../env"
import {
AccountDefaultWalletDocument,
AccountDefaultWalletQuery,
Expand All @@ -13,14 +12,12 @@ import { client } from "./graphql"

const COMMENT_SIZE = 2000 // 2000 characters max for GET

const nostrEnabled = !!NOSTR_PUBKEY
const nostrEnabled = !!env.NOSTR_PUBKEY

export async function GET(
request: Request,
{ params }: { params: { username: string } },
) {
console.log(NOSTR_PUBKEY)

const { searchParams } = new URL(request.url)

const username = params.username
Expand Down Expand Up @@ -73,10 +70,10 @@ export async function GET(

const metadata = JSON.stringify([
["text/plain", `Payment to ${username}`],
["text/identifier", `${username}@${URL_HOST_DOMAIN}`],
["text/identifier", `${username}@${env.NEXT_PUBLIC_PAY_DOMAIN}`],
])

const callback = `${PAY_SERVER}/lnurlp/${username}/callback`
const callback = `${env.PAY_URL}/lnurlp/${username}/callback`

let minSendable = 1000 // 1 sat in millisat
let maxSendable = 100000000000 // 1 BTC in millisat
Expand All @@ -96,7 +93,7 @@ export async function GET(
...(nostrEnabled
? {
allowsNostr: true,
nostrPubkey: NOSTR_PUBKEY,
nostrPubkey: env.NOSTR_PUBKEY,
}
: {}),
})
Expand Down
4 changes: 2 additions & 2 deletions apps/pay/components/Layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRouter } from "next/router"
import React from "react"
import { Image, OverlayTrigger, Tooltip } from "react-bootstrap"

import { URL_HOST_DOMAIN } from "../../config/config"
import { env } from "../../env"

import styles from "./app-layout.module.css"

Expand All @@ -19,7 +19,7 @@ const AppLayout = ({ children, username }: Props) => {
const [openSideBar, setOpenSideBar] = React.useState<boolean>(false)
const [copied, setCopied] = React.useState<boolean>(false)
const lightningAddr = username
? `${username?.toString().toLowerCase()}@${URL_HOST_DOMAIN}`
? `${username?.toString().toLowerCase()}@${env.NEXT_PUBLIC_PAY_DOMAIN}`
: ""

const cashRegisterLink = username ? `/${username}` : "#"
Expand Down
5 changes: 3 additions & 2 deletions apps/pay/components/ParsePOSPayment/Receive-Invoice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Tooltip from "react-bootstrap/Tooltip"
import { QRCode } from "react-qrcode-logo"
import { useScreenshot } from "use-react-screenshot"

import { URL_HOST_DOMAIN, USD_INVOICE_EXPIRE_INTERVAL } from "../../config/config"
import { USD_INVOICE_EXPIRE_INTERVAL } from "../../config/config"
import { env } from "../../env"
import useCreateInvoice from "../../hooks/use-Create-Invoice"
import { LnInvoiceObject } from "../../lib/graphql/index.types.d"
import useSatPrice from "../../lib/use-sat-price"
Expand Down Expand Up @@ -52,7 +53,7 @@ function ReceiveInvoice({ recipientWalletCurrency, walletId, state, dispatch }:

const shareUrl =
!amount && !unit && !memo
? `https://${URL_HOST_DOMAIN}/${username}?amount=${
? `https://${env.NEXT_PUBLIC_PAY_DOMAIN}/${username}?amount=${
state.currentAmount
}&sats=${usdToSats(
state.currentAmount,
Expand Down
2 changes: 0 additions & 2 deletions apps/pay/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const USD_INVOICE_EXPIRE_INTERVAL = 60 * 5
export const MAX_INPUT_VALUE_LENGTH = 14
export const CASH_REGISTER_DESCRIPTION = "Blink POS Cash Register"
export const APP_DESCRIPTION = "Blink official lightning network node"
export const URL_HOST_DOMAIN = "blink.sv"
33 changes: 33 additions & 0 deletions apps/pay/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createEnv } from "@t3-oss/env-nextjs"
import { z } from "zod"

export const env = createEnv({
server: {
CORE_GQL_URL_INTRANET: z.string(), // Use intranet URL to preserve tracing headers
PAY_URL: z.string(),
NOSTR_PUBKEY: z.string().optional(),
REDIS_PASSWORD: z.string().optional(),
REDIS_MASTER_NAME: z.string().optional(),
REDIS_0_DNS: z.string().optional(),
REDIS_1_DNS: z.string().optional(),
REDIS_2_DNS: z.string().optional(),
},
client: {
NEXT_PUBLIC_CORE_GQL_URL: z.string(),
NEXT_PUBLIC_CORE_GQL_WEB_SOCKET_URL: z.string(),
NEXT_PUBLIC_PAY_DOMAIN: z.string(),
},
runtimeEnv: {
CORE_GQL_URL_INTRANET: process.env.CORE_GQL_URL_INTRANET,
NEXT_PUBLIC_CORE_GQL_URL: process.env.NEXT_PUBLIC_CORE_GQL_URL,
NEXT_PUBLIC_CORE_GQL_WEB_SOCKET_URL: process.env.NEXT_PUBLIC_CORE_GQL_WEB_SOCKET_URL,
PAY_URL: process.env.PAY_URL,
NEXT_PUBLIC_PAY_DOMAIN: process.env.NEXT_PUBLIC_PAY_DOMAIN,
NOSTR_PUBKEY: process.env.NOSTR_PUBKEY, // Optional but required for Nostr Zaps
REDIS_PASSWORD: process.env.REDIS_PASSWORD, // Optional but required for Nostr Zaps
REDIS_MASTER_NAME: process.env.REDIS_MASTER_NAME, // Optional but required for Nostr Zaps
REDIS_0_DNS: process.env.REDIS_0_DNS, // Optional but required for Nostr Zaps
REDIS_1_DNS: process.env.REDIS_1_DNS, // Optional but required for Nostr Zaps
REDIS_2_DNS: process.env.REDIS_2_DNS, // Optional but required for Nostr Zaps
},
})
43 changes: 0 additions & 43 deletions apps/pay/lib/config.ts

This file was deleted.

6 changes: 3 additions & 3 deletions apps/pay/lib/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { onError } from "@apollo/client/link/error"

import { createClient } from "graphql-ws"

import { GRAPHQL_URL, GRAPHQL_WEBSOCKET_URL } from "./config"
import { env } from "../env"

const httpLink = new HttpLink({
uri: GRAPHQL_URL,
uri: env.NEXT_PUBLIC_CORE_GQL_URL,
})

const wsLink = new GraphQLWsLink(
createClient({
url: GRAPHQL_WEBSOCKET_URL,
url: env.NEXT_PUBLIC_CORE_GQL_WEB_SOCKET_URL,
retryAttempts: 12,
connectionParams: {},
shouldRetry: (errOrCloseEvent) => {
Expand Down
14 changes: 8 additions & 6 deletions apps/pay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@apollo/client": "^3.7.12",
"@galoymoney/client": "^0.2.2",
"@t3-oss/env-nextjs": "^0.6.1",
"bech32": "^2.0.0",
"bitcoinjs-lib": "5.0.5",
"bolt11": "1.3.4",
Expand All @@ -35,7 +36,8 @@
"react-qrcode-logo": "^2.9.0",
"react-to-print": "^2.14.12",
"use-debounce": "^8.0.4",
"use-react-screenshot": "^3.0.0"
"use-react-screenshot": "^3.0.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@galoy/eslint-config": "workspace:^",
Expand All @@ -47,12 +49,17 @@
"@graphql-codegen/typescript-react-apollo": "^3.3.7",
"@types/eslint": "^8.2.0",
"@types/lodash.debounce": "^4.0.6",
"@types/node": "^20.8.9",
"@types/original-url": "^1.2.0",
"@types/prettier": "^2.7.0",
"@types/react": "^18.2.33",
"@types/react-dev-utils": "^9.0.11",
"@types/react-dom": "^18.2.14",
"@types/react-lottie": "^1.2.6",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.51.0",
"eslint": "^8.52.0",
"eslint-config-next": "13.5.6",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.26.0",
Expand All @@ -65,11 +72,6 @@
"prettier": "^2.4.1",
"react-dev-utils": "^12.0.1",
"ts-pnp": "1.2.0",
"@types/node": "^20.8.9",
"@types/react": "^18.2.33",
"@types/react-dom": "^18.2.14",
"eslint": "^8.52.0",
"eslint-config-next": "13.5.6",
"typescript": "5.2.2"
},
"eslintConfig": {
Expand Down
Loading

0 comments on commit c539c99

Please sign in to comment.