We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Add a sample middleware to startproject's template
It will be nice to have a middleware template inside the template project to serve as an example for people that want to use it.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scrapy/commands/startproject.py`
Content:
```
1 from __future__ import print_function
2 import re
3 import os
4 import string
5 from importlib import import_module
6 from os.path import join, exists, abspath
7 from shutil import ignore_patterns, move, copy2, copystat
8 
9 import scrapy
10 from scrapy.commands import ScrapyCommand
11 from scrapy.utils.template import render_templatefile, string_camelcase
12 from scrapy.exceptions import UsageError
13 
14 
15 TEMPLATES_TO_RENDER = (
16     ('scrapy.cfg',),
17     ('${project_name}', 'settings.py.tmpl'),
18     ('${project_name}', 'items.py.tmpl'),
19     ('${project_name}', 'pipelines.py.tmpl'),
20 )
21 
22 IGNORE = ignore_patterns('*.pyc', '.svn')
23 
24 
25 class Command(ScrapyCommand):
26 
27     requires_project = False
28     default_settings = {'LOG_ENABLED': False}
29 
30     def syntax(self):
31         return "<project_name> [project_dir]"
32 
33     def short_desc(self):
34         return "Create new project"
35 
36     def _is_valid_name(self, project_name):
37         def _module_exists(module_name):
38             try:
39                 import_module(module_name)
40                 return True
41             except ImportError:
42                 return False
43 
44         if not re.search(r'^[_a-zA-Z]\w*$', project_name):
45             print('Error: Project names must begin with a letter and contain'\
46                     ' only\nletters, numbers and underscores')
47         elif _module_exists(project_name):
48             print('Error: Module %r already exists' % project_name)
49         else:
50             return True
51         return False
52 
53     def _copytree(self, src, dst):
54         """
55         Since the original function always creates the directory, to resolve
56         the issue a new function had to be created. It's a simple copy and
57         was reduced for this case.
58 
59         More info at:
60         https://github.com/scrapy/scrapy/pull/2005
61         """
62         ignore = IGNORE
63         names = os.listdir(src)
64         ignored_names = ignore(src, names)
65 
66         if not os.path.exists(dst):
67             os.makedirs(dst)
68 
69         for name in names:
70             if name in ignored_names:
71                 continue
72 
73             srcname = os.path.join(src, name)
74             dstname = os.path.join(dst, name)
75             if os.path.isdir(srcname):
76                 self._copytree(srcname, dstname)
77             else:
78                 copy2(srcname, dstname)
79         copystat(src, dst)
80 
81     def run(self, args, opts):
82         if len(args) not in (1, 2):
83             raise UsageError()
84 
85         project_name = args[0]
86         project_dir = args[0]
87 
88         if len(args) == 2:
89             project_dir = args[1]
90 
91         if exists(join(project_dir, 'scrapy.cfg')):
92             self.exitcode = 1
93             print('Error: scrapy.cfg already exists in %s' % abspath(project_dir))
94             return
95 
96         if not self._is_valid_name(project_name):
97             self.exitcode = 1
98             return
99 
100         self._copytree(self.templates_dir, abspath(project_dir))
101         move(join(project_dir, 'module'), join(project_dir, project_name))
102         for paths in TEMPLATES_TO_RENDER:
103             path = join(*paths)
104             tplfile = join(project_dir,
105                 string.Template(path).substitute(project_name=project_name))
106             render_templatefile(tplfile, project_name=project_name,
107                 ProjectName=string_camelcase(project_name))
108         print("New Scrapy project %r, using template directory %r, created in:" % \
109               (project_name, self.templates_dir))
110         print("    %s\n" % abspath(project_dir))
111         print("You can start your first spider with:")
112         print("    cd %s" % project_dir)
113         print("    scrapy genspider example example.com")
114 
115     @property
116     def templates_dir(self):
117         _templates_base_dir = self.settings['TEMPLATES_DIR'] or \
118             join(scrapy.__path__[0], 'templates')
119         return join(_templates_base_dir, 'project')
120     
121
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/scrapy/commands/startproject.py b/scrapy/commands/startproject.py
--- a/scrapy/commands/startproject.py
+++ b/scrapy/commands/startproject.py
@@ -17,6 +17,7 @@
     ('${project_name}', 'settings.py.tmpl'),
     ('${project_name}', 'items.py.tmpl'),
     ('${project_name}', 'pipelines.py.tmpl'),
