Skip to content

Commit

Permalink
Added postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
Koudy committed Jan 3, 2021
1 parent e57ad85 commit 0975a0b
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ APP_SECRET=b714a52506e33cdbe10a883b083c5b54
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"
POSTGRES_USER=main
POSTGRES_PASSWORD=main
POSTGRES_DB=lottery
POSTGRES_HOST=postgresql
DATABASE_URL="postgresql://main:main@postgresql:5432/lottery?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
###< symfony/phpunit-bridge ###

/logs/*

.idea
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"symfony/monolog-bundle": "^3.6",
"symfony/notifier": "5.2.*",
"symfony/process": "5.2.*",
"symfony/orm-pack": "2.1.*",
"symfony/property-access": "5.2.*",
"symfony/property-info": "5.2.*",
"symfony/proxy-manager-bridge": "5.2.*",
Expand All @@ -36,6 +37,7 @@
"symfony/string": "5.2.*",
"symfony/translation": "5.2.*",
"symfony/twig-bundle": "^5.2",
"symfony/twig-pack": "^1.0",
"symfony/validator": "5.2.*",
"symfony/web-link": "5.2.*",
"symfony/yaml": "5.2.*",
Expand Down
86 changes: 85 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
doctrine:
dbal:
driver: pdo_pgsql
url: '%env(resolve:DATABASE_URL)%'
host: '%env(resolve:POSTGRES_HOST)%'
dbname: '%env(resolve:POSTGRES_DB)%'
user: '%env(resolve:POSTGRES_USER)%'
password: '%env(resolve:POSTGRES_PASSWORD)%'

# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
Expand Down
14 changes: 11 additions & 3 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
security:
encoders:
App\Entity\User:
algorithm: auto

# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
enable_authenticator_manager: true
providers:
users_in_memory: { memory: null }
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: name
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: true
lazy: true
provider: users_in_memory
provider: app_user_provider

# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
Expand Down
4 changes: 4 additions & 0 deletions config/routes/routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
app_lucky_number:
path: /lucky/number
controller: App\Controller\LuckyController::number
methods: GET
14 changes: 12 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ services:
volumes:
- ./:/app
working_dir: /app
env_file:
- .env

webapp:
container_name: 'nginx'
container_name: 'nginx'
image: nginx:1.19.6-alpine
hostname: 'lottery'
domainname: lottery.docker
Expand All @@ -29,7 +31,15 @@ services:
- ./docker/nginx/conf.d/:/etc/nginx/conf.d
- ./logs:/var/log
working_dir: /app
domainname: www.example.co.uk
restart: always
depends_on:
- fpm

db:
container_name: 'postgresql'
image: postgres:13.1-alpine
ports:
- "49198:5432"
env_file:
- .env
restart: always
18 changes: 18 additions & 0 deletions src/Controller/LuckyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class LuckyController extends AbstractController
{
public function number(): Response
{
$number = random_int(0, 100);

return $this->render('lucky/number.html.twig', [
'number' => $number,
]);
}
}
115 changes: 115 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $name;

/**
* @ORM\Column(type="json")
*/
private $roles = [];

/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;

public function getId(): ?int
{
return $this->id;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->name;
}

/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';

return array_unique($roles);
}

public function setRoles(array $roles): self
{
$this->roles = $roles;

return $this;
}

/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}

public function setPassword(string $password): self
{
$this->password = $password;

return $this;
}

/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}

/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
}
Loading

0 comments on commit 0975a0b

Please sign in to comment.