@@ -13,6 +13,7 @@ import (
1313 "strings"
1414
1515 "github.com/julienschmidt/httprouter"
16+ "robinplatform.dev/internal/log"
1617)
1718
1819//go:generate rm -rf web
@@ -32,13 +33,27 @@ func (server *Server) loadRoutes() {
3233 return err
3334 }
3435 if ! entry .IsDir () {
35- route := strings .TrimPrefix (assetPath , "web" )
36+ routes := []string {strings .TrimPrefix (assetPath , "web" )}
37+
3638 if strings .HasSuffix (assetPath , ".html" ) {
37- route = regexp .MustCompile (`\[([^\]]+)\]` ).ReplaceAllString (route , ":$1" )
38- route = strings .TrimSuffix (route , ".html" )
39+ wildcardStartIndex := strings .Index (routes [0 ], "[[..." )
40+ if wildcardStartIndex != - 1 {
41+ wildcardName := routes [0 ][wildcardStartIndex + 5 :]
42+ wildcardName = wildcardName [:len (wildcardName )- 7 ]
43+
44+ routes = []string {
45+ routes [0 ][:wildcardStartIndex - 1 ],
46+ routes [0 ][:wildcardStartIndex ] + "*" + wildcardName ,
47+ }
48+ }
49+
50+ for idx := range routes {
51+ routes [idx ] = regexp .MustCompile (`\[([^\]]+)\]` ).ReplaceAllString (routes [idx ], ":$1" )
52+ routes [idx ] = strings .TrimSuffix (routes [idx ], ".html" )
3953
40- if strings .HasSuffix (route , "/index" ) {
41- route = strings .TrimSuffix (route , "index" )
54+ if strings .HasSuffix (routes [0 ], "/index" ) {
55+ routes [idx ] = strings .TrimSuffix (routes [idx ], "index" )
56+ }
4257 }
4358 }
4459
@@ -47,20 +62,27 @@ func (server *Server) loadRoutes() {
4762 mimetype = "text/html"
4863 }
4964
50- router .GET (route , func (res http.ResponseWriter , req * http.Request , _ httprouter.Params ) {
51- file , err := nextBuild .Open (assetPath )
52- if os .IsNotExist (err ) {
53- // TODO: serve 404 page
54- res .WriteHeader (404 )
55- res .Write ([]byte ("404 - Not Found" ))
56- } else if err != nil {
57- res .WriteHeader (500 )
58- res .Write ([]byte (err .Error ()))
59- } else {
60- res .Header ().Set ("Content-Type" , mimetype )
61- io .Copy (res , file )
62- }
63- })
65+ for _ , route := range routes {
66+ logger .Debug ("Registering route" , log.Ctx {
67+ "route" : route ,
68+ "path" : assetPath ,
69+ })
70+
71+ router .GET (route , func (res http.ResponseWriter , req * http.Request , _ httprouter.Params ) {
72+ file , err := nextBuild .Open (assetPath )
73+ if os .IsNotExist (err ) {
74+ // TODO: serve 404 page
75+ res .WriteHeader (404 )
76+ res .Write ([]byte ("404 - Not Found" ))
77+ } else if err != nil {
78+ res .WriteHeader (500 )
79+ res .Write ([]byte (err .Error ()))
80+ } else {
81+ res .Header ().Set ("Content-Type" , mimetype )
82+ io .Copy (res , file )
83+ }
84+ })
85+ }
6486 }
6587 return nil
6688 })
0 commit comments