[MapServer-dev] Variables in mapfiles

Seth G sethg at geographika.co.uk
Wed Dec 27 01:51:50 PST 2023


Hi Steve,

I like the idea of a VARIABLES block in the CONFIG file. I'm not sure a VALIDATION block would also be required - if a user can edit a CONFIG file they'd have to assume responsibility for any values set. 
As a first step of processing the Mapfile any $varName in a VARIABLE block default variable value would be replaced by the values in the CONFIG file. This should require no changes to the Mapfile syntax. 
I'd prefer to avoid using environment variables for values as these are very much platform and web server dependent. 

Seth

--
web:https://geographika.net & https://mapserverstudio.net
twitter: @geographika

On Tue, Dec 26, 2023, at 10:57 PM, Steve Lime via MapServer-dev wrote:
> As a follow up. My previous message was inaccurate and kind of dumb on my part. You only have to set a default value with no validation block for things to basically function as a variable with runtime subs. MapServer will not try to do a substitution if a validation expression is not set and will automatically fall back to the default value. So, you just need:
> 
>   "variable_default" "some value"
> 
> in a validation block and then you'd reference "%variable%" in supported locations. Passing variable= in the request will have no effect.
> 
> If we allowed "some value" to reference an environment variable (maybe with a leading $ in the default value) then I could see where that might be a pretty useful way to externalize some configuration information. We might consider adding a VALIDATION block to the config file as well.
> 
> --Steve
> 
> On Fri, Nov 17, 2023 at 3:37 PM Lime, Steve D (MNIT) via MapServer-dev <mapserver-dev at lists.osgeo.org> wrote:
>> Hi Jukka: Yeah, this is a potential area for improvement. We’ve be using a couple of strategies where I work. One being includes plus runtime subs so you can embed replacement strings as necessary, similar to your examples. The environment file gets included in a validation block and you set both a default and validation patterns for each value:____
>> __ __
>>   “dbhost_default” “some.database.host”____
>>   “dbhost”                “^some\.database\.host$”____
>> __ __
>> So dbhost can only be the one value. I feels cleaner just write things the way you suggest. However, it feels wrong to have to set a value twice – and is potentially error prone in a bad way. Plus, there’s no connection to the environment. I’m not a huge fan.____
>> __ __
>> The other approach is using a pre-processor to essentially compile a mapfile into a final version. We’re using YAML + mappyfile to create an environment-specific version of the mapfile. The YAML definition can consist of constants plus references to environment variables. There is loads of potential here but deployment in different environments requires a compilation step plus a working Python environment.____
>> __ __
>> So, I can see value in a VARIABLES block or reference to a file. Could also do that at the config file level instead. Beyond loading the hash somehow, one would need a method to apply the variables in a limited fashion, probably while the mapfile is being parsed. So, load the DATA value and then apply variables or something like that.____
>> __ __
>> This warrants an RFC I think…____
>> __ __
>> --Steve____
>> __ __
>> *From:* MapServer-dev <mapserver-dev-bounces at lists.osgeo.org> *On Behalf Of *Rahkonen Jukka via MapServer-dev
>> *Sent:* Thursday, November 9, 2023 5:08 PM
>> *To:* 'mapserver-dev at lists.osgeo.org' <mapserver-dev at lists.osgeo.org>
>> *Subject:* [MapServer-dev] Variables in mapfiles____
>> 
>> __ __
>> Hi,____
>> __ __
>> When I was reading the old bug reports in the OSGeo code sprint this 20 years old one https://github.com/MapServer/MapServer/issues/408 from year 2003 did not feel rotten at all. And the last comment https://github.com/MapServer/MapServer/issues/408#issuecomment-1065080140 has received 5 thumbs up within 1 year which shows great user activity by our standards.____
>> __ __
>> I think that environmental variables may be too strong tool for this purpose. For example, are those who write mapfiles allowed to set env variables in managed environments? Maybe something similar to SYMBOLSET and INCLUDE would be easier to use.____
>> As an example, I found a pretty good bug report that includes a mapfile and data, but for testing the mapfile on my Windows machine I need to make a few edits.____
>> __ __
>> CONFIG "PROJ_LIB" "/usr/local/share/proj"____
>>                           I am on Windows and my proj is in some other place____
>> WMS_ONLINERESOURCE http://odroid1:5001/____
>> wms_service_onlineresource http://odroid1:5001/____
>>                           I don’t understand the difference between those two, but anyway I run Mapserver at http://localhost:8060/...____
>> DATA "./bug-report.xml"____
>>                           I prefer to keep data in different place than mapfiles____
>> DATA "./data-in-31287"____
>>                           Same for this shapefile data____
>> __ __
>> What if we could write non-fixed paths and other items into the mapfile like____
>> __ __
>> CONFIG "PROJ_LIB" "%proj_lib%"____
>> WMS_ONLINERESOURCE "%onlineresource_test%"____
>> wms_service_onlineresource "%onlineresource_test%"____
>> DATA "%datapath_test%/bug-report.xml"____
>> DATA "%datapath_test%/data-in-31287"____
>> ….____
>> MAPVARIABLES "map_variables.txt"____
>> END # mapfile____
>> __ __
>> The contents of "map_variables.txt"____
>> __ __
>> //Some magic____
>> proj_lib                                      [value]____
>> onlineresource_test                [value]____
>> onlineresource_qa                  [value]____
>> onlineresourse_production   [value]____
>> datapath_test                           [value]____
>> datapath_qa                             [value]____
>> datapath_production             [value]____
>> symbolset_topomap               [value]____
>> symbolset_citymap                 [value]____
>> etc.____
>> __ __
>> A difference to INCLUDE is that while the whole include file is slipped inside the mapfile the variable file could be a dictionary. That would allow forwarding the mapfile from test to qa to production easily. And when the IP address of test servers change an edit in one place would be enough. It would still be possible to use separate "map_variables_testing.txt" for testing if it feels better.____
>> __ __
>> I admit that there are alternative scenarios via INCLUDE or config file or environmental variables.____
>> __ __
>> -Jukka Rahkonen-____
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> __ __
>> _______________________________________________
>> MapServer-dev mailing list
>> MapServer-dev at lists.osgeo.org
>> https://lists.osgeo.org/mailman/listinfo/mapserver-dev
> _______________________________________________
> MapServer-dev mailing list
> MapServer-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/mapserver-dev
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/mapserver-dev/attachments/20231227/8bf76852/attachment-0001.htm>


More information about the MapServer-dev mailing list