how to change default behaviour of rack offline - ruby-on-rails-3

I am trying to use rack offline in rials to make my webpage available offline. By default rack offline takes all files from the public folder into the cache manifest. In which file should I make changes so that it will take the add the file that I want into the cache manifest. I want to include the file in my views folder.

You need to add it to your routes.rb file. Here is my routes.rb file with a customized manifest. This will give you the index and the new routes as well as all of the html files in your public root (*.html) and every file in a sub-folder to public (*/*.*). You can slice and dice that however you need it for stuff in the public folder.
I don't know how to get the database specific routes like show and edit while offline. I would imagine Javascript is needed. Check out Railscast episode 248 for some ideas for integrating JS
OfflineConfirm::Application.routes.draw do
#match '/application.manifest' => Rails::Offline
resources :contacts
offline = Rack::Offline.configure do
cache ["contacts/new", "contacts"]
public_path = Rails.root.join("public")
Dir[public_path.join("*.html"),
public_path.join("*/*.*")].each do |file|
p = Pathname.new(file)
cache p.relative_path_from(public_path)
end
network "/"
end
match '/application.manifest' => offline
end
The routes file above will produce the following application.manifest
CACHE MANIFEST
# 700ae3e3002382cb98b93c299d7b7bda151183b4703ef65d4c46b0ecf9c46093
contacts/new
contacts
404.html
422.html
500.html
index.html
images/rails.png
javascripts/application.js
javascripts/jquery.js
javascripts/jquery.min.js
javascripts/rails.js
stylesheets/scaffold.css
NETWORK:
/

None of the files in your views folder are available without a server. You want to make a route available in the cache manifest? For instance "/about", which corresponds to a "views/about.haml" file for instance?
Add this to your config:
offline = Rack::Offline.configure do
cache "about" # or whatever your route is
public_path = Rails.public_path
Dir[public_path.join("javascripts/*.js")].each do |file|
cache file.relative_path_from(public_path)
end
end

Related

Nuxt JS - reading conf/env file in static site generation

My project with Nuxt JS is set with target:static and ssr: false.
This app need to connect to a local endpoint to retrieve some informations.
I have multiple endpoints and I need multiple instances of the app, every app must read only his endpoint.
The question is: how change the endpoint address for every app without rebuild everyone?
I tried with env file or a json file in the static folder (in order to have access to this file in the dist folder after the build process).
But if I modify the content of the env/json file in the dist folder and then reload the webpage (or also restart the web server that serve the dist folder), the app continue to use the original endpoint provided at the build time.
There is a way or I have to switch to server side rendering mode (which I would rather not use)?
Thank you!
When you use SSG, it will bundle your app at build time. Last time I checked, there was no hack regarding that. (I don't have the Github issue under my hand but it's a popular one)
And at the same time, I don't really see how it would be done since you want to mix something static and dynamic at the same time.
SSR is the only way here.
Otherwise, you may have some other logic to generate dynamic markup when updating your endpoints (not related to Nuxt) by fetching a remote endpoint I guess.
With the module nuxt content it's possible to create a folder "/content" in project directory and read json files from that directory.
After, when creating the dist with nuxt generate command, the "content" folder it's included in "_nuxt" folder of the dist and if you modify the content of the json file and refresh the webpage that read it, will take the new values.

Web2py & nginx - do I have to set up static folder

I'm running nginx/uWSGI and trying to lock down a web2py site using 'auth.requires_login()' (https://groups.google.com/forum/#!topic/web2py/0j92-sPp4bc) so that only logged in users can get to it, even the content under /static/. If I set up nginx config file with
location ~* /(\w+)/static/ {
root /home/www-data/web2py/applications/;
}
as recommended in the docs, won't that bypass the access control, and allow anyone to see the static content? If I leave this line out of the config file, will web2py still share the static content to logged-in users (although presumably a little slower)?
Yes, using that nginx rule will bypass web2py. Removing it and letting web2py handle /static/ won't change much either, as this is directly from the web2py manual:
http://127.0.0.1:8000/a/static/filename
There is no controller called "static". web2py interprets this as a request for the file called "filename" in the subfolder "static" of the application "a".
When static files are downloaded, web2py does not create a session, nor does it issue a cookie or execute the models.
So because there is no controller, you cannot directly use auth.requires_login() for static content. This is because files in /static/ are generally not meant to be access-controlled, or else browsers will not be able to get the css, js, etc. needed to even render the welcome or login page.
However, if you still want site-wide access control to static files (i.e. private pdf files) you can do it like so:
in your application directory, create a folder called private_static
then in your controller add the following:
#default.py
import os
auth.requires_login()
def private_static():
filename = os.path.join(*request.args)
allowed = ['private1.pdf', 'private2.pdf'] # have some kind of validation for security!
if not filename in allowed:
raise HTTP(404)
return response.stream(open(os.path.join(request.folder, 'private_static', filename)))
and in your view something like the following:
Private Document
Now accessing http://.../private_static/private1.pdf will force the user to login before getting the static file.

caches_page seems doesn't work properly in Rails 3?

I used caches_page in rails 3, everything went well, but I don't want those cache files spread in public directory by default, so I changed the default cache directory like this:
config.action_controller.page_cache_directory = Rails.public_path + "/caches"
Yes, it still works, it writes cache file to public/caches directory, but it seems doesn't read it back while refreshing the same page, it writes a new cache file again every time.
Is there something or any configuration I should do to fix this? or I should just use the default cache directory?
thank you all :)
eddie
Actually it is related to the server that is running the application .
for example: Webrick default cache directory is "public"
So when you set page_cache_directory to public, cached pages will be served properly .
The issue is related to the server not the app at all .
Quoted from http://guides.rubyonrails.org/caching_with_rails.html :
"By default, the page cache directory is set to Rails.public_path (which is usually set to the public folder) and this can be configured by changing the configuration setting config.action_controller.page_cache_directory. Changing the default from public helps avoid naming conflicts, since you may want to put other static html in public, but changing this will require web server reconfiguration to let the web server know where to serve the cached files from."
You might need to add a / after caches, try:
config.action_controller.page_cache_directory = Rails.public_path + "/caches/"
I have this on a Rails 3 app and it works:
config.action_controller.page_cache_directory = Rails.root.to_s + "/tmp/cache/"

