Skip to content

Commit

Permalink
Merge pull request #14 from timhabermaas/move-frontend-to-nextjs
Browse files Browse the repository at this point in the history
Frontend -> Next.js/React
  • Loading branch information
timhabermaas authored Jun 15, 2021
2 parents 314b36c + 30a9341 commit 5fdd1a2
Show file tree
Hide file tree
Showing 109 changed files with 5,232 additions and 82 deletions.
5 changes: 4 additions & 1 deletion app/assets/stylesheets/_base.css.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
body {
min-width: $width + 32px;
font-size: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif
font-family: Verdana, Arial, Helvetica, sans-serif;
height: 100vh;
display: flex;
flex-direction: column;
}

a, a:visited {
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/_content.css.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#content, div.fancybox-inner {
flex: 1 1 0%;

color: white;
background: #5C7DA0 image-url("content.gif") repeat-x;
.center {
Expand Down
4 changes: 3 additions & 1 deletion app/assets/stylesheets/_navigation.css.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
nav.main {
flex: none;

border-top: 1px solid $yellow;
background: image-url("navigation.gif") repeat-x;
overflow: hidden;
Expand Down Expand Up @@ -29,4 +31,4 @@ nav.main {
}
nav.main:after {
@include clear-fix;
}
}
9 changes: 9 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ def create
end
end

def show
if self.current_user
payload = { :user_id => current_user.id, :exp => Time.now.to_i + 30 * 3600 }
render :json => { :user_id => self.current_user, :token => JWT.encode(payload, ENV.fetch("HMAC_SECRET"), "HS256") }
else
render :json => { :error => "unauthorized" }, :status => :not_found
end
end

def destroy
self.current_user = nil
flash[:notice] = "You are now logged out."
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def subnavigation_path(puzzle)
end

def csv_download_path(user_id, puzzle_id)
payload = { :download_csv_for => current_user.id, :exp => Time.now.to_i + 30 * 3600 }
payload = { :user_id => current_user.id, :exp => Time.now.to_i + 30 * 3600 }
token = JWT.encode payload, ENV.fetch("HMAC_SECRET"), "HS256"
"/api/singles.csv?user_id=#{user_id}&puzzle_id=#{puzzle_id}&token=#{token}"
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Register</h1>

<%= semantic_form_for @user do |form| %>
<%= semantic_form_for @user, {url: register_path} do |form| %>
<%= form.inputs do %>
<%= form.input :name %>
<%= form.input :email %>
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
resource :session
match 'login' => 'sessions#new', :as => 'login'
match 'logout' => 'sessions#destroy', :as => 'logout'
match 'register' => 'users#new', :as => 'register'
match 'register' => 'users#new', :as => 'register', via: [:get]
match 'register' => 'users#create', :as => 'register', via: [:post]

match '*path' => "errors#not_found"
end
2 changes: 1 addition & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
set -e

env DOCKER_BUILDKIT=1 docker-compose -f docker-compose.yml -f docker-compose.prod.yml build web
docker-compose -f docker-compose.yml -f docker-compose.prod.yml run web bundle exec rake assets:precompile
docker-compose -f docker-compose.yml -f docker-compose.prod.yml run --rm web bundle exec rake assets:precompile
env DOCKER_BUILDKIT=1 docker-compose -f docker-compose.yml -f docker-compose.prod.yml build
21 changes: 21 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
version: "3.7"
services:
frontend:
build:
target: dev-runner
command: ["yarn", "dev", "-p", "4000"]
volumes:
- ./frontend:/app
- /app/node_modules/
ports:
- "4000:4000"
nginx:
volumes:
- ./public:/home/assets
- ./nginx/empty_ssl.conf:/etc/nginx/ssl.conf
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- "443:443"
- "8080:80"
records:
ports:
- "8081:8081"
records-build:
build:
context: ./records
dockerfile: Dockerfile
target: builder
depends_on:
- db
volumes:
- ./records:/home/rust/src/records
web:
ports:
- "3000:3000"
environment:
RAILS_ENV: development
db:
ports:
- "5432:5432"
3 changes: 3 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: "3.7"
services:
frontend:
build:
target: prod-runner
nginx:
volumes:
- /etc/letsencrypt:/etc/letsencrypt
Expand Down
16 changes: 7 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ services:
restart: always
volumes:
- postgres-volume:/var/lib/postgresql/data
frontend:
restart: always
build:
context: ./frontend
dockerfile: Dockerfile
image: timhabermaas/cubemania_frontend
nginx:
restart: always
build:
Expand All @@ -16,6 +22,7 @@ services:
- records
- web
- backup
- frontend
records:
build:
context: ./records
Expand All @@ -29,15 +36,6 @@ services:
DATABASE_URL: postgresql://postgres@db/cubemania_production
depends_on:
- db
records-build:
build:
context: ./records
dockerfile: Dockerfile
target: builder
depends_on:
- db
volumes:
- ./records:/home/rust/src/records
web:
build: .
image: timhabermaas/cubemania_rails
Expand Down
1 change: 1 addition & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
34 changes: 34 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
53 changes: 53 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Install dependencies only when needed
FROM node: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
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

FROM node:alpine AS dev-runner
WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules

EXPOSE 4000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
ENV NEXT_TELEMETRY_DISABLED 1

CMD ["yarn", "run", "dev", "-p", "4000"]

# Rebuild the source code only when needed
FROM node:alpine AS prod-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:alpine AS prod-runner
WORKDIR /app

ENV NODE_ENV production

# 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=prod-builder /app/public ./public
COPY --from=prod-builder /app/.next ./.next
COPY --from=prod-builder /app/node_modules ./node_modules
COPY --from=prod-builder /app/package.json ./package.json


EXPOSE 4000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
ENV NEXT_TELEMETRY_DISABLED 1

CMD ["yarn", "start", "-p", "4000"]
34 changes: 34 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
15 changes: 15 additions & 0 deletions frontend/commons/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function userPath(slug: string): string {
return `/users/${slug}`;
}

export const timer3x3x3Path: string = "/puzzles/3x3x3/timer";

export const records3x3x3Path: string = "/puzzles/3x3x3/records";

export const usersPath: string = "/users";

export const homePath: string = "/";

export function postPath(postId: number): string {
return `/posts/${postId}#comments`;
}
3 changes: 3 additions & 0 deletions frontend/commons/types/assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function assertExhausted(_x: never): never {
throw new Error("not exhausted");
}
5 changes: 5 additions & 0 deletions frontend/commons/types/page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Page = "Home" | "Timer" | "Users" | "Me" | "Records" | "Post";

export function eqPage(a: Page, b: Page) {
return a === b;
}
9 changes: 9 additions & 0 deletions frontend/components/Flash.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

export function Flash() {
return (
<div id="flash" className="flash notice" style={{ display: "none" }}>
<p>some content</p>
</div>
);
}
18 changes: 18 additions & 0 deletions frontend/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";

export function Footer() {
return (
<footer>
<p>
Founded by <a href="http://cubemania.org/users/tim">Tim Habermaas</a>,{" "}
<a
href="http://www.patrickstadler.de"
title="Patrick Stadler's Website"
>
Patrick Stadler
</a>{" "}
and Simon Wacker.
</p>
</footer>
);
}
Loading

0 comments on commit 5fdd1a2

Please sign in to comment.