cherrypy serve index.html at root (but not other files) - cherrypy

Using cherrypy, I am able to serve a static index.html file by using the below configuration information:
location = os.path.dirname(os.path.realpath(__file__))
conf = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir': '',
'tools.staticdir.root': location,
'tools.staticdir.index': 'index.html'
}
}
cherrypy.tree.mount(Root(), '/', conf)
cherrypy.engine.start()
cherrypy.engine.block()
However, in doing so it looks like I'm also exposing all of my files in the web root directory. For example, people can also download server.py (which contains my cherrypy code).
Is there a way to get around this? I know that some people will attempt to access my site by doing http://www.example.com/index.html and I don't want them to 404 each time, since cherrypy will only allow them to go to http://www.example.com or http://www.example.com/index this seems like a problem to me.

An urgent thing is to separate the code of static content. Create, for example, a 'static' directory, as shown.
And as for index.html, whether it should be an alias for '/', you can create methods, replacing their names on the occurrences of '.' . by '_', as explained here: cherrypy: respond to url that includes a dot?
An example:
#!/usr/bin/env python
import os.path
import cherrypy
class Root:
#cherrypy.expose
def index(self):
return "bla"
index_shtml = index_html = index_htm = index_php = index
location = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
conf = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir': '',
'tools.staticdir.root': location,
}
}
cherrypy.tree.mount(Root(), '/', conf)
cherrypy.engine.start()
cherrypy.engine.block()

Related

Next Js take several params in the root

Suppose, I want to take params region and lang in URLs like:
https://root.com/{region}/{lang}
https://root.com/{region}/{lang}/school
How do I do that? I tried Dynamic Routing, but it doesn't seem to work with the root index.js. How should I structure my folder tree?
/pages/[region]/[lang]/index.js Doesn't look like a valid one.
In your default exported component in /pages/[region]/[lang]/index.js you can use next/router - useRouter.
The query object will contain the region and lang parameters.
import { useRouter } from "next/router";
export const Page = () => {
const { query } = useRouter();
console.log({ query });
return <>My index page!</>;
};
export default Page;
to get the /school path simply add a school.js file in the same folder as index.js and get query params with useRouter as above.
Hey I believe there are couple ways,
Use dynamic routing for URLs like https://root.com/{region}/{lang}.
Create a "pages" folder with subfolders for each region and language. Inside each subfolder, have an "index.js" file.
Use a library like "next-routes" or "react-router" for routing.

gradle get relative resource path

When I iterate over source repository I do like this
def resourceDir = proj.sourceSets.main.output.resourcesDir
resourceDir.eachFileRecurse(groovy.io.FileType.FILES) { // only files will be recognized
file ->
def path = FilenameUtils.separatorsToUnix(file.toString())
if (FilenameUtils.getExtension(file.toString()) in supportedResourceExt) {
proj.logger.lifecycle("Reading file {}.", file)
//.....
}
}
In log it writes this
Reading file D:\PROJECT_FOLDER\project\subproject\subsubproject\build\resources\main\com\package\something\file.txt
How to get only the part starting with com\package\something\file.txt without explicitly reading it like file.substring(file.indexOf)?
Maybe it's posible to relativize it with project path somehow?
It seems that:
proj.logger.lifecycle("Reading file {}.", file.absolutePath - resourceDir.absolutePath)
should work. Can't check it right now.

Shoulda route matcher with subdomains

I am using shoulda matchers to test critical routes on Rails 4.2.1 with Ruby 2.2.0 on a new application. I just moved my API namespace to a subdomain, and I can't figure out how to get the shoulda routes matcher (or any other concise routes test) to work.
Here is some example code:
config/routes.rb (using versionist for versioning, but that shouldn't be relevant)
namespace :api, path: '', constraints: { subdomain: 'api'} do
api_version(module: 'V1',
path: {value: 'v1'},
defaults: {format: 'json'}, default: true) do
resources :bills, only: :index
end
end
app/controllers/api/v1/bills_controller.rb
module API
module V1
class Bill < APIVersionsController
# GET api.example.com/v1/bills.json
def index
#bills = Bill.all.limit(10)
render json: #bills
end
end
end
end
test/controllers/api/v1/routing_test.rb
module API
module V1
class RoutingTest < ActionController::TestCase
setup { #request.host = 'http://api.example.com' }
should route('/v1/bills')
.to(controller: :bill, action: :index, format: :json)
end
end
end
Before I was using a subdomain, should route('/api/v1/bills').to(action: :index, format: :json) in my BillsControllerTest worked just fine.
Now, when I run rake test, I get Minitest::Assertion: No route matches "/v1/bills".
I've tried putting this in the BillControllerTest (no change);
I've tried an integration test (route matcher doesn't work);
I've tried setting the host with setup { host! 'api.example.com' } and setup { #request.host = 'api.example.com' }
I've tried putting the full URL in the get request ( { get 'http://api.example.com/v1/bills' } );
and I've tried putting subdomain: 'api' and constraints: subdomain: 'api' anywhere that might make sense.
What is a concise way to do route testing with subdomains/what is the current best practice? Is there a way to get the shoulda route matcher to work with them?
This ended up being a simple fix. I just needed to add a subdomain constraint in the #to method and ensure the #route method had the full url:
module API
module V1
class RoutingTest < ActionController::TestCase
should route(:get, 'http://api.example.com/v1')
.to('api/v1/data#index',
subdomain: 'api',
format: :json)
...
Or, if you are in data_controller_test.rb,
module API
module V1
class DataControllerTest < ActionController::TestCase
should route(:get, 'http://api.example.com/v1')
.to(action: :index,
subdomain: 'api',
format: :json)
...

Returning static page by concrete path with Nginx

It is very easy, but I have not found a direct answer to this simple question
I have link like
mysite/api/location/login
and i need to return static page
login.html
which lies in directory
root /data/www/
location =/api/location/login {
root /data/www/;
how to return static page here?
}
Do not use "root" here, you'll get /data/www/api/location/login as a result. Use "alias" instead
location = /api/location/login {
alias /data/www/;
index login.html;
}
http://nginx.org/en/docs/http/ngx_http_index_module.html#index

Variable project configuration is bound to in groovy axis plugin for jenkins

I have a multi-configuration build for which I'd like essentally one build to be run for each file matching foo/*/bar/*.xml. I figured the GroovyAxis Plugin would be a nice fit, but I cannot find any documentation on how the build configuration can be accessed from within the script, so I cannot read the workspace-directory from anywhere.
Running something like return new File('.').listFiles().collect{it.toString()} returns all files within the root directory of the server.
Can anyone point me in the right direction?
It took a while to figure this out, but here is a solution. Note that since the Groovy script runs on the master, you must use FilePath to access the files on the slave.
import hudson.FilePath
def workspace = context?.build?.workspace
if (null == workspace) {
return ['noworkspace'] // avoid returning 'default' so the user has a chance of figuring out what went wrong
}
def configDir = workspace.toString() + '/openpower/configs/'
def dir = new FilePath(workspace.channel, configDir)
def files = []
dir.list().each {
def name = it.getName()
if (name.endsWith('_defconfig')) {
files << name.replace('_defconfig', '')
}
}
return files

Resources