How to access the "app" folder via browser (localhost) in cakePHP?

I am using Apache server. Usually when I want to start a new website project, I created a new folder inside my server directory and inside that folder I would have 'index.html or index.php'. When I direct my localhost URL to that folder, it would open it and automatically display index.php.
Now I am trying something new with this CakePHP framework. I finished setting it up, and when I direct my localhost URL to folder 'cakephp-cakephp' (folder containing all the cakePHP files), it then shows me this message:
Release Notes for CakePHP 2.0.0-dev.
Your tmp directory is writable.
The FileEngine is being used for caching. To change the config edit APP/config/core.php
Your database configuration file is present.
I think the next step here is to start the development by saving all my files to "app" folder. But the message is not gone even when I deleted the default index.php files from inside the folder 'cakephp-cakephp'. It seems anyway, the index.php files do not actually generate the message.
Does anybody know what file generates that release notes message? I want to delete that file so that I wont get the message (which I believe is triggered by some default file like 'index.php') when I access folder 'cakephp-cakephp', and then it will show me the file directory inside that folder instead, and I can traverse file directory easily using my browser and access my app folder, in which I am planning to store my index.php file, and save all my development files..
You cannot delete the index.php files and you usually don't modify those files unless you need to do some special configuration. Adding the file app/views/pages/home.ctp gets rid of that message and becomes your default home page.
Also, as stated in the comments, you should really read the manual and try the tutorial.
http://book.cakephp.org

Symfony: Images still save in web/uploads after I changed web root to public_html

In my symfony project, I changed my webroot directory to public_html to fit with a specific host.
However, in the admin generator, when saving a file, the system creates a web folder, then stores the image in web/uploads
How can I tell the system to now save in public_html instead?
Symfony v 1.0
The uploads are saved based on variables stored in the constants.php file.
symfony data folder / symfony / config / constants.php
In this case, the 2 required were:
'sf_web_dir_name' => $sf_web_dir_name = 'web',
'sf_upload_dir_name' => $sf_upload_dir_name = 'uploads',