Commit 26c2f96118815d4be2e6fbad2bfbc6fae9da2d65

Jeremy JEANNE 2025-05-12T17:42:47

doc route controller 'en' 'fr'

diff --git a/test/httpd/Makefile b/test/httpd/Makefile
index 3e7b7a1..9ae48a6 100644
--- a/test/httpd/Makefile
+++ b/test/httpd/Makefile
@@ -10,7 +10,7 @@
 ## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
 ## THIS SOFTWARE.
 
-include ../config.mk
+SRC_TOP = ../..
 
 main: assets
 	${SRC_TOP}/httpd/.libs/kc3_httpd -C ${SRC_TOP}/test/httpd -d 127.0.0.1 15004
diff --git a/test/httpd/app/controllers/doc_controller.kc3 b/test/httpd/app/controllers/doc_controller.kc3
index 34be0b2..84dcae1 100644
--- a/test/httpd/app/controllers/doc_controller.kc3
+++ b/test/httpd/app/controllers/doc_controller.kc3
@@ -5,6 +5,7 @@ defmodule DocController do
   require List
   require Str
 
+  def available_locales = ["en", "fr"]
   def doc_index = fn {
     (path, path_md) { doc_index(path, path_md, path, []) }
     ([], path_md, dir, acc) { List.reverse(acc) }
@@ -136,28 +137,40 @@ defmodule DocController do
     %HTTP.Response{body: body}
   }
 
-  def route = fn (request) {
-    if (request.method == GET ||
-        request.method == HEAD) do
-      locale = "en"
-      path_html = ".#{request.url}/index.#{locale}.html"
-      if File.exists?(path_html) do
-        show_html(path_html, request.url)
+    
+  def route_locale = fn (url, locale) {
+    path_html = ".#{url}/index.#{locale}.html"
+    if File.exists?(path_html) do
+      show_html(path_html, url)
+    else
+      path_md = ".#{url}/index.#{locale}.md"
+      if File.exists?(path_md) do
+        show_md(path_md, url)
       else
-        path_md = ".#{request.url}/index.#{locale}.md"
+        path_md = ".#{url}.#{locale}.md"
         if File.exists?(path_md) do
-          show_md(path_md, request.url)
-        else
-          path_md = ".#{request.url}.#{locale}.md"
-          if File.exists?(path_md) do
-            show_md(path_md, request.url)
-          else
-            HTTPd.error_404_page(request)
-          end
+          show_md(path_md, url)
         end
       end
-    else
-      HTTPd.error_404_page(request)
+    end
+  }
+  
+  def route = fn (request) {
+    url = request.url
+    slash = if Str.ends_with?(url, "/") do "" else "/" end
+    url = url + slash
+    if (request.method == GET ||
+      request.method == HEAD) do
+      locale = if (url[0] == '/' && url[3] == '/') do
+        l = "#{url[1]}#{url[2]}"
+        if List.has?(available_locales, l) do
+          url = Str.slice(url, 3, -1)
+          l
+        end
+      end || "en"
+      if Str.starts_with?(url, "/doc/") do
+        route_locale(url, locale)
+      end
     end
   }
 
diff --git a/test/httpd/config/routes.kc3 b/test/httpd/config/routes.kc3
index bf6cd4d..f74b105 100644
--- a/test/httpd/config/routes.kc3
+++ b/test/httpd/config/routes.kc3
@@ -1,6 +1,6 @@
 def HTTPd.routes = []
 
-def_route("/doc/", DocController.route)
+def_route("/", DocController.route)
 
 # def_route("/fx/", FXController.fx_route)
 # def_route("/tags/", FXController.tags_route)