How to serve static file separately by creating handler in another folder.

43 views
Skip to first unread message

Shailesh Yadav

unread,
Apr 1, 2022, 8:04:18 AM4/1/22
to Tornado Web Server
I am new to tornado web framework. I have below code.

 self.application = tornado.web.Application(self.url_spec,
                                                #    static_url_prefix=f"/{version}/download",
                                                #  static_url_prefix=BaseRequestHandler.download(self),
                                                #    static_path=str(static_path)
                                               
                                                )
//Earlier It was working fine but now I want to implement authentication for static file so I am creating separate handler for that.
Note: self.url_spec will load all the created handler.

//now my custom handler code for static file is .
(I am not understanding now I am writing it outside of tornado.web.Application so It will work? ).

print("****** Testing Static File *********")
@class_logger
class static_file(AuthorizationHandler):

    URL_PATTERN = r"/v2/download/(file|static_file)$"

    def do_get(self,version,api):
         
        static_path= pathlib.Path(pkg_resources.resource_filename("nzrest.app.data", "web")),      
        static_url_prefix= str(static_path),
               

        print("static_file passed",api)
        print("*************************************")


        return HTTPResponse(200, 'OK', static_path)

//any help on this would be very much appreciated. 
Reply all
Reply to author
Forward
0 new messages