+    ('${project_name}', 'middlewares.py.tmpl'),
 )
 
 IGNORE = ignore_patterns('*.pyc', '.svn')

{"golden_diff": "diff --git a/scrapy/commands/startproject.py b/scrapy/commands/startproject.py\n--- a/scrapy/commands/startproject.py\n+++ b/scrapy/commands/startproject.py\n@@ -17,6 +17,7 @@\n     ('${project_name}', 'settings.py.tmpl'),\n     ('${project_name}', 'items.py.tmpl'),\n     ('${project_name}', 'pipelines.py.tmpl'),\n+    ('${project_name}', 'middlewares.py.tmpl'),\n )\n \n IGNORE = ignore_patterns('*.pyc', '.svn')\n", "issue": "Add a sample middleware to startproject's template\nIt will be nice to have a middleware template inside the template project to serve as an example for people that want to use it.\n\n", "before_files": [{"content": "from __future__ import print_function\nimport re\nimport os\nimport string\nfrom importlib import import_module\nfrom os.path import join, exists, abspath\nfrom shutil import ignore_patterns, move, copy2, copystat\n\nimport scrapy\nfrom scrapy.commands import ScrapyCommand\nfrom scrapy.utils.template import render_templatefile, string_camelcase\nfrom scrapy.exceptions import UsageError\n\n\nTEMPLATES_TO_RENDER = (\n    ('scrapy.cfg',),\n    ('${project_name}', 'settings.py.tmpl'),\n    ('${project_name}', 'items.py.tmpl'),\n    ('${project_name}', 'pipelines.py.tmpl'),\n)\n\nIGNORE = ignore_patterns('*.pyc', '.svn')\n\n\nclass Command(ScrapyCommand):\n\n    requires_project = False\n    default_settings = {'LOG_ENABLED': False}\n\n    def syntax(self):\n        return \"<project_name> [project_dir]\"\n\n    def short_desc(self):\n        return \"Create new project\"\n\n    def _is_valid_name(self, project_name):\n        def _module_exists(module_name):\n            try:\n                import_module(module_name)\n                return True\n            except ImportError:\n                return False\n\n        if not re.search(r'^[_a-zA-Z]\\w*$', project_name):\n            print('Error: Project names must begin with a letter and contain'\\\n                    ' only\\nletters, numbers and underscores')\n        elif _module_exists(project_name):\n            print('Error: Module %r already exists' % project_name)\n        else:\n            return True\n        return False\n\n    def _copytree(self, src, dst):\n        \"\"\"\n        Since the original function always creates the directory, to resolve\n        the issue a new function had to be created. It's a simple copy and\n        was reduced for this case.\n\n        More info at:\n        https://github.com/scrapy/scrapy/pull/2005\n        \"\"\"\n        ignore = IGNORE\n        names = os.listdir(src)\n        ignored_names = ignore(src, names)\n\n        if not os.path.exists(dst):\n            os.makedirs(dst)\n\n        for name in names:\n            if name in ignored_names:\n                continue\n\n            srcname = os.path.join(src, name)\n            dstname = os.path.join(dst, name)\n            if os.path.isdir(srcname):\n                self._copytree(srcname, dstname)\n            else:\n                copy2(srcname, dstname)\n        copystat(src, dst)\n\n    def run(self, args, opts):\n        if len(args) not in (1, 2):\n            raise UsageError()\n\n        project_name = args[0]\n        project_dir = args[0]\n\n        if len(args) == 2:\n            project_dir = args[1]\n\n        if exists(join(project_dir, 'scrapy.cfg')):\n            self.exitcode = 1\n            print('Error: scrapy.cfg already exists in %s' % abspath(project_dir))\n            return\n\n        if not self._is_valid_name(project_name):\n            self.exitcode = 1\n            return\n\n        self._copytree(self.templates_dir, abspath(project_dir))\n        move(join(project_dir, 'module'), join(project_dir, project_name))\n        for paths in TEMPLATES_TO_RENDER:\n            path = join(*paths)\n            tplfile = join(project_dir,\n                string.Template(path).substitute(project_name=project_name))\n            render_templatefile(tplfile, project_name=project_name,\n                ProjectName=string_camelcase(project_name))\n        print(\"New Scrapy project %r, using template directory %r, created in:\" % \\\n              (project_name, self.templates_dir))\n        print(\"    %s\\n\" % abspath(project_dir))\n        print(\"You can start your first spider with:\")\n        print(\"    cd %s\" % project_dir)\n        print(\"    scrapy genspider example example.com\")\n\n    @property\n    def templates_dir(self):\n        _templates_base_dir = self.settings['TEMPLATES_DIR'] or \\\n            join(scrapy.__path__[0], 'templates')\n        return join(_templates_base_dir, 'project')\n    \n", "path": "scrapy/commands/startproject.py"}], "after_files": [{"content": "from __future__ import print_function\nimport re\nimport os\nimport string\nfrom importlib import import_module\nfrom os.path import join, exists, abspath\nfrom shutil import ignore_patterns, move, copy2, copystat\n\nimport scrapy\nfrom scrapy.commands import ScrapyCommand\nfrom scrapy.utils.template import render_templatefile, string_camelcase\nfrom scrapy.exceptions import UsageError\n\n\nTEMPLATES_TO_RENDER = (\n    ('scrapy.cfg',),\n    ('${project_name}', 'settings.py.tmpl'),\n    ('${project_name}', 'items.py.tmpl'),\n    ('${project_name}', 'pipelines.py.tmpl'),\n    ('${project_name}', 'middlewares.py.tmpl'),\n)\n\nIGNORE = ignore_patterns('*.pyc', '.svn')\n\n\nclass Command(ScrapyCommand):\n\n    requires_project = False\n    default_settings = {'LOG_ENABLED': False}\n\n    def syntax(self):\n        return \"<project_name> [project_dir]\"\n\n    def short_desc(self):\n        return \"Create new project\"\n\n    def _is_valid_name(self, project_name):\n        def _module_exists(module_name):\n            try:\n                import_module(module_name)\n                return True\n            except ImportError:\n                return False\n\n        if not re.search(r'^[_a-zA-Z]\\w*$', project_name):\n            print('Error: Project names must begin with a letter and contain'\\\n                    ' only\\nletters, numbers and underscores')\n        elif _module_exists(project_name):\n            print('Error: Module %r already exists' % project_name)\n        else:\n            return True\n        return False\n\n    def _copytree(self, src, dst):\n        \"\"\"\n        Since the original function always creates the directory, to resolve\n        the issue a new function had to be created. It's a simple copy and\n        was reduced for this case.\n\n        More info at:\n        https://github.com/scrapy/scrapy/pull/2005\n        \"\"\"\n        ignore = IGNORE\n        names = os.listdir(src)\n        ignored_names = ignore(src, names)\n\n        if not os.path.exists(dst):\n            os.makedirs(dst)\n\n        for name in names:\n            if name in ignored_names:\n                continue\n\n            srcname = os.path.join(src, name)\n            dstname = os.path.join(dst, name)\n            if os.path.isdir(srcname):\n                self._copytree(srcname, dstname)\n            else:\n                copy2(srcname, dstname)\n        copystat(src, dst)\n\n    def run(self, args, opts):\n        if len(args) not in (1, 2):\n            raise UsageError()\n\n        project_name = args[0]\n        project_dir = args[0]\n\n        if len(args) == 2:\n            project_dir = args[1]\n\n        if exists(join(project_dir, 'scrapy.cfg')):\n            self.exitcode = 1\n            print('Error: scrapy.cfg already exists in %s' % abspath(project_dir))\n            return\n\n        if not self._is_valid_name(project_name):\n            self.exitcode = 1\n            return\n\n        self._copytree(self.templates_dir, abspath(project_dir))\n        move(join(project_dir, 'module'), join(project_dir, project_name))\n        for paths in TEMPLATES_TO_RENDER:\n            path = join(*paths)\n            tplfile = join(project_dir,\n                string.Template(path).substitute(project_name=project_name))\n            render_templatefile(tplfile, project_name=project_name,\n                ProjectName=string_camelcase(project_name))\n        print(\"New Scrapy project %r, using template directory %r, created in:\" % \\\n              (project_name, self.templates_dir))\n        print(\"    %s\\n\" % abspath(project_dir))\n        print(\"You can start your first spider with:\")\n        print(\"    cd %s\" % project_dir)\n        print(\"    scrapy genspider example example.com\")\n\n    @property\n    def templates_dir(self):\n        _templates_base_dir = self.settings['TEMPLATES_DIR'] or \\\n            join(scrapy.__path__[0], 'templates')\n        return join(_templates_base_dir, 'project')\n    \n", "path": "scrapy/commands/startproject.py"}]}