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

Commit 55953f1

Browse files
Merge pull request #16 from open-AIMS/fix-auth
Fix auth middleware
2 parents 852ade6 + aa047c1 commit 55953f1

File tree

5 files changed

+21
-36
lines changed

5 files changed

+21
-36
lines changed

sample.config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
PREPPED_DATA_DIR = "/data"
33

44
[server_config]
5-
CACHE_DIR = "./cache"
5+
TIFF_CACHE_DIR = "<some location to cache geotiffs>"
6+
REGIONAL_CACHE_DIR = "<some location to cache regional datasets>"
67
DEBUG_MODE = "false"
78
COG_THREADS = "2" # Optional, Number of threads to use when creating COGs (defaults to 1)
89
TILE_SIZE = "256" # Optional, tile block size to use (defaults to 256)

src/Middleware.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function setup_jwt_middleware(config::Dict)
4040
public_key = jwks_json["keys"][1]["n"]
4141
rsa_public = JSONWebTokens.RS256(public_key)
4242

43-
function jwt_auth_middleware(handler)
43+
return function jwt_auth_middleware(handler)
4444
return function(req::HTTP.Request)
4545
auth_header = HTTP.header(req, "Authorization", "")
4646
if !startswith(auth_header, "Bearer ")

src/ReefGuideAPI.jl

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ function tile_size(config::Dict)::Tuple
183183
return tile_dims
184184
end
185185

186-
function get_auth_middleware(config :: Dict)
186+
function get_auth_router(config::Dict)
187187
# Setup auth middleware - depends on config.toml - can return identity func
188188
auth = setup_jwt_middleware(config)
189-
return [auth]
189+
return router(""; middleware=[auth])
190190
end
191191

192192
"""
@@ -205,35 +205,25 @@ function start_server(config_path)
205205

206206
@info "Parsing configuration from $(config_path)..."
207207
config = TOML.parsefile(config_path)
208-
@info "Successfully parsed configuration."
209208

210-
# setting up middleware
211-
@info "Setting up middleware."
212-
auth = get_middleware(config)
213-
@info "Done."
209+
@info "Setting up auth middleware and router."
210+
auth = get_auth_router(config)
214211

215212
@info "Setting up region routes..."
216213
setup_region_routes(config, auth)
217-
@info "Completed region routes setup."
218214

219215
@info "Setting up tile routes..."
220216
setup_tile_routes(auth)
221217

222-
@info "Setting up region routes..."
223-
setup_region_routes(config)
224-
@info "Completed region routes setup."
218+
port = 8000
219+
@info "Initialisation complete, starting server on port $(port) with $(Threads.nthreads()) threads."
225220

226-
@info "Setting up tile routes..."
227-
setup_tile_routes()
228-
@info "Completed tile routes setup."
229-
230-
@info "Initialisation complete, starting server on port 8000."
231-
@info "Starting with $(Threads.nthreads()) threads..."
232-
if Threads.nthreads() > 1
233-
serveparallel(middleware=[CorsMiddleware], host="0.0.0.0", port=8000)
234-
else
235-
serve(middleware=[CorsMiddleware], host="0.0.0.0", port=8000)
236-
end
221+
serve(
222+
middleware=[CorsMiddleware],
223+
host="0.0.0.0",
224+
port=port,
225+
parallel=Threads.nthreads() > 1
226+
)
237227
end
238228

239229
export

src/assessment/criteria.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ end
9999
function setup_region_routes(config, auth)
100100
reg_assess_data = setup_regional_data(config)
101101

102-
@get("/assess/{reg}/{rtype}", middleware=[auth])
103-
function (req::Request, reg::String, rtype::String)
102+
@get auth("/assess/{reg}/{rtype}") function (req::Request, reg::String, rtype::String)
104103
qp = queryparams(req)
105104
file_id = string(hash(qp))
106105
mask_temp_path = _cache_location(config)
@@ -155,15 +154,13 @@ function setup_region_routes(config, auth)
155154
return file(mask_path)
156155
end
157156

158-
@get("/bounds/{reg}", middleware=[auth])
159-
function (req::Request, reg::String)
157+
@get auth("/bounds/{reg}") function (req::Request, reg::String)
160158
rst_stack = reg_assess_data[reg].stack
161159

162160
return json(Rasters.bounds(rst_stack))
163161
end
164162

165-
@get("/tile/{z}/{x}/{y}",middleware=[auth])
166-
function (req::Request, z::Int64, x::Int64, y::Int64)
163+
@get auth("/tile/{z}/{x}/{y}") function (req::Request, z::Int64, x::Int64, y::Int64)
167164
# http://127.0.0.1:8000/tile/10/10/10?region=Cairns-Cooktown&rtype=slopes&criteria_names=Depth,Slope,Rugosity&lb=-9.0,0.0,0.0&ub=-2.0,40.0,0.0
168165
qp = queryparams(req)
169166
file_id = string(hash(qp))
@@ -262,8 +259,7 @@ function setup_region_routes(config, auth)
262259
end
263260

264261
# Parse the form data and return it
265-
@post("/form", middleware=[auth])
266-
function(req)
262+
@post auth("/form") function(req)
267263
data = formdata(req)
268264
return data
269265
end

src/assessment/tiles.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,12 @@ function masked_nearest(
157157
end
158158

159159
function setup_tile_routes(auth)
160-
@get("/to-tile/{zoom}/{lon}/{lat}", middleware=[auth])
161-
function (req::Request, zoom::Int64, lon::Float64, lat::Float64)
160+
@get auth("/to-tile/{zoom}/{lon}/{lat}") function (req::Request, zoom::Int64, lon::Float64, lat::Float64)
162161
x, y = _lon_lat_to_tile(zoom, lon, lat)
163162
return json(Dict(:x=>x, :y=>y))
164163
end
165164

166-
@get("/to-lonlat/{zoom}/{x}/{y}", middleware=[auth])
167-
function (req::Request, zoom::Int64, x::Int64, y::Int64)
165+
@get auth("/to-lonlat/{zoom}/{x}/{y}") function (req::Request, zoom::Int64, x::Int64, y::Int64)
168166
lon_min, lon_max, lat_max, lat_min = _tile_bounds(zoom, x, y)
169167
return json(
170168
Dict(

0 commit comments

Comments
 (0)