├── .gitignore ├── Lumen Microservice.postman_collection.json ├── LumenApiGateway ├── .env.example ├── .gitignore ├── app │ ├── Console │ │ ├── Commands │ │ │ └── .gitkeep │ │ └── Kernel.php │ ├── Events │ │ ├── Event.php │ │ └── ExampleEvent.php │ ├── Exceptions │ │ └── Handler.php │ ├── Http │ │ ├── Controllers │ │ │ ├── Author │ │ │ │ └── AuthorController.php │ │ │ ├── Book │ │ │ │ └── BookController.php │ │ │ ├── Controller.php │ │ │ └── ExampleController.php │ │ └── Middleware │ │ │ ├── Authenticate.php │ │ │ └── ExampleMiddleware.php │ ├── Jobs │ │ ├── ExampleJob.php │ │ └── Job.php │ ├── Listeners │ │ └── ExampleListener.php │ ├── Providers │ │ ├── AppServiceProvider.php │ │ ├── AuthServiceProvider.php │ │ └── EventServiceProvider.php │ ├── Services │ │ ├── AuthorService.php │ │ └── BookService.php │ ├── Traits │ │ ├── ApiResponser.php │ │ └── ConsumeExternalService.php │ └── User.php ├── artisan ├── bootstrap │ └── app.php ├── composer.json ├── composer.lock ├── config │ ├── auth.php │ └── services.php ├── database │ ├── factories │ │ └── ModelFactory.php │ ├── migrations │ │ └── .gitkeep │ └── seeds │ │ └── DatabaseSeeder.php ├── phpunit.xml ├── public │ ├── .htaccess │ └── index.php ├── readme.md ├── resources │ └── views │ │ └── .gitkeep ├── routes │ └── web.php ├── storage │ ├── app │ │ └── .gitignore │ ├── framework │ │ ├── cache │ │ │ └── .gitignore │ │ └── views │ │ │ └── .gitignore │ └── logs │ │ └── .gitignore └── tests │ ├── ExampleTest.php │ └── TestCase.php ├── LumenAuthorApi ├── .env.example ├── .gitignore ├── app │ ├── Console │ │ ├── Commands │ │ │ └── .gitkeep │ │ └── Kernel.php │ ├── Events │ │ ├── Event.php │ │ └── ExampleEvent.php │ ├── Exceptions │ │ └── Handler.php │ ├── Http │ │ ├── Controllers │ │ │ ├── Author │ │ │ │ └── AuthorController.php │ │ │ └── Controller.php │ │ └── Middleware │ │ │ ├── Authenticate.php │ │ │ ├── AuthenticateAccessMiddleware.php │ │ │ └── ExampleMiddleware.php │ ├── Jobs │ │ ├── ExampleJob.php │ │ └── Job.php │ ├── Listeners │ │ └── ExampleListener.php │ ├── Models │ │ └── User.php │ ├── Providers │ │ ├── AppServiceProvider.php │ │ ├── AuthServiceProvider.php │ │ └── EventServiceProvider.php │ └── Traits │ │ └── ApiResponser.php ├── artisan ├── bootstrap │ └── app.php ├── composer.json ├── composer.lock ├── database │ ├── database.sqlite │ ├── factories │ │ └── ModelFactory.php │ ├── migrations │ │ ├── .gitkeep │ │ └── 2019_01_01_162530_create_authors_table.php │ └── seeds │ │ └── DatabaseSeeder.php ├── phpunit.xml ├── public │ ├── .htaccess │ └── index.php ├── readme.md ├── resources │ └── views │ │ └── .gitkeep ├── routes │ └── web.php ├── storage │ ├── app │ │ └── .gitignore │ ├── framework │ │ ├── cache │ │ │ └── .gitignore │ │ └── views │ │ │ └── .gitignore │ └── logs │ │ └── .gitignore └── tests │ ├── ExampleTest.php │ └── TestCase.php ├── LumenBookApi ├── .env.example ├── .gitignore ├── app │ ├── Console │ │ ├── Commands │ │ │ └── .gitkeep │ │ └── Kernel.php │ ├── Events │ │ ├── Event.php │ │ └── ExampleEvent.php │ ├── Exceptions │ │ └── Handler.php │ ├── Http │ │ ├── Controllers │ │ │ ├── Book │ │ │ │ └── BookController.php │ │ │ └── Controller.php │ │ └── Middleware │ │ │ ├── Authenticate.php │ │ │ ├── AuthenticateAccessMiddleware.php │ │ │ └── ExampleMiddleware.php │ ├── Jobs │ │ ├── ExampleJob.php │ │ └── Job.php │ ├── Listeners │ │ └── ExampleListener.php │ ├── Models │ │ └── Book.php │ ├── Providers │ │ ├── AppServiceProvider.php │ │ ├── AuthServiceProvider.php │ │ └── EventServiceProvider.php │ └── Traits │ │ └── ApiResponser.php ├── artisan ├── bootstrap │ └── app.php ├── composer.json ├── composer.lock ├── database │ ├── database.sqlite │ ├── factories │ │ └── ModelFactory.php │ ├── migrations │ │ ├── .gitkeep │ │ └── 2019_01_02_202525_create_books_table.php │ └── seeds │ │ └── DatabaseSeeder.php ├── phpunit.xml ├── public │ ├── .htaccess │ └── index.php ├── readme.md ├── resources │ └── views │ │ └── .gitkeep ├── routes │ └── web.php ├── storage │ ├── app │ │ └── .gitignore │ ├── framework │ │ ├── cache │ │ │ └── .gitignore │ │ └── views │ │ │ └── .gitignore │ └── logs │ │ └── .gitignore └── tests │ ├── ExampleTest.php │ └── TestCase.php ├── architecture.png └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /LumenApiGateway/.env.example: -------------------------------------------------------------------------------- 1 | APP_NAME=Lumen 2 | APP_ENV=local 3 | APP_KEY= 4 | APP_DEBUG=true 5 | APP_URL=http://localhost 6 | APP_TIMEZONE=UTC 7 | 8 | LOG_CHANNEL=stack 9 | LOG_SLACK_WEBHOOK_URL= 10 | 11 | DB_CONNECTION=sqlite 12 | #DB_HOST=127.0.0.1 13 | #DB_PORT=3306 14 | #DB_DATABASE=homestead 15 | #DB_USERNAME=homestead 16 | #DB_PASSWORD=secret 17 | 18 | CACHE_DRIVER=file 19 | QUEUE_CONNECTION=sync 20 | 21 | AUTHORS_SERVICE_BASE_URL=localhost:8066 22 | AUTHORS_SERVICE_SECRET= 23 | BOOKS_SERVICE_BASE_URL=localhost:8067 24 | BOOKS_SERVICE_SECRET 25 | -------------------------------------------------------------------------------- /LumenApiGateway/.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.idea 3 | Homestead.json 4 | Homestead.yaml 5 | .env 6 | database/database.sqlite 7 | storage/*.key -------------------------------------------------------------------------------- /LumenApiGateway/app/Console/Commands/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenApiGateway/app/Console/Commands/.gitkeep -------------------------------------------------------------------------------- /LumenApiGateway/app/Console/Kernel.php: -------------------------------------------------------------------------------- 1 | getStatusCode(); 55 | $message = Response::$statusTexts[$code]; 56 | return $this->errorResponse($message, $code); 57 | } 58 | if ($exception instanceof ModelNotFoundException) { 59 | $model = strtolower(class_basename($exception->getModel())); 60 | return $this->errorResponse("Does not exist any instance of {$model} with the given id", Response::HTTP_NOT_FOUND); 61 | } 62 | if ($exception instanceof AuthorizationException) { 63 | return $this->errorResponse($exception->getMessage(), Response::HTTP_FORBIDDEN); 64 | } 65 | if ($exception instanceof AuthenticationException) { 66 | return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED); 67 | } 68 | if ($exception instanceof ValidationException) { 69 | $errors = $exception->validator->errors()->getMessages(); 70 | return $this->errorResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY); 71 | } 72 | if ($exception instanceof ClientException) { 73 | $message = $exception->getResponse()->getBody(); 74 | $code = $exception->getCode(); 75 | 76 | return $this->errorMessage($message, $code); 77 | } 78 | 79 | 80 | if (env('APP_DEBUG', false)) { 81 | return parent::render($request, $exception); 82 | } 83 | return $this->errorResponse('Unexpected error. Try later', Response::HTTP_INTERNAL_SERVER_ERROR); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /LumenApiGateway/app/Http/Controllers/Author/AuthorController.php: -------------------------------------------------------------------------------- 1 | authorService = $authorService; 24 | } 25 | 26 | 27 | /** 28 | * Get Author data 29 | * @return \Illuminate\Http\JsonResponse 30 | */ 31 | public function index() 32 | { 33 | return $this->successResponse($this->authorService->obtainAuthors()); 34 | } 35 | 36 | 37 | /** 38 | * Save an author data 39 | * @param Request $request 40 | * @return \Illuminate\Http\JsonResponse 41 | */ 42 | public function store(Request $request) 43 | { 44 | return $this->successResponse($this->authorService->createAuthor($request->all())); 45 | } 46 | 47 | 48 | /** 49 | * Show a single author details 50 | * @param $author 51 | * @return \Illuminate\Http\JsonResponse 52 | */ 53 | public function show($author) 54 | { 55 | return $this->successResponse($this->authorService->obtainAuthor($author)); 56 | } 57 | 58 | 59 | /** 60 | * Update a single author data 61 | * @param Request $request 62 | * @param $author 63 | * @return \Illuminate\Http\JsonResponse 64 | */ 65 | public function update(Request $request, $author) 66 | { 67 | return $this->successResponse($this->authorService->editAuthor($request->all(),$author)); 68 | } 69 | 70 | 71 | /** 72 | * Delete a single author details 73 | * @param $author 74 | * @return \Illuminate\Http\JsonResponse 75 | */ 76 | public function destroy($author) 77 | { 78 | return $this->successResponse($this->authorService->deleteAuthor($author)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /LumenApiGateway/app/Http/Controllers/Book/BookController.php: -------------------------------------------------------------------------------- 1 | bookService = $bookService; 31 | $this->authorService = $authorService; 32 | } 33 | 34 | 35 | /** 36 | * Get Book data 37 | * @return \Illuminate\Http\JsonResponse 38 | */ 39 | public function index() 40 | { 41 | return $this->successResponse($this->bookService->obtainBooks()); 42 | } 43 | 44 | 45 | /** 46 | * Save an book data 47 | * @param Request $request 48 | * @return \Illuminate\Http\JsonResponse 49 | */ 50 | public function store(Request $request) 51 | { 52 | $this->authorService->obtainAuthor($request->author_id); 53 | return $this->successResponse($this->bookService->createBook($request->all())); 54 | } 55 | 56 | 57 | /** 58 | * Show a single book details 59 | * @param $book 60 | * @return \Illuminate\Http\JsonResponse 61 | */ 62 | public function show($book) 63 | { 64 | return $this->successResponse($this->bookService->obtainBook($book)); 65 | } 66 | 67 | 68 | /** 69 | * Update a single book data 70 | * @param Request $request 71 | * @param $book 72 | * @return \Illuminate\Http\JsonResponse 73 | */ 74 | public function update(Request $request, $book) 75 | { 76 | if(isset($request->author_id)) 77 | { 78 | $this->authorService->obtainAuthor($request->author_id); 79 | } 80 | return $this->successResponse($this->bookService->editBook($request->all(),$book)); 81 | } 82 | 83 | 84 | /** 85 | * Delete a single book details 86 | * @param $book 87 | * @return \Illuminate\Http\JsonResponse 88 | */ 89 | public function destroy($book) 90 | { 91 | return $this->successResponse($this->bookService->deleteBook($book)); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /LumenApiGateway/app/Http/Controllers/Controller.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @param string|null $guard 34 | * @return mixed 35 | */ 36 | public function handle($request, Closure $next, $guard = null) 37 | { 38 | if ($this->auth->guard($guard)->guest()) { 39 | return response('Unauthorized.', 401); 40 | } 41 | 42 | return $next($request); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LumenApiGateway/app/Http/Middleware/ExampleMiddleware.php: -------------------------------------------------------------------------------- 1 | app['auth']->viaRequest('api', function ($request) { 35 | // if ($request->input('api_token')) { 36 | // return User::where('api_token', $request->input('api_token'))->first(); 37 | // } 38 | // }); 39 | LumenPassport::routes($this->app->router); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LumenApiGateway/app/Providers/EventServiceProvider.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'App\Listeners\ExampleListener', 17 | ], 18 | ]; 19 | } 20 | -------------------------------------------------------------------------------- /LumenApiGateway/app/Services/AuthorService.php: -------------------------------------------------------------------------------- 1 | baseUri = config('services.authors.base_uri'); 26 | $this->secret = config('services.authors.secret'); 27 | } 28 | 29 | 30 | /** 31 | * Obtain the full list of author from the author service 32 | */ 33 | public function obtainAuthors() 34 | { 35 | return $this->performRequest('GET', '/authors'); 36 | } 37 | 38 | /** 39 | * Create Author 40 | */ 41 | public function createAuthor($data) 42 | { 43 | return $this->performRequest('POST', '/authors', $data); 44 | } 45 | 46 | /** 47 | * Get a single author data 48 | */ 49 | public function obtainAuthor($author) 50 | { 51 | return $this->performRequest('GET', "/authors/{$author}"); 52 | } 53 | 54 | /** 55 | * Edit a single author data 56 | */ 57 | public function editAuthor($data, $author) 58 | { 59 | return $this->performRequest('PUT', "/authors/{$author}", $data); 60 | } 61 | 62 | /** 63 | * Delete an Author 64 | */ 65 | public function deleteAuthor($author) 66 | { 67 | return $this->performRequest('DELETE', "/authors/{$author}"); 68 | } 69 | } -------------------------------------------------------------------------------- /LumenApiGateway/app/Services/BookService.php: -------------------------------------------------------------------------------- 1 | baseUri = config('services.books.base_uri'); 26 | $this->secret = config('services.books.secret'); 27 | } 28 | 29 | 30 | public function obtainBooks() 31 | { 32 | return $this->performRequest('GET', '/books'); 33 | } 34 | 35 | public function createBook($data) 36 | { 37 | return $this->performRequest('POST', '/books', $data); 38 | } 39 | 40 | public function obtainBook($book) 41 | { 42 | return $this->performRequest('GET', "/books/{$book}"); 43 | } 44 | 45 | public function editBook($data, $book) 46 | { 47 | return $this->performRequest('PUT', "/books/{$book}", $data); 48 | } 49 | 50 | public function deleteBook($book) 51 | { 52 | return $this->performRequest('DELETE', "/books/{$book}"); 53 | } 54 | } -------------------------------------------------------------------------------- /LumenApiGateway/app/Traits/ApiResponser.php: -------------------------------------------------------------------------------- 1 | header('Content-Type', 'application/json'); 16 | } 17 | 18 | public function errorResponse($message, $code) 19 | { 20 | return response()->json(['error' => $message, 'code' => $code], $code); 21 | } 22 | 23 | public function errorMessage($message, $code) 24 | { 25 | return response($message, $code)->header('Content-Type', 'application/json'); 26 | } 27 | } -------------------------------------------------------------------------------- /LumenApiGateway/app/Traits/ConsumeExternalService.php: -------------------------------------------------------------------------------- 1 | $this->baseUri, 21 | ]); 22 | 23 | if(isset($this->secret)) 24 | { 25 | $headers['Authorization'] = $this->secret; 26 | } 27 | 28 | $response = $client->request($method, $requestUrl, [ 29 | 'form_params' => $formParams, 30 | 'headers' => $headers, 31 | ]); 32 | return $response->getBody()->getContents(); 33 | } 34 | } -------------------------------------------------------------------------------- /LumenApiGateway/app/User.php: -------------------------------------------------------------------------------- 1 | make( 32 | 'Illuminate\Contracts\Console\Kernel' 33 | ); 34 | 35 | exit($kernel->handle(new ArgvInput, new ConsoleOutput)); 36 | -------------------------------------------------------------------------------- /LumenApiGateway/bootstrap/app.php: -------------------------------------------------------------------------------- 1 | load(); 7 | } catch (Dotenv\Exception\InvalidPathException $e) { 8 | // 9 | } 10 | 11 | /* 12 | |-------------------------------------------------------------------------- 13 | | Create The Application 14 | |-------------------------------------------------------------------------- 15 | | 16 | | Here we will load the environment and create the application instance 17 | | that serves as the central piece of this framework. We'll use this 18 | | application as an "IoC" container and router for this framework. 19 | | 20 | */ 21 | 22 | $app = new Laravel\Lumen\Application( 23 | dirname(__DIR__) 24 | ); 25 | 26 | $app->withFacades(); 27 | 28 | $app->withEloquent(); 29 | 30 | /** 31 | * Registering custom config files 32 | */ 33 | $app->configure('services'); 34 | $app->configure('auth'); 35 | 36 | /* 37 | |-------------------------------------------------------------------------- 38 | | Register Container Bindings 39 | |-------------------------------------------------------------------------- 40 | | 41 | | Now we will register a few bindings in the service container. We will 42 | | register the exception handler and the console kernel. You may add 43 | | your own bindings here if you like or you can make another file. 44 | | 45 | */ 46 | 47 | $app->singleton( 48 | Illuminate\Contracts\Debug\ExceptionHandler::class, 49 | App\Exceptions\Handler::class 50 | ); 51 | 52 | $app->singleton( 53 | Illuminate\Contracts\Console\Kernel::class, 54 | App\Console\Kernel::class 55 | ); 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Register Middleware 60 | |-------------------------------------------------------------------------- 61 | | 62 | | Next, we will register the middleware with the application. These can 63 | | be global middleware that run before and after each request into a 64 | | route or middleware that'll be assigned to some specific routes. 65 | | 66 | */ 67 | 68 | // $app->middleware([ 69 | // App\Http\Middleware\ExampleMiddleware::class 70 | // ]); 71 | 72 | $app->routeMiddleware([ 73 | // 'auth' => App\Http\Middleware\Authenticate::class, 74 | 'client.credentials' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class, 75 | ]); 76 | 77 | /* 78 | |-------------------------------------------------------------------------- 79 | | Register Service Providers 80 | |-------------------------------------------------------------------------- 81 | | 82 | | Here we will register all of the application's service providers which 83 | | are used to bind services into the container. Service providers are 84 | | totally optional, so you are not required to uncomment this line. 85 | | 86 | */ 87 | 88 | // $app->register(App\Providers\AppServiceProvider::class); 89 | // $app->register(App\Providers\EventServiceProvider::class); 90 | $app->register(App\Providers\AuthServiceProvider::class); 91 | $app->register(\Laravel\Passport\PassportServiceProvider::class); 92 | $app->register(\Dusterio\LumenPassport\PassportServiceProvider::class); 93 | 94 | /* 95 | |-------------------------------------------------------------------------- 96 | | Load The Application Routes 97 | |-------------------------------------------------------------------------- 98 | | 99 | | Next we will include the routes file so that they can all be added to 100 | | the application. This will provide all of the URLs the application 101 | | can respond to, as well as the controllers that may handle them. 102 | | 103 | */ 104 | 105 | $app->router->group([ 106 | 'namespace' => 'App\Http\Controllers', 107 | ], function ($router) { 108 | require __DIR__.'/../routes/web.php'; 109 | }); 110 | 111 | return $app; 112 | -------------------------------------------------------------------------------- /LumenApiGateway/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel/lumen", 3 | "description": "The Laravel Lumen Framework.", 4 | "keywords": ["framework", "laravel", "lumen"], 5 | "license": "MIT", 6 | "type": "project", 7 | "require": { 8 | "php": ">=7.1.3", 9 | "dusterio/lumen-passport": "^0.2.9", 10 | "guzzlehttp/guzzle": "^6.3", 11 | "laravel/lumen-framework": "5.7.*", 12 | "vlucas/phpdotenv": "~2.2" 13 | }, 14 | "require-dev": { 15 | "fzaninotto/faker": "~1.4", 16 | "phpunit/phpunit": "~7.0", 17 | "mockery/mockery": "~1.0" 18 | }, 19 | "autoload": { 20 | "classmap": [ 21 | "database/seeds", 22 | "database/factories" 23 | ], 24 | "psr-4": { 25 | "App\\": "app/" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "classmap": [ 30 | "tests/" 31 | ] 32 | }, 33 | "scripts": { 34 | "post-root-package-install": [ 35 | "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" 36 | ] 37 | }, 38 | "config": { 39 | "preferred-install": "dist", 40 | "sort-packages": true, 41 | "optimize-autoloader": true 42 | }, 43 | "minimum-stability": "dev", 44 | "prefer-stable": true 45 | } 46 | -------------------------------------------------------------------------------- /LumenApiGateway/config/auth.php: -------------------------------------------------------------------------------- 1 | [ 17 | 'guard' => env('AUTH_GUARD', 'api'), 18 | ], 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Authentication Guards 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Next, you may define every authentication guard for your application. 26 | | Of course, a great default configuration has been defined for you 27 | | here which uses session storage and the Eloquent user provider. 28 | | 29 | | All authentication drivers have a user provider. This defines how the 30 | | users are actually retrieved out of your database or other storage 31 | | mechanisms used by this application to persist your user's data. 32 | | 33 | | Supported: "token" 34 | | 35 | */ 36 | 37 | 'guards' => [ 38 | 'api' => ['driver' => 'passport'], 39 | ], 40 | 41 | /* 42 | |-------------------------------------------------------------------------- 43 | | User Providers 44 | |-------------------------------------------------------------------------- 45 | | 46 | | All authentication drivers have a user provider. This defines how the 47 | | users are actually retrieved out of your database or other storage 48 | | mechanisms used by this application to persist your user's data. 49 | | 50 | | If you have multiple user tables or models you may configure multiple 51 | | sources which represent each model / table. These sources may then 52 | | be assigned to any extra authentication guards you have defined. 53 | | 54 | | Supported: "database", "eloquent" 55 | | 56 | */ 57 | 58 | 'providers' => [ 59 | // 60 | ], 61 | 62 | /* 63 | |-------------------------------------------------------------------------- 64 | | Resetting Passwords 65 | |-------------------------------------------------------------------------- 66 | | 67 | | Here you may set the options for resetting passwords including the view 68 | | that is your password reset e-mail. You may also set the name of the 69 | | table that maintains all of the reset tokens for your application. 70 | | 71 | | You may specify multiple password reset configurations if you have more 72 | | than one user table or model in the application and you want to have 73 | | separate password reset settings based on the specific user types. 74 | | 75 | | The expire time is the number of minutes that the reset token should be 76 | | considered valid. This security feature keeps tokens short-lived so 77 | | they have less time to be guessed. You may change this as needed. 78 | | 79 | */ 80 | 81 | 'passwords' => [ 82 | // 83 | ], 84 | 85 | ]; 86 | -------------------------------------------------------------------------------- /LumenApiGateway/config/services.php: -------------------------------------------------------------------------------- 1 | [ 5 | 'base_uri' => env('AUTHORS_SERVICE_BASE_URL'), 6 | 'secret' => env('AUTHORS_SERVICE_SECRET'), 7 | ], 8 | 9 | 'books' => [ 10 | 'base_uri' => env('BOOKS_SERVICE_BASE_URL'), 11 | 'secret' => env('BOOKS_SERVICE_SECRET'), 12 | ], 13 | ]; -------------------------------------------------------------------------------- /LumenApiGateway/database/factories/ModelFactory.php: -------------------------------------------------------------------------------- 1 | define(App\User::class, function (Faker\Generator $faker) { 15 | return [ 16 | 'name' => $faker->name, 17 | 'email' => $faker->email, 18 | ]; 19 | }); 20 | -------------------------------------------------------------------------------- /LumenApiGateway/database/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenApiGateway/database/migrations/.gitkeep -------------------------------------------------------------------------------- /LumenApiGateway/database/seeds/DatabaseSeeder.php: -------------------------------------------------------------------------------- 1 | call('UsersTableSeeder'); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LumenApiGateway/phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests 14 | 15 | 16 | 17 | 18 | ./app 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /LumenApiGateway/public/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | 3 | Options -MultiViews -Indexes 4 | 5 | 6 | RewriteEngine On 7 | 8 | # Handle Authorization Header 9 | RewriteCond %{HTTP:Authorization} . 10 | RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 11 | 12 | # Redirect Trailing Slashes If Not A Folder... 13 | RewriteCond %{REQUEST_FILENAME} !-d 14 | RewriteCond %{REQUEST_URI} (.+)/$ 15 | RewriteRule ^ %1 [L,R=301] 16 | 17 | # Handle Front Controller... 18 | RewriteCond %{REQUEST_FILENAME} !-d 19 | RewriteCond %{REQUEST_FILENAME} !-f 20 | RewriteRule ^ index.php [L] 21 | 22 | -------------------------------------------------------------------------------- /LumenApiGateway/public/index.php: -------------------------------------------------------------------------------- 1 | run(); 29 | -------------------------------------------------------------------------------- /LumenApiGateway/readme.md: -------------------------------------------------------------------------------- 1 | # Lumen PHP Framework 2 | 3 | [![Build Status](https://travis-ci.org/laravel/lumen-framework.svg)](https://travis-ci.org/laravel/lumen-framework) 4 | [![Total Downloads](https://poser.pugx.org/laravel/lumen-framework/d/total.svg)](https://packagist.org/packages/laravel/lumen-framework) 5 | [![Latest Stable Version](https://poser.pugx.org/laravel/lumen-framework/v/stable.svg)](https://packagist.org/packages/laravel/lumen-framework) 6 | [![Latest Unstable Version](https://poser.pugx.org/laravel/lumen-framework/v/unstable.svg)](https://packagist.org/packages/laravel/lumen-framework) 7 | [![License](https://poser.pugx.org/laravel/lumen-framework/license.svg)](https://packagist.org/packages/laravel/lumen-framework) 8 | 9 | Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching. 10 | 11 | ## Official Documentation 12 | 13 | Documentation for the framework can be found on the [Lumen website](https://lumen.laravel.com/docs). 14 | 15 | ## Security Vulnerabilities 16 | 17 | If you discover a security vulnerability within Lumen, please send an e-mail to Taylor Otwell at taylor@laravel.com. All security vulnerabilities will be promptly addressed. 18 | 19 | ## License 20 | 21 | The Lumen framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). 22 | -------------------------------------------------------------------------------- /LumenApiGateway/resources/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenApiGateway/resources/views/.gitkeep -------------------------------------------------------------------------------- /LumenApiGateway/routes/web.php: -------------------------------------------------------------------------------- 1 | group(['middleware' => 'client.credentials'], function() use ($router){ 15 | 16 | $router->get('/authors', 'Author\AuthorController@index'); 17 | $router->post('/authors', 'Author\AuthorController@store'); 18 | $router->get('/authors/{author}', 'Author\AuthorController@show'); 19 | $router->put('/authors/{author}', 'Author\AuthorController@update'); 20 | $router->patch('/authors/{author}', 'Author\AuthorController@update'); 21 | $router->delete('/authors/{author}', 'Author\AuthorController@destroy'); 22 | 23 | $router->get('/books', 'Book\BookController@index'); 24 | $router->post('/books', 'Book\BookController@store'); 25 | $router->get('/books/{book}', 'Book\BookController@show'); 26 | $router->put('/books/{book}', 'Book\BookController@update'); 27 | $router->patch('/books/{book}', 'Book\BookController@update'); 28 | $router->delete('/books/{book}', 'Book\BookController@destroy'); 29 | 30 | }); 31 | 32 | 33 | -------------------------------------------------------------------------------- /LumenApiGateway/storage/app/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenApiGateway/storage/framework/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenApiGateway/storage/framework/views/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenApiGateway/storage/logs/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenApiGateway/tests/ExampleTest.php: -------------------------------------------------------------------------------- 1 | get('/'); 16 | 17 | $this->assertEquals( 18 | $this->app->version(), $this->response->getContent() 19 | ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LumenApiGateway/tests/TestCase.php: -------------------------------------------------------------------------------- 1 | getStatusCode(); 54 | $message = Response::$statusTexts[$code]; 55 | return $this->errorResponse($message, $code); 56 | } 57 | 58 | // If Model Not found (e.g: not existing user error) 59 | if ($exception instanceof ModelNotFoundException) { 60 | $model = strtolower(class_basename($exception->getModel())); 61 | return $this->errorResponse("Does not exist any instance of {$model} with the given id", Response::HTTP_NOT_FOUND); 62 | } 63 | 64 | // Handling the Unauthorized exception 65 | if ($exception instanceof AuthorizationException) { 66 | return $this->errorResponse($exception->getMessage(), Response::HTTP_FORBIDDEN); 67 | } 68 | 69 | if ($exception instanceof AuthenticationException) { 70 | return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED); 71 | } 72 | 73 | if ($exception instanceof ValidationException) { 74 | $errors = $exception->validator->errors()->getMessages(); 75 | return $this->errorResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY); 76 | } 77 | 78 | if(env('APP_DEBUG', false)) 79 | { 80 | return parent::render($request, $exception); 81 | } 82 | 83 | return $this->errorResponse('Unexpected error. Try later', Response::HTTP_INTERNAL_SERVER_ERROR); 84 | 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /LumenAuthorApi/app/Http/Controllers/Author/AuthorController.php: -------------------------------------------------------------------------------- 1 | successResponse($authors); 32 | } 33 | 34 | 35 | /** 36 | * Create one new authors 37 | * @param Request $request 38 | * @return Response 39 | */ 40 | public function store(Request $request) 41 | { 42 | $rules = [ 43 | 'name' => 'required|max:254', 44 | 'gender' => 'required|max:20|in:male,female', 45 | 'country' => 'required|max:254', 46 | ]; 47 | 48 | $this->validate($request, $rules); 49 | 50 | $author = Author::create($request->all()); 51 | 52 | return $this->successResponse($author, Response::HTTP_CREATED); 53 | } 54 | 55 | 56 | /** 57 | * Show a specific author 58 | * @param Author $author 59 | * @return Response 60 | */ 61 | public function show($author) 62 | { 63 | $author = Author::findOrFail($author); 64 | return $this->successResponse($author); 65 | } 66 | 67 | 68 | /** 69 | * Update author information 70 | * @param Request $request 71 | * @param $author 72 | * @return \Illuminate\Http\JsonResponse 73 | */ 74 | public function update(Request $request, $author) 75 | { 76 | $rules = [ 77 | 'name' => 'max:254', 78 | 'gender' => 'max:20|in:male,female', 79 | 'country' => 'max:254', 80 | ]; 81 | $this->validate($request, $rules); 82 | $author = Author::findOrFail($author); 83 | $author->fill($request->all()); 84 | if($author->isClean()){ 85 | return $this->errorResponse("Atleast one value must change", Response::HTTP_UNPROCESSABLE_ENTITY); 86 | } 87 | $author->save(); 88 | return $this->successResponse($author); 89 | } 90 | 91 | 92 | /** 93 | * Delete author information 94 | * @param $author 95 | * @return \Illuminate\Http\JsonResponse 96 | */ 97 | public function destroy($author) 98 | { 99 | $author = Author::findOrFail($author); 100 | $author->delete(); 101 | return $this->successResponse($author); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /LumenAuthorApi/app/Http/Controllers/Controller.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @param string|null $guard 34 | * @return mixed 35 | */ 36 | public function handle($request, Closure $next, $guard = null) 37 | { 38 | if ($this->auth->guard($guard)->guest()) { 39 | return response('Unauthorized.', 401); 40 | } 41 | 42 | return $next($request); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LumenAuthorApi/app/Http/Middleware/AuthenticateAccessMiddleware.php: -------------------------------------------------------------------------------- 1 | header('Authorization'), $validSecrets)) 21 | { 22 | return $next($request); 23 | } 24 | 25 | abort(Response::HTTP_UNAUTHORIZED); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LumenAuthorApi/app/Http/Middleware/ExampleMiddleware.php: -------------------------------------------------------------------------------- 1 | app['auth']->viaRequest('api', function ($request) { 34 | if ($request->input('api_token')) { 35 | return User::where('api_token', $request->input('api_token'))->first(); 36 | } 37 | }); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LumenAuthorApi/app/Providers/EventServiceProvider.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'App\Listeners\ExampleListener', 17 | ], 18 | ]; 19 | } 20 | -------------------------------------------------------------------------------- /LumenAuthorApi/app/Traits/ApiResponser.php: -------------------------------------------------------------------------------- 1 | json(['data' => $data], $code); 18 | } 19 | 20 | 21 | public function errorResponse($message, $code) 22 | { 23 | return \response()->json(['error' => $message, 'code' => $code], $code); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /LumenAuthorApi/artisan: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | make( 32 | 'Illuminate\Contracts\Console\Kernel' 33 | ); 34 | 35 | exit($kernel->handle(new ArgvInput, new ConsoleOutput)); 36 | -------------------------------------------------------------------------------- /LumenAuthorApi/bootstrap/app.php: -------------------------------------------------------------------------------- 1 | load(); 7 | } catch (Dotenv\Exception\InvalidPathException $e) { 8 | // 9 | } 10 | 11 | /* 12 | |-------------------------------------------------------------------------- 13 | | Create The Application 14 | |-------------------------------------------------------------------------- 15 | | 16 | | Here we will load the environment and create the application instance 17 | | that serves as the central piece of this framework. We'll use this 18 | | application as an "IoC" container and router for this framework. 19 | | 20 | */ 21 | 22 | $app = new Laravel\Lumen\Application( 23 | dirname(__DIR__) 24 | ); 25 | $app->withFacades(); 26 | 27 | $app->withEloquent(); 28 | 29 | /* 30 | |-------------------------------------------------------------------------- 31 | | Register Container Bindings 32 | |-------------------------------------------------------------------------- 33 | | 34 | | Now we will register a few bindings in the service container. We will 35 | | register the exception handler and the console kernel. You may add 36 | | your own bindings here if you like or you can make another file. 37 | | 38 | */ 39 | 40 | $app->singleton( 41 | Illuminate\Contracts\Debug\ExceptionHandler::class, 42 | App\Exceptions\Handler::class 43 | ); 44 | 45 | $app->singleton( 46 | Illuminate\Contracts\Console\Kernel::class, 47 | App\Console\Kernel::class 48 | ); 49 | 50 | /* 51 | |-------------------------------------------------------------------------- 52 | | Register Middleware 53 | |-------------------------------------------------------------------------- 54 | | 55 | | Next, we will register the middleware with the application. These can 56 | | be global middleware that run before and after each request into a 57 | | route or middleware that'll be assigned to some specific routes. 58 | | 59 | */ 60 | 61 | $app->middleware([ 62 | App\Http\Middleware\AuthenticateAccessMiddleware::class 63 | ]); 64 | 65 | // $app->routeMiddleware([ 66 | // 'auth' => App\Http\Middleware\Authenticate::class, 67 | // ]); 68 | 69 | /* 70 | |-------------------------------------------------------------------------- 71 | | Register Service Providers 72 | |-------------------------------------------------------------------------- 73 | | 74 | | Here we will register all of the application's service providers which 75 | | are used to bind services into the container. Service providers are 76 | | totally optional, so you are not required to uncomment this line. 77 | | 78 | */ 79 | 80 | // $app->register(App\Providers\AppServiceProvider::class); 81 | // $app->register(App\Providers\AuthServiceProvider::class); 82 | // $app->register(App\Providers\EventServiceProvider::class); 83 | 84 | /* 85 | |-------------------------------------------------------------------------- 86 | | Load The Application Routes 87 | |-------------------------------------------------------------------------- 88 | | 89 | | Next we will include the routes file so that they can all be added to 90 | | the application. This will provide all of the URLs the application 91 | | can respond to, as well as the controllers that may handle them. 92 | | 93 | */ 94 | 95 | $app->router->group([ 96 | 'namespace' => 'App\Http\Controllers', 97 | ], function ($router) { 98 | require __DIR__.'/../routes/web.php'; 99 | }); 100 | 101 | return $app; 102 | -------------------------------------------------------------------------------- /LumenAuthorApi/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel/lumen", 3 | "description": "The Laravel Lumen Framework.", 4 | "keywords": ["framework", "laravel", "lumen"], 5 | "license": "MIT", 6 | "type": "project", 7 | "require": { 8 | "php": ">=7.1.3", 9 | "laravel/lumen-framework": "5.7.*", 10 | "vlucas/phpdotenv": "~2.2" 11 | }, 12 | "require-dev": { 13 | "fzaninotto/faker": "~1.4", 14 | "phpunit/phpunit": "~7.0", 15 | "mockery/mockery": "~1.0" 16 | }, 17 | "autoload": { 18 | "classmap": [ 19 | "database/seeds", 20 | "database/factories" 21 | ], 22 | "psr-4": { 23 | "App\\": "app/" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "classmap": [ 28 | "tests/" 29 | ] 30 | }, 31 | "scripts": { 32 | "post-root-package-install": [ 33 | "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" 34 | ] 35 | }, 36 | "config": { 37 | "preferred-install": "dist", 38 | "sort-packages": true, 39 | "optimize-autoloader": true 40 | }, 41 | "minimum-stability": "dev", 42 | "prefer-stable": true 43 | } 44 | -------------------------------------------------------------------------------- /LumenAuthorApi/database/database.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenAuthorApi/database/database.sqlite -------------------------------------------------------------------------------- /LumenAuthorApi/database/factories/ModelFactory.php: -------------------------------------------------------------------------------- 1 | define(App\Models\Author::class, function (Faker\Generator $faker) { 15 | return [ 16 | 'gender' => $gender = $faker->randomElement(['male', 'female']), 17 | 'name' => $faker->name ($gender), 18 | 'country' => $faker->country, 19 | ]; 20 | }); 21 | -------------------------------------------------------------------------------- /LumenAuthorApi/database/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenAuthorApi/database/migrations/.gitkeep -------------------------------------------------------------------------------- /LumenAuthorApi/database/migrations/2019_01_01_162530_create_authors_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('name'); 19 | $table->string('gender', 20); 20 | $table->string('country'); 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::dropIfExists('authors'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LumenAuthorApi/database/seeds/DatabaseSeeder.php: -------------------------------------------------------------------------------- 1 | call('UsersTableSeeder'); 15 | factory(\App\Models\Author::class, 50)->create(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LumenAuthorApi/phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests 14 | 15 | 16 | 17 | 18 | ./app 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /LumenAuthorApi/public/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | 3 | Options -MultiViews -Indexes 4 | 5 | 6 | RewriteEngine On 7 | 8 | # Handle Authorization Header 9 | RewriteCond %{HTTP:Authorization} . 10 | RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 11 | 12 | # Redirect Trailing Slashes If Not A Folder... 13 | RewriteCond %{REQUEST_FILENAME} !-d 14 | RewriteCond %{REQUEST_URI} (.+)/$ 15 | RewriteRule ^ %1 [L,R=301] 16 | 17 | # Handle Front Controller... 18 | RewriteCond %{REQUEST_FILENAME} !-d 19 | RewriteCond %{REQUEST_FILENAME} !-f 20 | RewriteRule ^ index.php [L] 21 | 22 | -------------------------------------------------------------------------------- /LumenAuthorApi/public/index.php: -------------------------------------------------------------------------------- 1 | run(); 29 | -------------------------------------------------------------------------------- /LumenAuthorApi/readme.md: -------------------------------------------------------------------------------- 1 | # Lumen PHP Framework 2 | 3 | [![Build Status](https://travis-ci.org/laravel/lumen-framework.svg)](https://travis-ci.org/laravel/lumen-framework) 4 | [![Total Downloads](https://poser.pugx.org/laravel/lumen-framework/d/total.svg)](https://packagist.org/packages/laravel/lumen-framework) 5 | [![Latest Stable Version](https://poser.pugx.org/laravel/lumen-framework/v/stable.svg)](https://packagist.org/packages/laravel/lumen-framework) 6 | [![Latest Unstable Version](https://poser.pugx.org/laravel/lumen-framework/v/unstable.svg)](https://packagist.org/packages/laravel/lumen-framework) 7 | [![License](https://poser.pugx.org/laravel/lumen-framework/license.svg)](https://packagist.org/packages/laravel/lumen-framework) 8 | 9 | Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching. 10 | 11 | ## Official Documentation 12 | 13 | Documentation for the framework can be found on the [Lumen website](https://lumen.laravel.com/docs). 14 | 15 | ## Security Vulnerabilities 16 | 17 | If you discover a security vulnerability within Lumen, please send an e-mail to Taylor Otwell at taylor@laravel.com. All security vulnerabilities will be promptly addressed. 18 | 19 | ## License 20 | 21 | The Lumen framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). 22 | -------------------------------------------------------------------------------- /LumenAuthorApi/resources/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenAuthorApi/resources/views/.gitkeep -------------------------------------------------------------------------------- /LumenAuthorApi/routes/web.php: -------------------------------------------------------------------------------- 1 | get('/authors', 'Author\AuthorController@index'); 15 | $router->post('/authors', 'Author\AuthorController@store'); 16 | $router->get('/authors/{author}', 'Author\AuthorController@show'); 17 | $router->put('/authors/{author}', 'Author\AuthorController@update'); 18 | $router->patch('/authors/{author}', 'Author\AuthorController@update'); 19 | $router->delete('/authors/{author}', 'Author\AuthorController@destroy'); 20 | -------------------------------------------------------------------------------- /LumenAuthorApi/storage/app/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenAuthorApi/storage/framework/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenAuthorApi/storage/framework/views/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenAuthorApi/storage/logs/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenAuthorApi/tests/ExampleTest.php: -------------------------------------------------------------------------------- 1 | get('/'); 16 | 17 | $this->assertEquals( 18 | $this->app->version(), $this->response->getContent() 19 | ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LumenAuthorApi/tests/TestCase.php: -------------------------------------------------------------------------------- 1 | getStatusCode(); 54 | $message = Response::$statusTexts[$code]; 55 | return $this->errorResponse($message, $code); 56 | } 57 | if ($exception instanceof ModelNotFoundException) { 58 | $model = strtolower(class_basename($exception->getModel())); 59 | return $this->errorResponse("Does not exist any instance of {$model} with the given id", Response::HTTP_NOT_FOUND); 60 | } 61 | if ($exception instanceof AuthorizationException) { 62 | return $this->errorResponse($exception->getMessage(), Response::HTTP_FORBIDDEN); 63 | } 64 | if ($exception instanceof AuthenticationException) { 65 | return $this->errorResponse($exception->getMessage(), Response::HTTP_UNAUTHORIZED); 66 | } 67 | if ($exception instanceof ValidationException) { 68 | $errors = $exception->validator->errors()->getMessages(); 69 | return $this->errorResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY); 70 | } 71 | if (env('APP_DEBUG', false)) { 72 | return parent::render($request, $exception); 73 | } 74 | return $this->errorResponse('Unexpected error. Try later', Response::HTTP_INTERNAL_SERVER_ERROR); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /LumenBookApi/app/Http/Controllers/Book/BookController.php: -------------------------------------------------------------------------------- 1 | successResponse($books); 32 | } 33 | 34 | 35 | /** 36 | * Store a single book information 37 | * @param Request $request 38 | * @return \Illuminate\Http\JsonResponse 39 | */ 40 | public function store(Request $request) 41 | { 42 | $rules = [ 43 | 'title' => 'required|max:255', 44 | 'description' => 'required', 45 | 'price' => 'required|min:1', 46 | 'author_id' => 'required|min:1', 47 | ]; 48 | $this->validate($request, $rules); 49 | 50 | $book = Book::create($request->all()); 51 | 52 | return $this->successResponse($book, Response::HTTP_CREATED); 53 | } 54 | 55 | 56 | /** 57 | * Storing a single book data 58 | * @param $book 59 | * @return \Illuminate\Http\JsonResponse 60 | */ 61 | public function show($book) 62 | { 63 | $book = Book::findOrFail($book); 64 | return $this->successResponse($book); 65 | } 66 | 67 | 68 | /** 69 | * @param Request $request 70 | * @param $book 71 | * @return \Illuminate\Http\JsonResponse 72 | */ 73 | public function update(Request $request, $book) 74 | { 75 | $rules = [ 76 | 'title' => 'max:255', 77 | 'description' => 'min:1', 78 | 'price' => 'min:1', 79 | 'author_id' => 'min:1', 80 | ]; 81 | $this->validate($request, $rules); 82 | $book = Book::findOrFail($book); 83 | $book->fill($request->all()); 84 | if($book->isClean()){ 85 | return $this->errorResponse("At least one value must change", Response::HTTP_UNPROCESSABLE_ENTITY); 86 | } 87 | $book->save(); 88 | return $this->successResponse($book); 89 | } 90 | 91 | 92 | /** 93 | * Delete book information 94 | * @param $book 95 | * @return \Illuminate\Http\JsonResponse 96 | */ 97 | public function destroy($book) 98 | { 99 | $book = Book::findOrFail($book); 100 | $book->delete(); 101 | return $this->successResponse($book); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /LumenBookApi/app/Http/Controllers/Controller.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @param string|null $guard 34 | * @return mixed 35 | */ 36 | public function handle($request, Closure $next, $guard = null) 37 | { 38 | if ($this->auth->guard($guard)->guest()) { 39 | return response('Unauthorized.', 401); 40 | } 41 | 42 | return $next($request); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LumenBookApi/app/Http/Middleware/AuthenticateAccessMiddleware.php: -------------------------------------------------------------------------------- 1 | header('Authorization'), $validSecrets)) 22 | { 23 | return $next($request); 24 | } 25 | 26 | abort(Response::HTTP_UNAUTHORIZED); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LumenBookApi/app/Http/Middleware/ExampleMiddleware.php: -------------------------------------------------------------------------------- 1 | app['auth']->viaRequest('api', function ($request) { 34 | if ($request->input('api_token')) { 35 | return User::where('api_token', $request->input('api_token'))->first(); 36 | } 37 | }); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LumenBookApi/app/Providers/EventServiceProvider.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'App\Listeners\ExampleListener', 17 | ], 18 | ]; 19 | } 20 | -------------------------------------------------------------------------------- /LumenBookApi/app/Traits/ApiResponser.php: -------------------------------------------------------------------------------- 1 | json(['data' => $data], $code); 18 | } 19 | 20 | 21 | public function errorResponse($message, $code) 22 | { 23 | return \response()->json(['error' => $message, 'code' => $code], $code); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /LumenBookApi/artisan: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | make( 32 | 'Illuminate\Contracts\Console\Kernel' 33 | ); 34 | 35 | exit($kernel->handle(new ArgvInput, new ConsoleOutput)); 36 | -------------------------------------------------------------------------------- /LumenBookApi/bootstrap/app.php: -------------------------------------------------------------------------------- 1 | load(); 7 | } catch (Dotenv\Exception\InvalidPathException $e) { 8 | // 9 | } 10 | 11 | /* 12 | |-------------------------------------------------------------------------- 13 | | Create The Application 14 | |-------------------------------------------------------------------------- 15 | | 16 | | Here we will load the environment and create the application instance 17 | | that serves as the central piece of this framework. We'll use this 18 | | application as an "IoC" container and router for this framework. 19 | | 20 | */ 21 | 22 | $app = new Laravel\Lumen\Application( 23 | dirname(__DIR__) 24 | ); 25 | 26 | $app->withFacades(); 27 | 28 | $app->withEloquent(); 29 | 30 | /* 31 | |-------------------------------------------------------------------------- 32 | | Register Container Bindings 33 | |-------------------------------------------------------------------------- 34 | | 35 | | Now we will register a few bindings in the service container. We will 36 | | register the exception handler and the console kernel. You may add 37 | | your own bindings here if you like or you can make another file. 38 | | 39 | */ 40 | 41 | $app->singleton( 42 | Illuminate\Contracts\Debug\ExceptionHandler::class, 43 | App\Exceptions\Handler::class 44 | ); 45 | 46 | $app->singleton( 47 | Illuminate\Contracts\Console\Kernel::class, 48 | App\Console\Kernel::class 49 | ); 50 | 51 | /* 52 | |-------------------------------------------------------------------------- 53 | | Register Middleware 54 | |-------------------------------------------------------------------------- 55 | | 56 | | Next, we will register the middleware with the application. These can 57 | | be global middleware that run before and after each request into a 58 | | route or middleware that'll be assigned to some specific routes. 59 | | 60 | */ 61 | 62 | $app->middleware([ 63 | App\Http\Middleware\AuthenticateAccessMiddleware::class 64 | ]); 65 | 66 | // $app->routeMiddleware([ 67 | // 'auth' => App\Http\Middleware\Authenticate::class, 68 | // ]); 69 | 70 | /* 71 | |-------------------------------------------------------------------------- 72 | | Register Service Providers 73 | |-------------------------------------------------------------------------- 74 | | 75 | | Here we will register all of the application's service providers which 76 | | are used to bind services into the container. Service providers are 77 | | totally optional, so you are not required to uncomment this line. 78 | | 79 | */ 80 | 81 | // $app->register(App\Providers\AppServiceProvider::class); 82 | // $app->register(App\Providers\AuthServiceProvider::class); 83 | // $app->register(App\Providers\EventServiceProvider::class); 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | Load The Application Routes 88 | |-------------------------------------------------------------------------- 89 | | 90 | | Next we will include the routes file so that they can all be added to 91 | | the application. This will provide all of the URLs the application 92 | | can respond to, as well as the controllers that may handle them. 93 | | 94 | */ 95 | 96 | $app->router->group([ 97 | 'namespace' => 'App\Http\Controllers', 98 | ], function ($router) { 99 | require __DIR__.'/../routes/web.php'; 100 | }); 101 | 102 | return $app; 103 | -------------------------------------------------------------------------------- /LumenBookApi/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel/lumen", 3 | "description": "The Laravel Lumen Framework.", 4 | "keywords": ["framework", "laravel", "lumen"], 5 | "license": "MIT", 6 | "type": "project", 7 | "require": { 8 | "php": ">=7.1.3", 9 | "laravel/lumen-framework": "5.7.*", 10 | "vlucas/phpdotenv": "~2.2" 11 | }, 12 | "require-dev": { 13 | "fzaninotto/faker": "~1.4", 14 | "phpunit/phpunit": "~7.0", 15 | "mockery/mockery": "~1.0" 16 | }, 17 | "autoload": { 18 | "classmap": [ 19 | "database/seeds", 20 | "database/factories" 21 | ], 22 | "psr-4": { 23 | "App\\": "app/" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "classmap": [ 28 | "tests/" 29 | ] 30 | }, 31 | "scripts": { 32 | "post-root-package-install": [ 33 | "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" 34 | ] 35 | }, 36 | "config": { 37 | "preferred-install": "dist", 38 | "sort-packages": true, 39 | "optimize-autoloader": true 40 | }, 41 | "minimum-stability": "dev", 42 | "prefer-stable": true 43 | } 44 | -------------------------------------------------------------------------------- /LumenBookApi/database/database.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenBookApi/database/database.sqlite -------------------------------------------------------------------------------- /LumenBookApi/database/factories/ModelFactory.php: -------------------------------------------------------------------------------- 1 | define(App\Models\Book::class, function (Faker\Generator $faker) { 15 | return [ 16 | 'title' => $faker->sentence(3, true), 17 | 'description' => $faker->sentence(8, true), 18 | 'price' => $faker->numberBetween(25, 150), 19 | 'author_id' => $faker->numberBetween(1, 50), 20 | ]; 21 | }); 22 | -------------------------------------------------------------------------------- /LumenBookApi/database/migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenBookApi/database/migrations/.gitkeep -------------------------------------------------------------------------------- /LumenBookApi/database/migrations/2019_01_02_202525_create_books_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('title'); 19 | $table->text('description'); 20 | $table->float('price')->unsigned(); 21 | $table->integer('author_id')->unsigned(); 22 | $table->timestamps(); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('books'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LumenBookApi/database/seeds/DatabaseSeeder.php: -------------------------------------------------------------------------------- 1 | call('UsersTableSeeder'); 15 | factory(\App\Models\Book::class, 150)->create(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LumenBookApi/phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests 14 | 15 | 16 | 17 | 18 | ./app 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /LumenBookApi/public/.htaccess: -------------------------------------------------------------------------------- 1 | 2 | 3 | Options -MultiViews -Indexes 4 | 5 | 6 | RewriteEngine On 7 | 8 | # Handle Authorization Header 9 | RewriteCond %{HTTP:Authorization} . 10 | RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 11 | 12 | # Redirect Trailing Slashes If Not A Folder... 13 | RewriteCond %{REQUEST_FILENAME} !-d 14 | RewriteCond %{REQUEST_URI} (.+)/$ 15 | RewriteRule ^ %1 [L,R=301] 16 | 17 | # Handle Front Controller... 18 | RewriteCond %{REQUEST_FILENAME} !-d 19 | RewriteCond %{REQUEST_FILENAME} !-f 20 | RewriteRule ^ index.php [L] 21 | 22 | -------------------------------------------------------------------------------- /LumenBookApi/public/index.php: -------------------------------------------------------------------------------- 1 | run(); 29 | -------------------------------------------------------------------------------- /LumenBookApi/readme.md: -------------------------------------------------------------------------------- 1 | # Lumen PHP Framework 2 | 3 | [![Build Status](https://travis-ci.org/laravel/lumen-framework.svg)](https://travis-ci.org/laravel/lumen-framework) 4 | [![Total Downloads](https://poser.pugx.org/laravel/lumen-framework/d/total.svg)](https://packagist.org/packages/laravel/lumen-framework) 5 | [![Latest Stable Version](https://poser.pugx.org/laravel/lumen-framework/v/stable.svg)](https://packagist.org/packages/laravel/lumen-framework) 6 | [![Latest Unstable Version](https://poser.pugx.org/laravel/lumen-framework/v/unstable.svg)](https://packagist.org/packages/laravel/lumen-framework) 7 | [![License](https://poser.pugx.org/laravel/lumen-framework/license.svg)](https://packagist.org/packages/laravel/lumen-framework) 8 | 9 | Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching. 10 | 11 | ## Official Documentation 12 | 13 | Documentation for the framework can be found on the [Lumen website](https://lumen.laravel.com/docs). 14 | 15 | ## Security Vulnerabilities 16 | 17 | If you discover a security vulnerability within Lumen, please send an e-mail to Taylor Otwell at taylor@laravel.com. All security vulnerabilities will be promptly addressed. 18 | 19 | ## License 20 | 21 | The Lumen framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). 22 | -------------------------------------------------------------------------------- /LumenBookApi/resources/views/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/code-architect/Microservices-with-Lumen/4672a676210891ef9a3741c978a2475b89a17ac0/LumenBookApi/resources/views/.gitkeep -------------------------------------------------------------------------------- /LumenBookApi/routes/web.php: -------------------------------------------------------------------------------- 1 | get('/books', 'Book\BookController@index'); 15 | $router->post('/books', 'Book\BookController@store'); 16 | $router->get('/books/{book}', 'Book\BookController@show'); 17 | $router->put('/books/{book}', 'Book\BookController@update'); 18 | $router->patch('/books/{book}', 'Book\BookController@update'); 19 | $router->delete('/books/{book}', 'Book\BookController@destroy'); 20 | -------------------------------------------------------------------------------- /LumenBookApi/storage/app/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenBookApi/storage/framework/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenBookApi/storage/framework/views/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenBookApi/storage/logs/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /LumenBookApi/tests/ExampleTest.php: -------------------------------------------------------------------------------- 1 | get('/'); 16 | 17 | $this->assertEquals( 18 | $this->app->version(), $this->response->getContent() 19 | ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LumenBookApi/tests/TestCase.php: -------------------------------------------------------------------------------- 1 |