Skip to content

Commit

Permalink
fix(framework): add ejs et twig service
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamensuli committed Feb 29, 2024
1 parent dce3fc1 commit f82544c
Show file tree
Hide file tree
Showing 37 changed files with 1,602 additions and 1,185 deletions.
6 changes: 5 additions & 1 deletion nodefony/config/module-http-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ const certificates = {
},
};

let rejectUnauthorized = true;
switch (kernel?.environment) {
case "production":
case "development":
default:
if (kernel?.isDev) {
rejectUnauthorized = false;
}
certificates.privateKeyPath = path.resolve(
certificates.path,
"server",
Expand All @@ -43,6 +47,6 @@ switch (kernel?.environment) {
}

export default {
rejectUnauthorized: true,
rejectUnauthorized,
certificates,
};
78 changes: 55 additions & 23 deletions nodefony/controllers/AppController.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,103 @@
//import { Service, Container, Module } from "nodefony";
import { inject, Fetch } from "nodefony";
import { resolve } from "node:path";

import { inject, Fetch, FileClass } from "nodefony";
import { DefineRoute, DefineController, Controller } from "@nodefony/framework";
import { ContextType } from "@nodefony/http";
import https from "node:https";
//import { twig } from "@nodefony/framework";

class AppController extends Controller {
static override basepath: string = "/app";
constructor(
context: ContextType,
@inject("Fetch") private fetch: Fetch
@inject("Fetch") private fetchService: Fetch
) {
super("app", context);
}

async initialize() {
//console.log("passs initialize");
//await this.startSession();
//let response = await this.fetchService.fetch("https://google.fr");
//TODO add certificat client in serfice for unit test
// const agent = new https.Agent({
// rejectUnauthorized: false,
// });
// const response = await this.fetchService.fetch("https://localhost:5152", {
// agent,
// });
// console.log(response.headers, response.status, response.statusText);
return this;
}

@DefineRoute("route1", { path: "/", method: "GET" })
@DefineRoute("route1", { path: "", method: "GET" })
async method1() {
console.log("call method", this.fetch.library);
//await this.startSession();
return this.renderJson({ foo: "bar" });
const view = resolve(
this.module?.path as string,
"nodefony",
"views",
"index.twig"
);
return this.renderTwigView(view, this.metaData).catch((e) => {
throw e;
});
}

@DefineRoute("route4", {
path: "/add/{name}",
requirements: { methods: ["GET", "POST"] },
defaults: { name: "cci" },
})
async method4(name: string) {
const view = resolve(
this.module?.path as string,
"nodefony",
"views",
"index.ejs"
);
return this.renderEjsView(view, { name, ...this.metaData }).catch((e) => {
throw e;
});
}

@DefineRoute("route3", { method: "DELETE" })
method3() {
console.log("call method3", this.route);
async method3() {
return this.renderJson(this.metaData);
}

@DefineRoute("route2", { path: "/add", requirements: { methods: "POST" } })
method2() {
async method2() {
console.log("call method2");
return this.renderJson({ foo: "bar" });
}

@DefineRoute("route4", {
path: "/add/{name}",
requirements: { methods: ["GET", "POST"] },
defaults: { name: "cci" },
})
method4(name: string) {
console.log("call method4", name, this.route);
return this.renderJson({ name });
}

@DefineRoute("route6", {
path: "/ele/{metier}/{format}/add",
defaults: { format: "cci" },
})
method6() {
async method6() {
console.log("other route for app");
return this.renderJson({ foo: "bar" });
}
@DefineRoute("route7", {
path: "/ele/{metier}/{format}/{method}/add",
})
method7(metier: string, format: string, method: string) {
return this.renderJson({ metier, format, method });
const view = resolve(
this.module?.path as string,
"nodefony",
"views",
"index.twig"
);
return this.renderTwigView(view, { metier, format, method });
}

@DefineRoute("route5", {
path: "*",
})
method5() {
async method5() {
console.log("other route for app");
return this.renderJson(this.route);
}
}

Expand Down
25 changes: 25 additions & 0 deletions nodefony/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="{{nodefony.local|escape}}">
<head>

<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Nodefony (https://nodefony.net)">
<meta name="keywords" content="web,Framework,realtime,node.js,symfony,javascript,npm,linux">
<meta name="description" content="Node.js full-stack web framework Symfony Like">

<title><%= nodefony.name %></title>

</head>

<body>

<h1><%= nodefony.name %></h1>
<h1><%= name %></h1>


</body>

</html>
27 changes: 27 additions & 0 deletions nodefony/views/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html lang="{{nodefony.local|escape}}">
<head>
{% block head %}
{% block meta %}
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Nodefony (https://nodefony.net)">
<meta name="keywords" content="web,Framework,realtime,node.js,symfony,javascript,npm,linux">
<meta name="description" content="Node.js full-stack web framework Symfony Like">
{% endblock %}
{% block description %}{% endblock %}
<title>{% block title %}{{nodefony.name|escape}}{% endblock %}</title>
{% block stylesheets %} {% endblock %}
{% endblock %}

</head>

<body>
<h1>{{nodefony.name}}</h1>
{% block body %}{% endblock %}
{% block javascripts %} {% endblock %}
</body>

</html>
Loading

0 comments on commit f82544c

Please sign in to comment.