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

Merge main changes to backend/python #93

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ SERVER_NAME=yourdomainename.com # your domain name, for the nginx configuration

# mongodb setup
DB_NAME=XXX
DB_USER=XXX
DB_PASSWORD=XXX
DB_PORT=XXX

# cors
CLIENT_ORIGIN=http://localhost:$REACT_PORT # URL of the frontend, to make a cors exception
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
# Make sure the actual branch is checked out when running on pull requests
ref: ${{ github.head_ref }}

- name: Prettify code
uses: creyD/prettier_action@v4.3
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ cp .env.example .env.development

After copying the example config of `.env`, you must fill in the missing information in this file. Check the example for more information.

> If you don't know how to deploy your database, consider using [Atlas](https://www.mongodb.com/atlas/database).

#### Backend

From the root directory of the repository, do the following:
Expand Down
7 changes: 3 additions & 4 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const articleRoutes = require('./routes/articles')
const httpServer = createServer(app)
require('dotenv').config({ path: `../.env.${process.env.NODE_ENV}` })

const { DB_USER, DB_PASSWORD, DB_NAME, NODE_PORT, CLIENT_ORIGIN } = process.env
const { DB_PORT, DB_NAME, NODE_PORT, CLIENT_ORIGIN } = process.env

app.use(cors({ origin: CLIENT_ORIGIN || 'http://localhost:3000' }))
app.use(express.json({ limit: '1MB' }))
Expand All @@ -19,10 +19,9 @@ app.get('/', (req, res) => {
res.send('iscsc.fr is running')
})

uri = `mongodb://mongodb:${DB_PORT}/${DB_NAME}?retryWrites=true&w=majority`
mongoose
.connect(
`mongodb+srv://${DB_USER}:${DB_PASSWORD}@iscsc.a11re32.mongodb.net/${DB_NAME}?retryWrites=true&w=majority`
)
.connect(uri)
.then(() => {
httpServer.listen(NODE_PORT || 3001, () => {
console.log(`Server listening: http://localhost:${NODE_PORT}`)
Expand Down
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
version: "3.8"

services:
mongodb:
image: docker.io/bitnami/mongodb:4.4
networks:
- database
restart: always
env_file: ./.env.production
ports:
- $DB_PORT:$DB_PORT
volumes:
- "./mongodb:/data/db"

node-app:
depends_on:
- mongodb
networks:
- proxy
- database
build: ./backend
restart: unless-stopped
env_file: ./.env.production
Expand All @@ -11,6 +27,8 @@ services:
react-app:
depends_on:
- node-app
networks:
- proxy
env_file: ./.env.production
build:
context: ./frontend
Expand All @@ -19,9 +37,12 @@ services:

nginx:
restart: always
networks:
- proxy
depends_on:
- react-app
- node-app
- mongodb
build:
context: ./nginx
args:
Expand All @@ -40,3 +61,7 @@ services:
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw

networks:
proxy:
database:
16 changes: 6 additions & 10 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ function App() {
<div className="pages">
<Routes>
<Route path="/" element={<Navigate to="/blog" />} />
<Route path="/blog" element={<Blog />} />
<Route
path="/blog/create-article"
element={user ? <CreateArticle /> : <Navigate to="/login" />}
element={
user ? <CreateArticle /> : <Login next="/blog/create-article" />
}
/>
<Route path="/blog/:id" element={<Article />} />
<Route
path="login"
element={!user ? <Login /> : <Navigate to="/" />}
/>
<Route
path="signup"
element={!user ? <Signup /> : <Navigate to="/" />}
/>
<Route path="/blog" element={<Blog />} />
<Route path="/login" element={<Login next="/" />} />
<Route path="/signup" element={<Signup next="/" />} />
</Routes>
</div>
</BrowserRouter>
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/pages/Login.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { useNavigate } from 'react-router-dom'
import { useAuthContext } from '../hooks/useAuthContext'
import { useLogin } from '../hooks/useLogin'

const { useState } = require('react')

const Login = () => {
const Login = ({ next }) => {
const navigate = useNavigate()
const { user } = useAuthContext()
if (user) navigate(next)

const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const { login, error, isLoading, ok } = useLogin()
const navigate = useNavigate()

const handleSubmit = async e => {
e.preventDefault()
await login(email, password)
if (ok) {
navigate('/')
navigate(next)
}
}

Expand Down
8 changes: 6 additions & 2 deletions frontend/src/pages/Signup.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useNavigate } from 'react-router-dom'
import { useAuthContext } from '../hooks/useAuthContext'
import { useSignup } from '../hooks/useSignup'
const { useState } = require('react')

const Signup = () => {
const Signup = ({ next }) => {
const navigate = useNavigate()
const { user } = useAuthContext()
if (user) navigate(next)

const [email, setEmail] = useState('')
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const { signup, isLoading, error, ok } = useSignup()
const navigate = useNavigate()

const handleSubmit = async e => {
e.preventDefault()
Expand Down