-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
898 lines (735 loc) · 34 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//go:generate ../hack/generate-deepcopy.sh
package config
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"github.com/PDOK/gokoala/internal/engine/util"
"github.com/creasty/defaults"
"github.com/docker/go-units"
"github.com/go-playground/validator/v10"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
)
const (
CookieMaxAge = 60 * 60 * 24
)
// NewConfig read YAML config file, required to start GoKoala
func NewConfig(configFile string) (*Config, error) {
yamlData, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to read config file %w", err)
}
// expand environment variables
yamlData = []byte(os.ExpandEnv(string(yamlData)))
var config *Config
err = yaml.Unmarshal(yamlData, &config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal config file, error: %w", err)
}
err = validateLocalPaths(config)
if err != nil {
return nil, fmt.Errorf("validation error in config file, error: %w", err)
}
return config, nil
}
// UnmarshalYAML hooks into unmarshalling to set defaults and validate config
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type cfg Config
if err := unmarshal((*cfg)(c)); err != nil {
return err
}
// init config
if err := setDefaults(c); err != nil {
return err
}
if err := validate(c); err != nil {
return err
}
return nil
}
func (c *Config) UnmarshalJSON(b []byte) error {
return yaml.Unmarshal(b, c)
}
func setDefaults(config *Config) error {
// process 'default' tags
if err := defaults.Set(config); err != nil {
return fmt.Errorf("failed to set default configuration: %w", err)
}
// custom default logic
if len(config.AvailableLanguages) == 0 {
config.AvailableLanguages = append(config.AvailableLanguages, Language{language.Dutch}) // default to Dutch only
}
return nil
}
func validate(config *Config) error {
// process 'validate' tags
v := validator.New()
err := v.Struct(config)
if err != nil {
var ive *validator.InvalidValidationError
if ok := errors.Is(err, ive); ok {
return fmt.Errorf("failed to validate config: %w", err)
}
var errMessages []string
var valErrs validator.ValidationErrors
if errors.As(err, &valErrs) {
for _, valErr := range valErrs {
errMessages = append(errMessages, valErr.Error()+"\n")
}
}
return fmt.Errorf("invalid config provided:\n%v", errMessages)
}
// custom validations
if config.OgcAPI.Features != nil {
return validateCollectionsTemporalConfig(config.OgcAPI.Features.Collections)
}
return nil
}
func validateCollectionsTemporalConfig(collections GeoSpatialCollections) error {
var errMessages []string
for _, collection := range collections {
if collection.Metadata != nil && collection.Metadata.TemporalProperties != nil &&
(collection.Metadata.Extent == nil || collection.Metadata.Extent.Interval == nil) {
errMessages = append(errMessages, fmt.Sprintf("validation failed for collection '%s'; "+
"field 'Extent.Interval' is required with field 'TemporalProperties'\n", collection.ID))
}
}
if len(errMessages) > 0 {
return fmt.Errorf("invalid config provided:\n%v", errMessages)
}
return nil
}
// validateLocalPaths validates the existence of local paths.
// Not suitable for general validation while unmarshalling.
// Because that could happen on another machine.
func validateLocalPaths(config *Config) error {
// Could use a deep dive and reflection.
// But the settings with a path are not recursive and relatively limited in numbers.
// GeoPackageCloudCache.Path is not verified. It will be created anyway in cloud_sqlite_vfs.createCacheDir during startup time.
if config.Resources != nil && config.Resources.Directory != nil && *config.Resources.Directory != "" &&
!isExistingLocalDir(*config.Resources.Directory) {
return errors.New("Config.Resources.Directory should be an existing directory: " + *config.Resources.Directory)
}
if config.OgcAPI.Styles != nil && !isExistingLocalDir(config.OgcAPI.Styles.StylesDir) {
return errors.New("Config.OgcAPI.Styles.StylesDir should be an existing directory: " + config.OgcAPI.Styles.StylesDir)
}
return nil
}
func isExistingLocalDir(path string) bool {
fileInfo, err := os.Stat(path)
return err == nil && fileInfo.IsDir()
}
// +kubebuilder:object:generate=true
type Config struct {
// Version of the API. When releasing a new version which contains backwards-incompatible changes, a new major version must be released.
Version string `yaml:"version" json:"version" validate:"required,semver"`
// Human friendly title of the API. Don't include "OGC API" in the title, this is added automatically.
Title string `yaml:"title" json:"title" validate:"required"`
// Shorted title / abbreviation describing the API.
ServiceIdentifier string `yaml:"serviceIdentifier" json:"serviceIdentifier" validate:"required"`
// Human friendly description of the API and dataset.
Abstract string `yaml:"abstract" json:"abstract" validate:"required"`
// Licensing term that apply to this API and dataset
License License `yaml:"license" json:"license" validate:"required"`
// The base URL - that's the part until the OGC API landing page - under which this API is served
BaseURL URL `yaml:"baseUrl" json:"baseUrl" validate:"required"`
// Optional reference to a catalog/portal/registry that lists all datasets, not just this one
DatasetCatalogURL URL `yaml:"datasetCatalogUrl" json:"datasetCatalogUrl"`
// The languages/translations to offer, valid options are Dutch (nl) and English (en). Dutch is the default.
AvailableLanguages []Language `yaml:"availableLanguages" json:"availableLanguages"`
// Define which OGC API building blocks this API supports
OgcAPI OgcAPI `yaml:"ogcApi" json:"ogcApi" validate:"required"`
// Reference to a PNG image to use a thumbnail on the landing page.
// The full path is constructed by appending Resources + Thumbnail.
// +optional
Thumbnail *string `yaml:"thumbnail,omitempty" json:"thumbnail,omitempty"`
// Keywords to make this API beter discoverable
// +optional
Keywords []string `yaml:"keywords,omitempty" json:"keywords,omitempty"`
// Moment in time when the dataset was last updated
// +optional
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format="date-time"
LastUpdated *string `yaml:"lastUpdated,omitempty" json:"lastUpdated,omitempty" validate:"omitempty,datetime=2006-01-02T15:04:05Z"`
// Who updated the dataset
// +optional
LastUpdatedBy string `yaml:"lastUpdatedBy,omitempty" json:"lastUpdatedBy,omitempty"`
// Available support channels
// +optional
Support *Support `yaml:"support,omitempty" json:"support,omitempty"`
// Key/value pairs to add extra information to the landing page
// +optional
DatasetDetails []DatasetDetail `yaml:"datasetDetails,omitempty" json:"datasetDetails,omitempty"`
// Location where resources (e.g. thumbnails) specific to the given dataset are hosted
// +optional
Resources *Resources `yaml:"resources,omitempty" json:"resources,omitempty"`
}
func (c *Config) CookieMaxAge() int {
return CookieMaxAge
}
func (c *Config) HasCollections() bool {
return c.AllCollections() != nil
}
func (c *Config) AllCollections() GeoSpatialCollections {
var result GeoSpatialCollections
if c.OgcAPI.GeoVolumes != nil {
result = append(result, c.OgcAPI.GeoVolumes.Collections...)
}
if c.OgcAPI.Tiles != nil {
result = append(result, c.OgcAPI.Tiles.Collections...)
}
if c.OgcAPI.Features != nil {
result = append(result, c.OgcAPI.Features.Collections...)
}
return result
}
// +kubebuilder:object:generate=true
type Support struct {
// Name of the support organization
Name string `yaml:"name" json:"name" validate:"required"`
// URL to external support webpage
// +kubebuilder:validation:Type=string
URL URL `yaml:"url" json:"url" validate:"required"`
// Email for support questions
// +optional
Email string `yaml:"email,omitempty" json:"email,omitempty" validate:"omitempty,email"`
}
// +kubebuilder:object:generate=true
type DatasetDetail struct {
// Arbitrary name to add extra information to the landing page
Name string `yaml:"name" json:"name"`
// Arbitrary value associated with the given name
Value string `yaml:"value" json:"value"`
}
// +kubebuilder:object:generate=true
type Resources struct {
// Location where resources (e.g. thumbnails) specific to the given dataset are hosted. This is optional if Directory is set
// +optional
URL *URL `yaml:"url,omitempty" json:"url,omitempty" validate:"required_without=Directory,omitempty"`
// // Location where resources (e.g. thumbnails) specific to the given dataset are hosted. This is optional if URL is set
// +optional
Directory *string `yaml:"directory,omitempty" json:"directory,omitempty" validate:"required_without=URL,omitempty,dirpath|filepath"`
}
// +kubebuilder:object:generate=true
type OgcAPI struct {
// Enable when this API should offer OGC API 3D GeoVolumes. This includes OGC 3D Tiles.
// +optional
GeoVolumes *OgcAPI3dGeoVolumes `yaml:"3dgeovolumes,omitempty" json:"3dgeovolumes,omitempty"`
// Enable when this API should offer OGC API Tiles. This also requires OGC API Styles.
// +optional
Tiles *OgcAPITiles `yaml:"tiles,omitempty" json:"tiles,omitempty" validate:"required_with=Styles"`
// Enable when this API should offer OGC API Styles.
// +optional
Styles *OgcAPIStyles `yaml:"styles,omitempty" json:"styles,omitempty"`
// Enable when this API should offer OGC API Features.
// +optional
Features *OgcAPIFeatures `yaml:"features,omitempty" json:"features,omitempty"`
// Enable when this API should offer OGC API Processes.
// +optional
Processes *OgcAPIProcesses `yaml:"processes,omitempty" json:"processes,omitempty"`
}
// +kubebuilder:object:generate=true
type GeoSpatialCollection struct {
// Unique ID of the collection
ID string `yaml:"id" validate:"required" json:"id"`
// Metadata describing the collection contents
// +optional
Metadata *GeoSpatialCollectionMetadata `yaml:"metadata,omitempty" json:"metadata,omitempty"`
// 3D GeoVolumes specific to this collection
// +optional
GeoVolumes *CollectionEntry3dGeoVolumes `yaml:",inline" json:",inline"`
// Tiles specific to this collection
// +optional
Tiles *CollectionEntryTiles `yaml:",inline" json:",inline"`
// Features specific to this collection
// +optional
Features *CollectionEntryFeatures `yaml:",inline" json:",inline"`
}
type GeoSpatialCollectionJSON struct {
ID string `json:"id"`
Metadata *GeoSpatialCollectionMetadata `json:"metadata,omitempty"`
*CollectionEntry3dGeoVolumes `json:",inline"`
*CollectionEntryTiles `json:",inline"`
*CollectionEntryFeatures `json:",inline"`
}
// MarshalJSON custom because inlining only works on embedded structs.
// Value instead of pointer receiver because only that way it can be used for both.
func (c GeoSpatialCollection) MarshalJSON() ([]byte, error) {
return json.Marshal(GeoSpatialCollectionJSON{
ID: c.ID,
Metadata: c.Metadata,
CollectionEntry3dGeoVolumes: c.GeoVolumes,
CollectionEntryTiles: c.Tiles,
CollectionEntryFeatures: c.Features,
})
}
// UnmarshalJSON parses a string to GeoSpatialCollection
func (c *GeoSpatialCollection) UnmarshalJSON(b []byte) error {
return yaml.Unmarshal(b, c)
}
// +kubebuilder:object:generate=true
type GeoSpatialCollectionMetadata struct {
// Human friendly title of this collection. When no title is specified the collection ID is used.
// +optional
Title *string `yaml:"title,omitempty" json:"title,omitempty"`
// Describes the content of this collection
Description *string `yaml:"description" json:"description" validate:"required"`
// Reference to a PNG image to use a thumbnail on the collections.
// The full path is constructed by appending Resources + Thumbnail.
// +optional
Thumbnail *string `yaml:"thumbnail,omitempty" json:"thumbnail,omitempty"`
// Keywords to make this collection beter discoverable
// +optional
Keywords []string `yaml:"keywords,omitempty" json:"keywords,omitempty"`
// Moment in time when the collection was last updated
//
// +optional
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format="date-time"
LastUpdated *string `yaml:"lastUpdated,omitempty" json:"lastUpdated,omitempty" validate:"omitempty,datetime=2006-01-02T15:04:05Z"`
// Who updated this collection
// +optional
LastUpdatedBy string `yaml:"lastUpdatedBy,omitempty" json:"lastUpdatedBy,omitempty"`
// Fields in the datasource to be used in temporal queries
// +optional
TemporalProperties *TemporalProperties `yaml:"temporalProperties,omitempty" json:"temporalProperties,omitempty" validate:"omitempty,required_with=Extent.Interval"`
// Extent of the collection, both geospatial and/or temporal
// +optional
Extent *Extent `yaml:"extent,omitempty" json:"extent,omitempty"`
// The CRS identifier which the features are originally stored, meaning no CRS transformations are applied when features are retrieved in this CRS.
// WGS84 is the default storage CRS.
//
// +kubebuilder:default="http://www.opengis.net/def/crs/OGC/1.3/CRS84"
// +kubebuilder:validation:Pattern=`^http:\/\/www\.opengis\.net\/def\/crs\/.*$`
// +optional
StorageCrs *string `yaml:"storageCrs,omitempty" json:"storageCrs,omitempty" default:"http://www.opengis.net/def/crs/OGC/1.3/CRS84" validate:"startswith=http://www.opengis.net/def/crs"`
}
// +kubebuilder:object:generate=true
type CollectionEntry3dGeoVolumes struct {
// Optional basepath to 3D tiles on the tileserver. Defaults to the collection ID.
// +optional
TileServerPath *string `yaml:"tileServerPath,omitempty" json:"tileServerPath,omitempty"`
// URI template for individual 3D tiles.
// +optional
URITemplate3dTiles *string `yaml:"uriTemplate3dTiles,omitempty" json:"uriTemplate3dTiles,omitempty" validate:"required_without_all=URITemplateDTM"`
// Optional URI template for subtrees, only required when "implicit tiling" extension is used.
// +optional
URITemplateImplicitTilingSubtree *string `yaml:"uriTemplateImplicitTilingSubtree,omitempty" json:"uriTemplateImplicitTilingSubtree,omitempty"`
// URI template for digital terrain model (DTM) in Quantized Mesh format, REQUIRED when you want to serve a DTM.
// +optional
URITemplateDTM *string `yaml:"uriTemplateDTM,omitempty" json:"uriTemplateDTM,omitempty" validate:"required_without_all=URITemplate3dTiles"`
// Optional URL to 3D viewer to visualize the given collection of 3D Tiles.
// +optional
URL3DViewer *URL `yaml:"3dViewerUrl,omitempty" json:"3dViewerUrl,omitempty"`
}
func (gv *CollectionEntry3dGeoVolumes) Has3DTiles() bool {
return gv.URITemplate3dTiles != nil
}
func (gv *CollectionEntry3dGeoVolumes) HasDTM() bool {
return gv.URITemplateDTM != nil
}
// +kubebuilder:object:generate=true
type CollectionEntryTiles struct {
// placeholder
}
// +kubebuilder:object:generate=true
type CollectionEntryFeatures struct {
// Optional way to explicitly map a collection ID to the underlying table in the datasource.
// +optional
TableName *string `yaml:"tableName,omitempty" json:"tableName,omitempty"`
// Optional collection specific datasources. Mutually exclusive with top-level defined datasources.
// +optional
Datasources *Datasources `yaml:"datasources,omitempty" json:"datasources,omitempty"`
// Filters available for this collection
// +optional
Filters FeatureFilters `yaml:"filters,omitempty" json:"filters,omitempty"`
}
// +kubebuilder:object:generate=true
type FeatureFilters struct {
// OAF Part 1: filter on feature properties
// https://docs.ogc.org/is/17-069r4/17-069r4.html#_parameters_for_filtering_on_feature_properties
//
// +optional
Properties []PropertyFilter `yaml:"properties,omitempty" json:"properties,omitempty" validate:"dive"`
// OAF Part 3: add config for complex/CQL filters here
// <placeholder>
}
// +kubebuilder:object:generate=true
type OgcAPI3dGeoVolumes struct {
// Reference to the server (or object storage) hosting the 3D Tiles
TileServer URL `yaml:"tileServer" json:"tileServer" validate:"required"`
// Collections to be served as 3D GeoVolumes
Collections GeoSpatialCollections `yaml:"collections" json:"collections"`
// Whether JSON responses will be validated against the OpenAPI spec
// since it has significant performance impact when dealing with large JSON payloads.
//
// +kubebuilder:default=true
// +optional
ValidateResponses *bool `yaml:"validateResponses,omitempty" json:"validateResponses,omitempty" default:"true"` // ptr due to https://github.com/creasty/defaults/issues/49
}
// +kubebuilder:validation:Enum=raster;vector
type TilesType string
const (
TilesTypeRaster TilesType = "raster"
TilesTypeVector TilesType = "vector"
)
// +kubebuilder:object:generate=true
type OgcAPITiles struct {
// Reference to the server (or object storage) hosting the tiles
TileServer URL `yaml:"tileServer" json:"tileServer" validate:"required"`
// Could be 'vector' and/or 'raster' to indicate the types of tiles offered
Types []TilesType `yaml:"types" json:"types" validate:"required"`
// Specifies in what projections (SRS/CRS) the tiles are offered
SupportedSrs []SupportedSrs `yaml:"supportedSrs" json:"supportedSrs" validate:"required,dive"`
// Optional template to the vector tiles on the tileserver. Defaults to {tms}/{z}/{x}/{y}.pbf.
// +optional
URITemplateTiles *string `yaml:"uriTemplateTiles,omitempty" json:"uriTemplateTiles,omitempty"`
// The collections to offer as tiles. When no collection is specified the tiles are hosted at the root of the API (/tiles endpoint).
// +optional
Collections GeoSpatialCollections `yaml:"collections,omitempty" json:"collections,omitempty"`
}
// +kubebuilder:object:generate=true
type OgcAPIStyles struct {
// ID of the style to use a default
Default string `yaml:"default" json:"default" validate:"required"`
// Location on disk where the styles are hosted
StylesDir string `yaml:"stylesDir" json:"stylesDir" validate:"required,dirpath|filepath"`
// Styles exposed though this API
SupportedStyles []Style `yaml:"supportedStyles" json:"supportedStyles" validate:"required,dive"`
}
// +kubebuilder:object:generate=true
type OgcAPIFeatures struct {
// Basemap to use in embedded viewer on the HTML pages.
// +kubebuilder:default="OSM"
// +kubebuilder:validation:Enum=OSM;BRT
// +optional
Basemap string `yaml:"basemap,omitempty" json:"basemap,omitempty" default:"OSM" validate:"oneof=OSM BRT"`
// Collections to be served as features through this API
Collections GeoSpatialCollections `yaml:"collections" json:"collections" validate:"required,dive"`
// Limits the amount of features to retrieve with a single call
// +optional
Limit Limit `yaml:"limit,omitempty" json:"limit,omitempty"`
// One or more datasources to get the features from (geopackages, postgis, etc).
// Optional since you can also define datasources at the collection level
// +optional
Datasources *Datasources `yaml:"datasources,omitempty" json:"datasources,omitempty"`
// Whether GeoJSON/JSON-FG responses will be validated against the OpenAPI spec
// since it has significant performance impact when dealing with large JSON payloads.
//
// +kubebuilder:default=true
// +optional
ValidateResponses *bool `yaml:"validateResponses,omitempty" json:"validateResponses,omitempty" default:"true"` // ptr due to https://github.com/creasty/defaults/issues/49
}
func (oaf *OgcAPIFeatures) ProjectionsForCollections() []string {
return oaf.ProjectionsForCollection("")
}
func (oaf *OgcAPIFeatures) ProjectionsForCollection(collectionID string) []string {
uniqueSRSs := make(map[string]struct{})
if oaf.Datasources != nil {
for _, a := range oaf.Datasources.Additional {
uniqueSRSs[a.Srs] = struct{}{}
}
}
for _, coll := range oaf.Collections {
if (coll.ID == collectionID || collectionID == "") && coll.Features != nil && coll.Features.Datasources != nil {
for _, a := range coll.Features.Datasources.Additional {
uniqueSRSs[a.Srs] = struct{}{}
}
break
}
}
result := util.Keys(uniqueSRSs)
slices.Sort(result)
return result
}
func (oaf *OgcAPIFeatures) PropertyFiltersForCollection(collectionID string) []PropertyFilter {
for _, coll := range oaf.Collections {
if coll.ID == collectionID && coll.Features != nil && coll.Features.Filters.Properties != nil {
return coll.Features.Filters.Properties
}
}
return []PropertyFilter{}
}
// +kubebuilder:object:generate=true
type OgcAPIProcesses struct {
// Enable to advertise dismiss operations on the conformance page
SupportsDismiss bool `yaml:"supportsDismiss" json:"supportsDismiss"`
// Enable to advertise callback operations on the conformance page
SupportsCallback bool `yaml:"supportsCallback" json:"supportsCallback"`
// Reference to an external service implementing the process API. GoKoala acts only as a proxy for OGC API Processes.
ProcessesServer URL `yaml:"processesServer" json:"processesServer" validate:"required"`
}
// +kubebuilder:object:generate=true
type Limit struct {
// Number of features to return by default.
// +kubebuilder:default=10
// +kubebuilder:validation:Minimum=2
// +optional
Default int `yaml:"default,omitempty" json:"default,omitempty" validate:"gt=1" default:"10"`
// Max number of features to return. Should be larger than 100 since the HTML interface always offers a 100 limit option.
// +kubebuilder:default=1000
// +kubebuilder:validation:Minimum=100
// +optional
Max int `yaml:"max,omitempty" json:"max,omitempty" validate:"gte=100" default:"1000"`
}
// +kubebuilder:object:generate=true
type Datasources struct {
// Features should always be available in WGS84 (according to spec).
// This specifies the datasource to be used for features in the WGS84 projection
DefaultWGS84 Datasource `yaml:"defaultWGS84" json:"defaultWGS84" validate:"required"`
// One or more additional datasources for features in other projections. GoKoala doesn't do
// any on-the-fly reprojection so additional datasources need to be reprojected ahead of time.
// +optional
Additional []AdditionalDatasource `yaml:"additional" json:"additional" validate:"dive"`
}
// +kubebuilder:object:generate=true
type Datasource struct {
// GeoPackage to get the features from.
// +optional
GeoPackage *GeoPackage `yaml:"geopackage,omitempty" json:"geopackage,omitempty" validate:"required_without_all=PostGIS"`
// PostGIS database to get the features from (not implemented yet).
// +optional
PostGIS *PostGIS `yaml:"postgis,omitempty" json:"postgis,omitempty" validate:"required_without_all=GeoPackage"`
// Add more datasources here such as Mongo, Elastic, etc
}
// +kubebuilder:object:generate=true
type AdditionalDatasource struct {
// Projection (SRS/CRS) used for the features in this datasource
// +kubebuilder:validation:Pattern=`^EPSG:\d+$`
Srs string `yaml:"srs" json:"srs" validate:"required,startswith=EPSG:"`
// The additional datasource
Datasource `yaml:",inline" json:",inline"`
}
// +kubebuilder:object:generate=true
type PostGIS struct {
// placeholder
}
// +kubebuilder:object:generate=true
type GeoPackage struct {
// Settings to read a GeoPackage from local disk
// +optional
Local *GeoPackageLocal `yaml:"local,omitempty" json:"local,omitempty" validate:"required_without_all=Cloud"`
// Settings to read a GeoPackage as a Cloud-Backed SQLite database
// +optional
Cloud *GeoPackageCloud `yaml:"cloud,omitempty" json:"cloud,omitempty" validate:"required_without_all=Local"`
}
// +kubebuilder:object:generate=true
type GeoPackageCommon struct {
// Feature id column name
// +kubebuilder:default="fid"
// +optional
Fid string `yaml:"fid,omitempty" json:"fid,omitempty" validate:"required" default:"fid"`
// Optional timeout after which queries are canceled
// +kubebuilder:default="15s"
// +optional
QueryTimeout Duration `yaml:"queryTimeout,omitempty" json:"queryTimeout,omitempty" validate:"required" default:"15s"`
// When the number of features in a bbox stay within the given value use an RTree index, otherwise use a BTree index
// +kubebuilder:default=30000
// +optional
MaxBBoxSizeToUseWithRTree int `yaml:"maxBBoxSizeToUseWithRTree,omitempty" json:"maxBBoxSizeToUseWithRTree,omitempty" validate:"required" default:"30000"`
// ADVANCED SETTING. Sets the SQLite "cache_size" pragma which determines how many pages are cached in-memory.
// See https://sqlite.org/pragma.html#pragma_cache_size for details.
// Default in SQLite is 2000 pages, which equates to 2000KiB (2048000 bytes). Which is denoted as -2000.
// +kubebuilder:default=-2000
// +optional
InMemoryCacheSize int `yaml:"inMemoryCacheSize,omitempty" json:"inMemoryCacheSize,omitempty" validate:"required" default:"-2000"`
}
// +kubebuilder:object:generate=true
type GeoPackageLocal struct {
// GeoPackageCommon shared config between local and cloud GeoPackage
GeoPackageCommon `yaml:",inline" json:",inline"`
// Location of GeoPackage on disk.
// You can place the GeoPackage here manually (out-of-band) or you can specify Download
// and let the application download the GeoPackage for you and store it at this location.
File string `yaml:"file" json:"file" validate:"required,omitempty,filepath"`
// Optional initialization task to download a GeoPackage during startup. GeoPackage will be
// downloaded to local disk and stored at the location specified in File.
// +optional
Download *GeoPackageDownload `yaml:"download,omitempty" json:"download,omitempty"`
}
// +kubebuilder:object:generate=true
type GeoPackageDownload struct {
// Location of GeoPackage on remote HTTP(S) URL. GeoPackage will be downloaded to local disk
// during startup and stored at the location specified in "file".
From URL `yaml:"from" json:"from" validate:"required"`
// ADVANCED SETTING. Determines how many workers (goroutines) in parallel will download the specified GeoPackage.
// Setting this to 1 will disable concurrent downloads.
// +kubebuilder:default=4
// +kubebuilder:validation:Minimum=1
// +optional
Parallelism int `yaml:"parallelism,omitempty" json:"parallelism,omitempty" validate:"required,gte=1" default:"4"`
// ADVANCED SETTING. When true TLS certs are NOT validated, false otherwise. Only use true for your own self-signed certificates!
// +kubebuilder:default=false
// +optional
TLSSkipVerify bool `yaml:"tlsSkipVerify,omitempty" json:"tlsSkipVerify,omitempty" default:"false"`
// ADVANCED SETTING. HTTP request timeout when downloading (part of) GeoPackage.
// +kubebuilder:default="2m"
// +optional
Timeout Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required" default:"2m"`
// ADVANCED SETTING. Minimum delay to use when retrying HTTP request to download (part of) GeoPackage.
// +kubebuilder:default="1s"
// +optional
RetryDelay Duration `yaml:"retryDelay,omitempty" json:"retryDelay,omitempty" validate:"required" default:"1s"`
// ADVANCED SETTING. Maximum overall delay of the exponential backoff while retrying HTTP requests to download (part of) GeoPackage.
// +kubebuilder:default="30s"
// +optional
RetryMaxDelay Duration `yaml:"retryMaxDelay,omitempty" json:"retryMaxDelay,omitempty" validate:"required" default:"30s"`
// ADVANCED SETTING. Maximum number of retries when retrying HTTP requests to download (part of) GeoPackage.
// +kubebuilder:default=5
// +kubebuilder:validation:Minimum=1
// +optional
MaxRetries int `yaml:"maxRetries,omitempty" json:"maxRetries,omitempty" validate:"required,gte=1" default:"5"`
}
// +kubebuilder:object:generate=true
type GeoPackageCloud struct {
// GeoPackageCommon shared config between local and cloud GeoPackage
GeoPackageCommon `yaml:",inline" json:",inline"`
// Reference to the cloud storage (either azure or google at the moment).
// For example 'azure?emulator=127.0.0.1:10000&sas=0' or 'google'
Connection string `yaml:"connection" json:"connection" validate:"required"`
// Username of the storage account, like devstoreaccount1 when using Azurite
User string `yaml:"user" json:"user" validate:"required"`
// Some kind of credential like a password or key to authenticate with the storage backend, e.g:
// 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==' when using Azurite
Auth string `yaml:"auth" json:"auth" validate:"required"`
// Container/bucket on the storage account
Container string `yaml:"container" json:"container" validate:"required"`
// Filename of the GeoPackage
File string `yaml:"file" json:"file" validate:"required"`
// Local cache of fetched blocks from cloud storage
// +optional
Cache GeoPackageCloudCache `yaml:"cache,omitempty" json:"cache,omitempty"`
// Only for debug purposes! When true all HTTP requests executed by sqlite to cloud object storage are logged to stdout
// +kubebuilder:default=false
// +optional
LogHTTPRequests bool `yaml:"logHttpRequests,omitempty" json:"logHttpRequests,omitempty" default:"false"`
}
func (gc *GeoPackageCloud) CacheDir() (string, error) {
fileNameWithoutExt := strings.TrimSuffix(gc.File, filepath.Ext(gc.File))
if gc.Cache.Path != nil {
randomSuffix := strconv.Itoa(rand.Intn(99999)) //nolint:gosec // random isn't used for security purposes
return filepath.Join(*gc.Cache.Path, fileNameWithoutExt+"-"+randomSuffix), nil
}
cacheDir, err := os.MkdirTemp("", fileNameWithoutExt)
if err != nil {
return "", fmt.Errorf("failed to create tempdir to cache %s, error %w", fileNameWithoutExt, err)
}
return cacheDir, nil
}
// +kubebuilder:object:generate=true
type GeoPackageCloudCache struct {
// Optional path to directory for caching cloud-backed GeoPackage blocks, when omitted a temp dir will be used.
// +optional
Path *string `yaml:"path,omitempty" json:"path,omitempty" validate:"omitempty,dirpath|filepath"`
// Max size of the local cache. Accepts human-readable size such as 100Mb, 4Gb, 1Tb, etc. When omitted 1Gb is used.
// +kubebuilder:default="1Gb"
// +optional
MaxSize string `yaml:"maxSize,omitempty" json:"maxSize,omitempty" default:"1Gb"`
// When true a warm-up query is executed on startup which aims to fill the local cache. Does increase startup time.
// +kubebuilder:default=false
// +optional
WarmUp bool `yaml:"warmUp,omitempty" json:"warmUp,omitempty" default:"false"`
}
func (cache *GeoPackageCloudCache) MaxSizeAsBytes() (int64, error) {
return units.FromHumanSize(cache.MaxSize)
}
// +kubebuilder:object:generate=true
type PropertyFilter struct {
// Needs to match with a column name in the feature table (in the configured datasource)
Name string `yaml:"name" json:"name" validate:"required"`
// Explains this property filter
// +kubebuilder:default="Filter features by this property"
// +optional
Description string `yaml:"description,omitempty" json:"description,omitempty" default:"Filter features by this property"`
// When true the property/column in the feature table needs to be indexed. Initialization will fail
// when no index is present, when false the index check is skipped. For large tables an index is recommended!
//
// +kubebuilder:default=true
// +optional
IndexRequired *bool `yaml:"indexRequired,omitempty" json:"indexRequired,omitempty" default:"true"` // ptr due to https://github.com/creasty/defaults/issues/49
}
// +kubebuilder:object:generate=true
type SupportedSrs struct {
// Projection (SRS/CRS) used
// +kubebuilder:validation:Pattern=`^EPSG:\d+$`
Srs string `yaml:"srs" json:"srs" validate:"required,startswith=EPSG:"`
// Available zoom levels
ZoomLevelRange ZoomLevelRange `yaml:"zoomLevelRange" json:"zoomLevelRange" validate:"required"`
}
// +kubebuilder:object:generate=true
type ZoomLevelRange struct {
// Start zoom level
// +kubebuilder:validation:Minimum=0
Start int `yaml:"start" json:"start" validate:"gte=0,ltefield=End"`
// End zoom level
End int `yaml:"end" json:"end" validate:"required,gtefield=Start"`
}
// +kubebuilder:object:generate=true
type Extent struct {
// Projection (SRS/CRS) to be used. When none is provided WGS84 (http://www.opengis.net/def/crs/OGC/1.3/CRS84) is used.
// +optional
// +kubebuilder:validation:Pattern=`^EPSG:\d+$`
Srs string `yaml:"srs,omitempty" json:"srs,omitempty" validate:"omitempty,startswith=EPSG:"`
// Geospatial extent
Bbox []string `yaml:"bbox" json:"bbox"`
// Temporal extent
// +optional
// +kubebuilder:validation:MinItems=2
// +kubebuilder:validation:MaxItems=2
Interval []string `yaml:"interval,omitempty" json:"interval,omitempty" validate:"omitempty,len=2"`
}
// +kubebuilder:object:generate=true
type TemporalProperties struct {
// Name of field in datasource to be used in temporal queries as the start date
StartDate string `yaml:"startDate" json:"startDate" validate:"required"`
// Name of field in datasource to be used in temporal queries as the end date
EndDate string `yaml:"endDate" json:"endDate" validate:"required"`
}
// +kubebuilder:object:generate=true
type License struct {
// Name of the license, e.g. MIT, CC0, etc
Name string `yaml:"name" json:"name" validate:"required"`
// URL to license text on the web
URL URL `yaml:"url" json:"url" validate:"required"`
}
// +kubebuilder:object:generate=true
type Style struct {
// Unique ID of this style
ID string `yaml:"id" json:"id" validate:"required"`
// Human-friendly name of this style
Title string `yaml:"title" json:"title" validate:"required"`
// Explains what is visualized by this style
// +optional
Description *string `yaml:"description,omitempty" json:"description,omitempty"`
// Keywords to make this style better discoverable
// +optional
Keywords []string `yaml:"keywords,omitempty" json:"keywords,omitempty"`
// Moment in time when the style was last updated
// +optional
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format="date-time"
LastUpdated *string `yaml:"lastUpdated,omitempty" json:"lastUpdated,omitempty" validate:"omitempty,datetime=2006-01-02T15:04:05Z"`
// Optional version of this style
// +optional
Version *string `yaml:"version,omitempty" json:"version,omitempty"`
// Reference to a PNG image to use a thumbnail on the style metadata page.
// The full path is constructed by appending Resources + Thumbnail.
// +optional
Thumbnail *string `yaml:"thumbnail,omitempty" json:"thumbnail,omitempty"`
// This style is offered in the following formats
Formats []StyleFormat `yaml:"formats" json:"formats" validate:"required,dive"`
}
// +kubebuilder:object:generate=true
type StyleFormat struct {
// Name of the format
// +kubebuilder:default="mapbox"
// +optional
Format string `yaml:"format,omitempty" json:"format,omitempty" default:"mapbox" validate:"required,oneof=mapbox sld10"`
}