forum-msg-id-135198-edit

Original date:2020-11-25 09:54:27 Edited by: ghaberek Subject: Re: Euphoria MVC updates

jmduro said...

Where should local CSS files be located to be used with the development server ?

Yeah I need to add a feature for serving files directly from a local directory. I'd also like to add an option for "base directory" so you can change it from current directory.

In the mean time, I've at least added send_file() and download_file() which are shorthand for verifying a file exists, looking up its mime-type, and then sending the required headers.

include mvc/app.e

send_file( sequence path ) - Return the contents of a file directly (and let the browser decide).
download_file( sequence filename, sequence path=filename ) - Return the contents of a file and prompt the browser to download it.

I pushed the commit for adding send_file() just now: https://github.com/OpenEuphoria/euphoria-mvc/releases/tag/v1.12.1

You can use this to add a static route handler to your application. Then just put everything in /static/, like /static/css/, /static/js/, etc.

Edit: Or you can add a route pattern for specific directories as I've indicated below.

include std/map.e 
include mvc/app.e 
 
include std/filesys.e -- for canonical_path() 
include std/search.e -- for search:begins() 
 
-- 
-- Route handler for files in /static/ 
-- 
function static( object request ) 
 
    -- get the path to the requested file 
    sequence path_info = map:get( request, "PATH_INFO" ) 
 
    -- 
    -- Note: path_info will always start with 
    -- "/static/" thanks to the route handler. 
    -- 
 
    -- ensure the local file is an actual full path 
    sequence local_file = canonical_path( "." & path_info ) 
 
    -- ensure the local file exists in current directory 
    if search:begins( current_dir(), local_file ) then 
 
        -- go ahead and send the file to the browser 
        return send_file( local_path ) 
 
    end if 
 
    -- tell the browser the file doesn't exist 
    return response_code( 404 ) 
end function 
app:route( "/static/.+" ) 
 
-- Or add a handler for specific directories like this: 
app:route( "/(css|js|fonts|images)/.+", "static" ) 
 
-- 
-- Note: that pattern should resolve to "static" and find the function above. If not, you can call 
-- app:route("pattern", "func name") or even app:route("pattern", "func name", routine_id("func name")) 
-- 

-Greg

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu