Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 558c956

Browse files
Merge branch 'main' into jwt-auth
2 parents bc5f5f8 + 028f194 commit 558c956

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Oxygen = "df9a0d86-3283-4920-82dc-4555fc0d1d8b"
3737
Proj = "c94c279d-25a6-4763-9509-64d165bea63e"
3838
ProtoBuf = "3349acd9-ac6a-5e09-bcdb-63829b23a429"
3939
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
40+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
4041
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
4142
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
4243
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ These are currently the files/data created in Step/Script 1a in https://github.c
1818
PREPPED_DATA_DIR = "C:/some_path_to_data/MPA/"
1919

2020
[server_config]
21-
CACHE_DIR = "<some location to cache geotiffs>"
21+
TIFF_CACHE_DIR = "<some location to cache geotiffs>"
22+
REGIONAL_CACHE_DIR = "<some location to cache regional datasets>"
2223
DEBUG_MODE = "false" # Optional, disables file caching and displays debug logs
2324
COG_THREADS = "2" # Optional, Number of threads to use when creating COGs (defaults to 1)
2425
TILE_SIZE = "256" # Optional, tile block size to use (defaults to 256)

src/ReefGuideAPI.jl

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using
55
Glob,
66
TOML
77

8+
using Serialization
9+
810
using DataFrames
911
using OrderedCollections
1012
using Memoization
@@ -38,18 +40,38 @@ function get_regions()
3840
return regions
3941
end
4042

43+
"""
44+
setup_regional_data(config::Dict)
45+
46+
Load regional data to act as an in-memory cache.
47+
48+
# Arguments
49+
- `config` : Configuration settings, typically loaded from a TOML file.
50+
- `reef_data_path` : Path to pre-prepared reef data
51+
52+
# Returns
53+
OrderedDict of `RegionalCriteria` for each region.
54+
"""
4155
function setup_regional_data(config::Dict)
42-
return setup_regional_data(config["prepped_data"]["PREPPED_DATA_DIR"])
43-
end
44-
function setup_regional_data(reef_data_path::String)
4556
if @isdefined(REGIONAL_DATA)
4657
@debug "Using previously generated regional data store."
4758
sleep(1) # Pause so message is noticeably visible
4859
return REGIONAL_DATA
4960
end
5061

62+
# Check disk-based store
63+
reg_cache_dir = config["server_config"]["REGIONAL_CACHE_DIR"]
64+
reg_cache_fn = joinpath(reg_cache_dir, "regional_cache.dat")
65+
if isfile(reg_cache_fn)
66+
@debug "Loading regional data cache from disk"
67+
@eval const REGIONAL_DATA = deserialize($(reg_cache_fn))
68+
return REGIONAL_DATA
69+
end
70+
5171
@debug "Setting up regional data store..."
5272

73+
reef_data_path = config["prepped_data"]["PREPPED_DATA_DIR"]
74+
5375
regional_assessment_data = OrderedDict{String, RegionalCriteria}()
5476
for reg in get_regions()
5577
data_paths = String[]
@@ -75,7 +97,8 @@ function setup_regional_data(reef_data_path::String)
7597
elseif occursin("flat", string(dp))
7698
flat_table = GeoParquet.read(parq_file)
7799
else
78-
error("Unknown lookup found: $(parq_file)")
100+
msg = "Unknown lookup found: $(parq_file). Must be 'slope' or 'flat'"
101+
throw(ArgumentError(msg))
79102
end
80103
end
81104
end
@@ -97,19 +120,28 @@ function setup_regional_data(reef_data_path::String)
97120
)
98121
end
99122

123+
# Store cache on disk to avoid excessive cold startup times
124+
@debug "Saving regional data cache to disk"
125+
serialize(reg_cache_fn, regional_assessment_data)
126+
100127
# Remember, `@eval` runs in global scope.
101128
@eval const REGIONAL_DATA = $(regional_assessment_data)
102129

103130
return REGIONAL_DATA
104131
end
105132

106-
function _cache_location(config)
133+
"""
134+
_cache_location(config::Dict)::String
135+
136+
Retrieve cache location for geotiffs.
137+
"""
138+
function _cache_location(config::Dict)::String
107139
cache_loc = try
108140
in_debug = "DEBUG_MODE" in config["server_config"]
109141
if in_debug && lowercase(config["server_config"]["DEBUG_MODE"]) == "true"
110142
mktempdir()
111143
else
112-
config["server_config"]["CACHE_DIR"]
144+
config["server_config"]["TIFF_CACHE_DIR"]
113145
end
114146
catch
115147
mktempdir()
@@ -118,7 +150,12 @@ function _cache_location(config)
118150
return cache_loc
119151
end
120152

121-
function n_gdal_threads(config)::String
153+
"""
154+
n_gdal_threads(config::Dict)::String
155+
156+
Retrieve the configured number of threads to use when writing COGs with GDAL.
157+
"""
158+
function n_gdal_threads(config::Dict)::String
122159
n_cog_threads = try
123160
config["server_config"]["COG_THREADS"]
124161
catch
@@ -128,7 +165,12 @@ function n_gdal_threads(config)::String
128165
return n_cog_threads
129166
end
130167

131-
function tile_size(config)::Tuple
168+
"""
169+
tile_size(config::Dict)::Tuple
170+
171+
Retrieve the configured size of map tiles in pixels (width and height / lon and lat).
172+
"""
173+
function tile_size(config::Dict)::Tuple
132174
tile_dims = try
133175
res = parse(Int, config["server_config"]["TILE_SIZE"])
134176
(res, res)
@@ -145,8 +187,19 @@ function get_auth_middleware(config :: Dict)
145187
return [auth]
146188
end
147189

190+
"""
191+
warmup_cache(config_path::String)
192+
193+
Invokes warm up of regional data cache to reduce later spin up times.
194+
"""
195+
function warmup_cache(config_path::String)
196+
config = TOML.parsefile(config_path)
197+
setup_regional_data(config)
198+
end
199+
200+
148201
function start_server(config_path)
149-
@info "Launching server...please wait"
202+
@info "Launching server... please wait"
150203

151204
@info "Parsing configuration from $(config_path)..."
152205
config = TOML.parsefile(config_path)
@@ -163,6 +216,13 @@ function start_server(config_path)
163216

164217
@info "Setting up tile routes..."
165218
setup_tile_routes(auth)
219+
220+
@info "Setting up region routes..."
221+
setup_region_routes(config)
222+
@info "Completed region routes setup."
223+
224+
@info "Setting up tile routes..."
225+
setup_tile_routes()
166226
@info "Completed tile routes setup."
167227

168228
@info "Initialisation complete, starting server on port 8000."

0 commit comments

Comments
 (0)