浏览代码

Add sourcedir for more reliable file paths

1991
urlysses 7 年前
父节点
当前提交
6310938b54
共有 2 个文件被更改,包括 26 次插入2 次删除
  1. +23
    -0
      1991.fs
  2. +3
    -2
      examples/app.fs

+ 23
- 0
1991.fs 查看文件

@@ -12,6 +12,20 @@ include unix/socket.fs
2dup exchange 2dup exchange
-1 /string -1 /string
repeat 2drop ; repeat 2drop ;
: sourcedir ( -- saddr su )
\ Returns the directory in which the file
\ invoking the word finds itself
\ relative to gforth's execution directory.
\ Useful for specifying in which dir to find
\ specific files (e.g., public/, views/).
sourcefilename \ get the name of our file
2dup reverse \ reverse and search for first /
s" /" search if \ if found, reverse string to
2dup reverse \ to strip the filename but keep dir.
else
2drop \ no slash,
s" ./" \ same dir execution.
then ;


\ User-defined routing \ User-defined routing
wordlist constant routes wordlist constant routes
@@ -36,6 +50,15 @@ pubvar public
public 2! ; public 2! ;
: get-public-path ( -- addr u ) : get-public-path ( -- addr u )
public 2@ ; public 2@ ;
sourcedir s" public" s+ set-public-path

\ Views directory
pubvar views
: set-view-path ( addr u -- )
views 2! ;
: get-view-path ( -- addr u )
views 2@ ;
sourcedir s" views" s+ set-view-path


\ Query params \ Query params
pubvar queryString pubvar queryString


+ 3
- 2
examples/app.fs 查看文件

@@ -1,7 +1,8 @@
\ App demo: \ App demo:
include ~+/1991.fs
include ../1991.fs


s" ~+/examples/public" set-public-path
sourcedir s" public" s+ set-public-path
sourcedir s" views" s+ set-view-path


: handle-/ s" fff" ; : handle-/ s" fff" ;
: handle-hi s" hi!" ; : handle-hi s" hi!" ;


正在加载...
取消
保存