Skip to content

Commit a81e09e

Browse files
feat(CREATE): add configuration option to enable/disable database writes
1 parent d9c8230 commit a81e09e

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

config/pg_featureserv.toml.example

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ WriteTimeoutSec = 30
6868
# Publish functions from these schemas (default is publish postgisftw)
6969
# FunctionIncludes = [ "postgisftw", "schema2" ]
7070

71+
# Allow write changes to database. Default is to read only.
72+
# AllowWrite = false
73+
7174
[Paging]
7275
# The default number of features in a response
7376
LimitDefault = 20

hugo/content/installation/configuration.md

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ WriteTimeoutSec = 30
118118
# Publish functions from these schemas (default is publish postgisftw)
119119
# FunctionIncludes = [ "postgisftw", "schema2" ]
120120

121+
# Allow write changes to database. Default is to read only.
122+
# AllowWrite = false
123+
121124
[Paging]
122125
# The default number of features in a response
123126
LimitDefault = 20

internal/conf/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func setDefaultConfig() {
4444
viper.SetDefault("Database.TableIncludes", []string{})
4545
viper.SetDefault("Database.TableExcludes", []string{})
4646
viper.SetDefault("Database.FunctionIncludes", []string{"postgisftw"})
47+
viper.SetDefault("Database.AllowWrite", false)
4748

4849
viper.SetDefault("Paging.LimitDefault", 10)
4950
viper.SetDefault("Paging.LimitMax", 1000)
@@ -94,6 +95,7 @@ type Database struct {
9495
TableIncludes []string
9596
TableExcludes []string
9697
FunctionIncludes []string
98+
AllowWrite bool
9799
}
98100

99101
// Metadata config

internal/service/db_test/handler_db_test.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313

1414
"github.com/CrunchyData/pg_featureserv/internal/api"
15+
"github.com/CrunchyData/pg_featureserv/internal/conf"
1516
"github.com/CrunchyData/pg_featureserv/internal/data"
1617
"github.com/CrunchyData/pg_featureserv/internal/service"
1718
"github.com/CrunchyData/pg_featureserv/util"
@@ -33,6 +34,8 @@ type geojsonFeatureData struct {
3334
}
3435

3536
func TestMain(m *testing.M) {
37+
conf.Configuration.Database.AllowWrite = true
38+
3639
db = util.CreateTestDb()
3740
defer util.CloseTestDb(db)
3841

@@ -50,23 +53,6 @@ func TestProperDbInit(t *testing.T) {
5053
util.Equals(t, 2, len(tables), "# table in DB")
5154
}
5255

53-
func TestTestPropertiesAllFromDb(t *testing.T) {
54-
/*rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2")
55-
56-
var v FeatureCollection
57-
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
58-
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
59-
60-
// Note that JSON numbers are read as float64
61-
util.Equals(t, 2, len(v.Features), "# features")
62-
util.Equals(t, 4, len(v.Features[0].Props), "feature 1 # properties")
63-
64-
util.Equals(t, "propA", v.Features[0].Props["prop_a"], "feature 1 # property A")
65-
util.Equals(t, 1.0, v.Features[0].Props["prop_b"], "feature 1 # property B")
66-
util.Equals(t, "propC", v.Features[0].Props["prop_c"], "feature 1 # property C")
67-
util.Equals(t, 1.0, v.Features[0].Props["prop_d"], "feature 1 # property D")*/
68-
}
69-
7056
func TestCreateFeatureWithBadGeojsonInputDb(t *testing.T) {
7157
var header = make(http.Header)
7258
header.Add("Content-Type", "application/geo+json")

internal/service/handler.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ func InitRouter(basePath string) *mux.Router {
6161

6262
addRoute(router, "/collections/{id}/items", handleCollectionItems)
6363
addRoute(router, "/collections/{id}/items.{fmt}", handleCollectionItems)
64-
addRouteWithMethod(router, "/collections/{id}/items", handleCreateCollectionItem, "POST")
6564

66-
addRoute(router, "/collections/{id}/schema", handleCollectionSchemas)
65+
if conf.Configuration.Database.AllowWrite {
66+
addRouteWithMethod(router, "/collections/{id}/items", handleCreateCollectionItem, "POST")
67+
68+
addRoute(router, "/collections/{id}/schema", handleCollectionSchemas)
69+
}
6770

6871
addRoute(router, "/collections/{id}/items/{fid}", handleItem)
6972
addRoute(router, "/collections/{id}/items/{fid}.{fmt}", handleItem)

internal/service/handler_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525

2626
"github.com/CrunchyData/pg_featureserv/internal/api"
27+
"github.com/CrunchyData/pg_featureserv/internal/conf"
2728
"github.com/CrunchyData/pg_featureserv/internal/data"
2829
"github.com/CrunchyData/pg_featureserv/util"
2930
)
@@ -51,6 +52,8 @@ var hTest util.HttpTesting
5152
var catalogMock *data.CatalogMock
5253

5354
func TestMain(m *testing.M) {
55+
conf.Configuration.Database.AllowWrite = true
56+
5457
catalogMock = data.CatMockInstance()
5558
catalogInstance = catalogMock
5659

0 commit comments

Comments
 (0)