Can I create a `gramex` `microservice` that automatically takes the subdirectories and renders the `markdown` in them? - gramex

I am trying to create a Gramex application. The intention is to:
See the root folder readme.md page when I access localhost:9988 (local gramex generic port)
See the readme.md in subdir1 when I access localhost:9988/subdir1/
See the readme.md in subdir2 when I access localhost:9988/subdir2/
For that I have a gramex.yaml file, that reads
url:
app:
pattern: /$YAMLURL/
handler: FileHandler
kwargs:
index: false
path:
"": $YAMLPATH/{dir}/readme.md
transform:
"*.md":
encoding: utf-8
function: markdown.markdown(content)
headers:
Content-Type: text/html; charset=UTF-8
default:
dir: ""
file: readme
ext: md
Here localhost:9988 renders the root folder readme.md.
However, browsing localhost:9988:subdir1 renders the directory index, implying that subdirs are not working.
Wondering if this is possible or not.

Related

Drupal 9.4 is ignoring my translation info

The ...info.yml:
name: CEnquire
version: '9.x-1.3'
description: BlahBlah
package: CPackage
type: module
core_version_requirement: ^8 || ^9
interface translation project: cenquire
interface translation server pattern: modules/cpackage/cenquire/translation/%project-%version.%language.po
The language: Ukrainian (uk)
The .po file is named cenquire-9.x-1.3.uk.po and placed in proper directory
drush locale-check just ignores yml data: I don't get any Translation file not found errors. The site just has no data about any translation for the project exists. This is not a cache issue.
What am I doing wrong?

Conditional auth_basic_user_file depending on the request path

I would like to authorize users depending on the requested path. For example, only user1 and user2 should have access to /projects/1.
My /etc/nginx/.htpasswd looks like this:
user1:$hashed_psswd1
user2:$hashed_psswd2
user3:$hashed_psswd3
...
Here's the location block in nginx config:
location ~ /projects(/.*) {
auth_basic "Please provide credentials";
auth_basic_user_file /etc/nginx/.htpasswd;
include /var/www/html/myapp.com/config/nginx/git-http-backend.conf;
}
And the rails action:
def git
redirect_to "#{Rails.configuration.x.domain}/projects/#{#project.id}"
end
The authentication seems to work:
$ git clone http://localhost:3000/projects/1
Cloning into '1'...
Username for 'http://localhost:3001': user1
Password for 'http://deploy#localhost:3001':
remote: Enumerating objects: 94, done.
# ... it successfully clones the git repo...
The problem is that user3 is able to clone project 1, but he shouldn't be able to clone such project.
So how could I authorize users in nginx + rails depending on the requested path?
Here is a config I've just tested with OpenResty 1.17.8.2 (based on nginx 1.17.8 core) and can confirm that it is workable:
map $uri $realm {
~^/projects/ "Protected projects";
~^/images/ "Protected images";
default off;
}
map $uri $authfile {
~^/projects/ /path/to/.htpasswd_projects;
~^/images/ /path/to/.htpasswd_images;
}
server {
...
auth_basic $realm;
auth_basic_user_file $authfile;
...
}
.htpasswd_projects and .htpasswd_images files contains each own user/password list. With the above config every URI started with /projects/ prefix is protected with basic authorization and available only for users listed in the htpasswd_projects file, every URI started with /images/ is protected with basic authorization and available only for users listed in the htpasswd_images file, and rest of the URIs are available without authorization for everyone.
Update
Here is another example, using dynamically generated .htpasswd path depending on the requested URI:
location ~ ^/projects/(?<project_id>[^/]+) {
if (!-f /var/www/projects/$project_id/.htpasswd) {
# '.htpasswd' file for the requested project does not exists
# assuming wrong project ID is given, returning HTTP 404 Not found
return 404;
}
auth_basic "Please provide credentials for project $1";
auth_basic_user_file /var/www/projects/$project_id/.htpasswd;
...
}
This one would work with the following .htpasswd files tree:
/var/www/projects
├── 1
│ └── .htpasswd
├── 2
│ └── .htpasswd
...
You can place this projects directory wherever you want, it isn't dependent on web server root.

How can I easily reference separate js / css files for debugging when using gulp or grunt?

