Support configuration via environment file
Change-Id: I9c6bc5a418fce8f4c4ed2984d913032dce3042ff
diff --git a/go.mod b/go.mod
index c17a7d8..bf53cc8 100644
--- a/go.mod
+++ b/go.mod
@@ -25,6 +25,7 @@
github.com/golang/protobuf v1.5.0 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
+ github.com/joho/godotenv v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
diff --git a/go.sum b/go.sum
index 3deb7ef..0919672 100644
--- a/go.sum
+++ b/go.sum
@@ -61,6 +61,8 @@
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
+github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
diff --git a/service.go b/service.go
index 3ed40e7..6436ddd 100644
--- a/service.go
+++ b/service.go
@@ -9,6 +9,7 @@
badger "github.com/dgraph-io/badger/v3"
"github.com/gin-gonic/gin"
+ "github.com/joho/godotenv"
)
var db *badger.DB
@@ -76,13 +77,18 @@
r := gin.Default()
r.LoadHTMLGlob("templates/*")
+ korapServer := os.Getenv("KORAP_SERVER")
+ if korapServer == "" {
+ korapServer = "https://korap.ids-mannheim.de"
+ }
+
//
r.GET("/", func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "null")
c.Header("Access-Control-Allow-Credentials", "null")
c.Header("Vary", "Origin")
c.HTML(http.StatusOK, "main.html", gin.H{
- "korapServer": "https://korap.ids-mannheim.de/instance/test",
+ "korapServer": korapServer,
})
})
@@ -93,6 +99,8 @@
}
func main() {
+ godotenv.Load()
+
initDB("db")
defer closeDB()
@@ -139,5 +147,11 @@
return
}
r := setupRouter()
- log.Fatal(http.ListenAndServe(":5722", r))
+
+ port := os.Getenv("PORT")
+ if port == "" {
+ port = "5722"
+ }
+
+ log.Fatal(http.ListenAndServe(":"+port, r))
}
diff --git a/service_test.go b/service_test.go
index 8e27b56..7304ae7 100644
--- a/service_test.go
+++ b/service_test.go
@@ -3,6 +3,7 @@
import (
"net/http"
"net/http/httptest"
+ "os"
"testing"
"github.com/stretchr/testify/assert"
@@ -51,6 +52,19 @@
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
+ assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de\"")
+ assert.Contains(t, w.Body.String(), "<title>External Provider</title>")
+
+ os.Setenv("KORAP_SERVER", "https://korap.ids-mannheim.de/instance/test")
+
+ router = setupRouter()
+
+ w = httptest.NewRecorder()
+ req, _ = http.NewRequest(http.MethodGet, "/", nil)
+
+ router.ServeHTTP(w, req)
+
+ assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "data-server=\"https://korap.ids-mannheim.de/instance/test\"")
assert.Contains(t, w.Body.String(), "<title>External Provider</title>")
}