Skip to content

Commit

Permalink
feat(CREATE): add configuration option to enable/disable database writes
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitdm-oslandia committed Oct 11, 2022
1 parent d9c8230 commit a81e09e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
3 changes: 3 additions & 0 deletions config/pg_featureserv.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ WriteTimeoutSec = 30
# Publish functions from these schemas (default is publish postgisftw)
# FunctionIncludes = [ "postgisftw", "schema2" ]

# Allow write changes to database. Default is to read only.
# AllowWrite = false

[Paging]
# The default number of features in a response
LimitDefault = 20
Expand Down
3 changes: 3 additions & 0 deletions hugo/content/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ WriteTimeoutSec = 30
# Publish functions from these schemas (default is publish postgisftw)
# FunctionIncludes = [ "postgisftw", "schema2" ]

# Allow write changes to database. Default is to read only.
# AllowWrite = false

[Paging]
# The default number of features in a response
LimitDefault = 20
Expand Down
2 changes: 2 additions & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func setDefaultConfig() {
viper.SetDefault("Database.TableIncludes", []string{})
viper.SetDefault("Database.TableExcludes", []string{})
viper.SetDefault("Database.FunctionIncludes", []string{"postgisftw"})
viper.SetDefault("Database.AllowWrite", false)

viper.SetDefault("Paging.LimitDefault", 10)
viper.SetDefault("Paging.LimitMax", 1000)
Expand Down Expand Up @@ -94,6 +95,7 @@ type Database struct {
TableIncludes []string
TableExcludes []string
FunctionIncludes []string
AllowWrite bool
}

// Metadata config
Expand Down
20 changes: 3 additions & 17 deletions internal/service/db_test/handler_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/CrunchyData/pg_featureserv/internal/api"
"github.com/CrunchyData/pg_featureserv/internal/conf"
"github.com/CrunchyData/pg_featureserv/internal/data"
"github.com/CrunchyData/pg_featureserv/internal/service"
"github.com/CrunchyData/pg_featureserv/util"
Expand All @@ -33,6 +34,8 @@ type geojsonFeatureData struct {
}

func TestMain(m *testing.M) {
conf.Configuration.Database.AllowWrite = true

db = util.CreateTestDb()
defer util.CloseTestDb(db)

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

func TestTestPropertiesAllFromDb(t *testing.T) {
/*rr := hTest.DoRequest(t, "/collections/mock_a/items?limit=2")
var v FeatureCollection
errUnMarsh := json.Unmarshal(hTest.ReadBody(rr), &v)
util.Assert(t, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))
// Note that JSON numbers are read as float64
util.Equals(t, 2, len(v.Features), "# features")
util.Equals(t, 4, len(v.Features[0].Props), "feature 1 # properties")
util.Equals(t, "propA", v.Features[0].Props["prop_a"], "feature 1 # property A")
util.Equals(t, 1.0, v.Features[0].Props["prop_b"], "feature 1 # property B")
util.Equals(t, "propC", v.Features[0].Props["prop_c"], "feature 1 # property C")
util.Equals(t, 1.0, v.Features[0].Props["prop_d"], "feature 1 # property D")*/
}

func TestCreateFeatureWithBadGeojsonInputDb(t *testing.T) {
var header = make(http.Header)
header.Add("Content-Type", "application/geo+json")
Expand Down
7 changes: 5 additions & 2 deletions internal/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ func InitRouter(basePath string) *mux.Router {

addRoute(router, "/collections/{id}/items", handleCollectionItems)
addRoute(router, "/collections/{id}/items.{fmt}", handleCollectionItems)
addRouteWithMethod(router, "/collections/{id}/items", handleCreateCollectionItem, "POST")

addRoute(router, "/collections/{id}/schema", handleCollectionSchemas)
if conf.Configuration.Database.AllowWrite {
addRouteWithMethod(router, "/collections/{id}/items", handleCreateCollectionItem, "POST")

addRoute(router, "/collections/{id}/schema", handleCollectionSchemas)
}

addRoute(router, "/collections/{id}/items/{fid}", handleItem)
addRoute(router, "/collections/{id}/items/{fid}.{fmt}", handleItem)
Expand Down
3 changes: 3 additions & 0 deletions internal/service/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/CrunchyData/pg_featureserv/internal/api"
"github.com/CrunchyData/pg_featureserv/internal/conf"
"github.com/CrunchyData/pg_featureserv/internal/data"
"github.com/CrunchyData/pg_featureserv/util"
)
Expand Down Expand Up @@ -51,6 +52,8 @@ var hTest util.HttpTesting
var catalogMock *data.CatalogMock

func TestMain(m *testing.M) {
conf.Configuration.Database.AllowWrite = true

catalogMock = data.CatMockInstance()
catalogInstance = catalogMock

Expand Down

0 comments on commit a81e09e

Please sign in to comment.