I'm developing a legacy ASP.NET MVC 5 project which still uses ASP.NET Bundling and Minification. I'm interested in switching to Gulp or Grunt, because I need to save source maps for my js files.
It seems easy to generate a minified script bundle with Gulp or Grunt, but what I do not understand yet is the recommended setup for loading single js files when debugging and minified bundles in production. I guess it would be quite easy to generate a razor view for including the scripts as part of my Grunt / Gulp compilation process, but it feels like re-inventing the wheel.
For instance, in ASP.NET MVC i can write something like this:
#Scripts.Render("~/bundles/MyJSBundle")
and it will automatically load separate js files in development and a single script bundle in production. What is the easiest way achieve this with Gulp or Grunt?
Short answer:
Typically when using Grunt you generate two builds - one for "dev" (development) and another for "dist" (distribution/production). Whereby for the scenario you've described;
Both the "dev" and "dist" builds generate a single concatenated/minified file version (e.g. bundle.min.js) derived from multiple source .js files.
However, only the "dev" build generates an additional Source Map file(s), that holds information about your original .js files, for the purpose of debugging during the development lifecycle.
Grunt plugins, such as grunt-processhtml, provide a way to update any links to .js assets in the .html file. For example, let's say your source .html contains these two links;
<script src="js/a.js"/>
<script src="js/b.js"/>
They can be substituted during the "dist" and/or "dev" build step to the following single <script> element:
<script src="dir/bundle.min.js"/>
Example demo:
The following somewhat contrived example demonstrates how you may approach your requirement using Grunt.
Let's say our initial project directory is structured as follows:
project
├── Gruntfile.js
├── node_modules
│   └── ...
├── package.json
└── src
├── index.html
└── js
├── a.js
└── b.js
Note, in the src directory we have a single index.html file, and two .js files in the js directory.
In the contents of index.html shown below it contains two <script> elements, each one referencing a .js file.
project/src/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>demo</title>
</head>
<body>
<!--build:js js/bundle.min.js-->
<script src="js/a.js"></script>
<script src="js/b.js"></script>
<!--/build-->
</body>
</html>
Note, the custom HTML comments encasing both <script> elements. These custom HTML comments are utilized by grunt-processhtml. The part that reads; js/bundle.min.js in the comment essentially defines the new pathname to be used.
Let's consider the following Gruntfile.js configuration:
Gruntfile.js
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-processhtml');
grunt.initConfig({
// 1. Concatenate .js files.
concat: {
dist: {
src: [
'src/js/a.js',
'src/js/b.js'
],
dest: './dist/js/bundle.min.js'
},
dev: {
options: {
sourceMap: true
},
src: [
'src/js/a.js',
'src/js/b.js'
],
dest: './dev/js/bundle.min.js'
}
},
// 2. Minify .js files.
uglify: {
dist: {
files: {
'./dist/js/bundle.min.js': './dist/js/bundle.min.js' // dest : src
}
},
dev: {
options: {
mangle: false,
sourceMap: true,
sourceMapIn: './dev/js/bundle.min.js.map'
},
files: {
'./dev/js/bundle.min.js': './dev/js/bundle.min.js' // dest : src
}
}
},
// 2. Process .html file.
processhtml: {
dist: {
files: {
'./dist/index.html': './src/index.html' // dest : src
}
},
dev: {
files: {
'./dev/index.html': './src/index.html' // dest : src
}
}
}
});
grunt.registerTask('default', ['dist', 'dev']);
grunt.registerTask('dist', [
'concat:dist',
'uglify:dist',
'processhtml:dist'
]);
grunt.registerTask('dev', [
'concat:dev',
'uglify:dev',
'processhtml:dev'
]);
};
Explanation of Gruntfile.js:
In addition to the previously mentioned grunt-processhtml plugin the following two are also utilized in this example:
grunt-contrib-concat - for concatenating the two .js files.
grunt-contrib-uglify - for minifying the .js file.
Note: There are other plugins available for these types of task. I have chosen these additional two plugins for the purpose of this demonstration.
Each of the three Tasks (concat, uglify, and processhtml) contain two separate Targets named dist and dev. The main differences in each Target are:
Different dest (destination) paths for the resultant generated .jsfile(s).
For the concat:dev and uglify:dev Targets its options object defines the configuration for the resultant Source Map file.
At the end of Gruntfile.js three different grunt.registerTask() have been defined. Each one defines a taskList that essentially defines which Task and Target to run in the order specified.
For example consider the following registered task named dist:
grunt.registerTask('dist', [
'concat:dist',
'uglify:dist',
'processhtml:dist'
]);
When running grunt dist via the command line Grunt essentially invokes this Task, which subsequently performs the following in this order:
Firstly, runs the dist Target defined in the concat Task.
Then runs the dist Target defined in the uglify Task.
Finally, runs the dist Target defined in the processhtml Task.
Running Gruntfile.js (above) and its output
Running the following command via the command line:
grunt dev
outputs the following additional assets to the project directory:
project
├── ...
├── dev
│   ├── index.html
│   └── js
│   ├── bundle.min.js
│   └── bundle.min.js.map
└── ...
As you can see it has:
Created a new dev folder in the root of the project directory.
The two <script> elements originally defined in project/src/index.html have been substituted in the newly generated project/dev/index.html with a single <script> tag as follows:
<script src="js/bundle.min.js"></script>
Both files; project/src/js/a.js and project/src/js/b.js, have been concatenated and minified in the resultant project/dev/js/bundle.min.js.
The following source map file has been generated; project/dev/js/bundle.min.js.map. This file essentially maps back to the original project/src/js/a.js and project/src/js/b.js files.
Running the following command via the command line:
grunt dist
outputs the following additional assets to the project directory:
project
├── ...
├── dist
│ ├── index.html
│ └── js
│ └── bundle.min.js
└── ...
As you can see this time it has;
Created a dist folder in the root of the project directory.
Again, the two <script> elements originally defined in project/src/index.html have been substituted in the newly generated project/dist/index.html with a single <script> tag (as per the aforementioned dev Task).
Again, both files; project/src/js/a.js and project/src/js/b.js, have been concatenated and minified in the resultant project/dist/js/bundle.min.js.
However, the main notable difference is that NO source map file has been created.
Running the following command via the command line:
grunt
will produce both the outputs defined in the previous steps 1 and 2.

