Skip to content

Commit

Permalink
refactor: install the slim router for the php (#27)
Browse files Browse the repository at this point in the history
feat: setup a router

fix: correct nginx config to work with router

feat: serve the web pages using the router

refactor: require autoload and lib from index.php

fix: update interface according to new format

---------

Co-authored-by: Alex-zReeZ <[email protected]>
  • Loading branch information
Rignchen and Alex-zReeZ authored Jul 5, 2024
1 parent 79b8d1b commit b7364e6
Show file tree
Hide file tree
Showing 18 changed files with 898 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: test login with admin return token
# testing the login make sure that the database, the php, nginx, postgrest and the migration works
run: |
export TOKEN=$(curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "admin"}' http://localhost/php/login.php | jq ".token")
export TOKEN=$(curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "admin"}' http://localhost/php/login | jq ".token")
if [ "$TOKEN" == "null" ] || [ -z "$TOKEN" ]; then
echo "token is empty"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile/Dockerfile.Php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN composer install
# copy required files to new image
FROM php:8.3-fpm-alpine
COPY ../php/public /var/www/memoires-info/php/public
COPY ../php/private /var/www/memoires-info/php/private
COPY ../php/src /var/www/memoires-info/php/src
COPY ../php/lib.php /var/www/memoires-info/php
COPY ../.env /var/www/memoires-info/php/
COPY --from=0 ../app/vendor /var/www/memoires-info/php/vendor
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ clear && cat esp32/esp32.yaml | sed -n '31,$p'
- Replace the XXX... at the end of the code with the token of your esp32\
you can generate this token by running the following command on the server (replace `255.255.255.255` with the ip address of the esp32)
```bash
export TOKEN=$(curl 'http://127.0.0.1/php/login.php' -d '{"username":"admin","password":"admin"}' -X POST | jq '.token' -r)
curl -H "Authorization: Bearer $TOKEN" "http://127.0.0.1/php/esp.php" -d '{"ip":"255.255.255.255"}' -X POST | jq '.token' -r
export TOKEN=$(curl 'http://127.0.0.1/php/login' -d '{"username":"admin","password":"admin"}' -X POST 2> /dev/null | jq '.token' -r)
curl -H "Authorization: Bearer $TOKEN" "http://127.0.0.1/php/esp" -d '{"ip":"255.255.255.255"}' -X POST 2> /dev/null | jq '.token' -r
```
- Press `INSTALL` and select `Wirelessly` (the 1st option)
- Wait for the installation to finish
Expand Down
4 changes: 3 additions & 1 deletion nextjs-interface/src/app/ui/login/LoginElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export function LoginElement() {
const submit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();

await fetch(`/php/login.php?username=${username}&password=${password}`, {
await fetch(`/php/login`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ username: username, password: password }),
})
.then((response) => response.json())
.then((reponse) => {
Expand Down
2 changes: 1 addition & 1 deletion nextjs-interface/src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ interface DataRecord {

export const useFetchToken = (ip: any) => {
const [data, setData] = useState<string>("");
const url = `/php/esp.php`;
const url = `/php/esp`;
const response = fetch(url, {
method: "POST",
headers: {
Expand Down
13 changes: 8 additions & 5 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ server {
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

location /php/ {
location /php {
root /var/www/memoires-info/php/private;
rewrite ^/php(.*)$ $1 break;
root /var/www/memoires-info/php/public;
fastcgi_pass php:9000;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/memoires-info/php/public/index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REQUEST_URI $uri;
fastcgi_index index.php;
fastcgi_pass php:9000;
}

location /esp/ {
Expand Down
10 changes: 9 additions & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
"name": "rignchen/login",
"description": "login backend to autotificate users",
"type": "project",
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"require": {
"firebase/php-jwt": "^6.10",
"symfony/dotenv": "^7.0",
"ext-curl": "*"
"ext-curl": "*",
"slim/slim": "^4.13",
"slim/psr7": "^1.7",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"symfony/var-dumper": "^7.0",
Expand Down
Loading

0 comments on commit b7364e6

Please sign in to comment.