File tree 4 files changed +41
-0
lines changed
4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1
1
from fastapi import FastAPI
2
2
from routes import router
3
3
4
+ from custom_routes import router as custom_route
5
+
4
6
app = FastAPI ()
5
7
6
8
app .include_router (router )
9
+
10
+ app .include_router (custom_route )
Original file line number Diff line number Diff line change
1
+ from fastapi import APIRouter
2
+
3
+ router = APIRouter ()
4
+
5
+
6
+ @router .get ('/v1/hello' )
7
+ def greetings ():
8
+ return {"hello" : "world!" }
Original file line number Diff line number Diff line change @@ -82,15 +82,25 @@ const lang2extension = (lang) => {
82
82
}
83
83
}
84
84
85
+ const findFileNamesEndWith = ( dir , postfix ) => {
86
+ return fs . readdirSync ( dir ) . filter ( name => name . endsWith ( postfix ) )
87
+ }
88
+
85
89
const createApp = async ( destDir , lang ) => {
86
90
const ext = lang2extension ( lang )
87
91
const fileName = `app.${ ext } `
88
92
console . log ( 'Generate' , fileName ) ;
89
93
const resultFile = path . join ( destDir , fileName ) ;
94
+ const customRouters = findFileNamesEndWith ( destDir , `_routes.${ ext } ` )
95
+ if ( customRouters . length > 0 ) {
96
+ customRouters . forEach ( filename => console . log ( `Include a custom router from ${ filename } ` ) )
97
+ }
90
98
91
99
const resultedCode = await ejs . renderFile (
92
100
`${ __dirname } /templates/${ fileName } .ejs` ,
93
101
{
102
+ // @todo #27 Document usage of user defined routes
103
+ 'customRouteFilenames' : customRouters
94
104
}
95
105
)
96
106
Original file line number Diff line number Diff line change
1
+ <%
2
+
3
+ // "custom_routes.py" => "custom_route"
4
+ function fileName2routerName (filename ) {
5
+ return filename .replace (/ _routes\. py$ / , ' _route' )
6
+ }
7
+
8
+ // "custom_routes.py" => "custom_routes"
9
+ function removeExtension (filename ) {
10
+ return filename .replace (/ \. py$ / , ' ' )
11
+ }
12
+
13
+ - %>
1
14
from fastapi import FastAPI
2
15
from routes import router
16
+ <% customRouteFilenames .forEach (filename => { % >
17
+ from < %= removeExtension (filename) % > import router as <%= fileName2routerName(filename) %>
18
+ < % }) - %>
3
19
4
20
app = FastAPI()
5
21
6
22
app.include_router(router)
23
+ <% customRouteFilenames .forEach (filename => { % >
24
+ app .include_router (< %= fileName2routerName (filename) % > )
25
+ < % }) - %>
You can’t perform that action at this time.
0 commit comments