How to build with Electron-Builder when index.html has relative path

I want to build a project written in electron.js with Electron-builder when index.html file in the parent directory, a simple app with structure like below:
project
├── css
│ ├── style.css
│ └── img.png
├── electron
│ ├── main.js
│ └── package.json
│── index.html
│── index.js
└── icon.png
use win.loadFile('./../index.html'); in the main.js file, everythings works fine when use electron . command in the electron folder, but after run electron-builder, when run the app with the executable file nothing works and the index.html file doesn't appear.
I was trying to use files config in the package.json file but no success:
"build": {
"appId": "x.y.z",
"productName": "projectName",
"files": ["**/*", "build/icon.*", "../**/*"]
},
I don't know what to do ?!
I've found this issue https://github.com/electron-userland/electron-builder/issues/2693 but can't undestand it and don't know what should I do?
I can't answer in the general case, but here's the solution when using vue-electron-builder and TypeScript. This will let you organize your files in any arbitrary hierarchy.
Create a file vue.config.js in your project root (this more or less corresponds to the overrides you'd need for an electron project with webpack):
module.exports = {
pluginOptions: {
electronBuilder: {
// my 'main' process is in src/main.ts:
mainProcessFile: 'src/main/main.ts',
// my 'main.ts' should get recompiled if any of these change:
mainProcessWatch: [
'src/main/extra_file_1.ts',
'src/main/extra_file_2.ts',
]
}
},
pages: {
// I only have one renderer process (otherwise I'd need another entry in 'pages')
"renderer": {
// 'main' for the renderer process
entry: "src/renderer/main.ts",
// this is the part you care about: you can put the template anywhere
template: "src/viewer/index.html",
// this makes sure your renderer page has all the ts-to-js chunks it needs
chunks: ["chunk-vendors", "chunk-common", "renderer"]
}
}
};
Note that if you want your index.html to <link> to a static css file, you should put the static file in the public folder of your project root. Then you can link to it like this:
<link rel="stylesheet" href="<%= BASE_URL %>my_css_file.css">

pub serve not finding files under lib

I'm testing with the developer preview of Angular Version 2 and serving it up using pub serve
I would like to estalblish a convention to structure my files inside /lib/ as follows:
└── project/lib/root
├── root.css
├── root.dart
└── root.html
└── project/web/
├── index.html
└── index.dart
In my /web/index.dart file i am successfully able to initialize the #View and #Component from root.dart
However, when i preview in Dartium via pub serve - i can't seem to serve up /lib/root/root.html
#Component(
selector: 'jroot'
)
#View(
templateUrl: '../../lib/root/root.html',
directives: const [If]
)
class Root {
String content = 'Root - This is the entry point to the component';
List<Times> list;
Root(){
print('RootComponent Init');
}
}
Dartium Console:
GET http://localhost:8080/lib/root/root.html 404 (Not Found)
I've been reading the documents for polymer here which states:
"Non-Dart files under lib must use relative paths to import assets under lib:"
https://www.dartlang.org/polymer/app-directories.html
<!-- lib/a5/a6/a6.html imports lib/a4.html -->
<link rel="import" href="../../a4.html">
Running a python simple web server seems to work, so the problems is with pub serve:
python -m SimpleHTTPServer
Question: How do a configure pub serve to work with nested html files from the lib folder?
Ensure you have the proper entry points on your pubspec.yaml transformers:
name: 'project'
version: 0.0.1
dependencies:
angular2: '2.0.0-alpha.23'
browser: any
transformers:
- angular2:
entry_points:
- web/index.dart //this
reflection_entry_points:
- web/index.dart //this
Then I think this should work
templateUrl: 'packages/project/root/root.html',
or just
templateUrl: 'root.html',

Resources