Set service url via parameter/env

Change-Id: I560568e8c77b3898948a7a7260e00102c996dc49
diff --git a/README.md b/README.md
index d4ec78b..0ffd983 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,7 @@
 
 - `KORAP_SERVER`: The server URL of the hosting service.
 - `PORT`: The port the service should be listen to.
+- `KORAP_EXTERNAL_PROVIDER`: The URL the service is hosted.
 
 ## License
 
diff --git a/assets/plugin.json b/assets/plugin.json
deleted file mode 100644
index 93efb67..0000000
--- a/assets/plugin.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
-  "name" : "External Provider",
-  "desc" : "Buy content from an external provider",
-  "embed" : [{
-    "panel" : "match",
-    "title" : "Full Text",
-    "classes" : ["cart"],
-    "onClick" : {
-      "action" : "addWidget",
-      "template" : "https://korap.ids-mannheim.de/instance/test/plugin/external/",
-      "permissions": [
-        "scripts",
-        "popups" 
-      ]
-    }
-  }]
-}
\ No newline at end of file
diff --git a/go.mod b/go.mod
index bf53cc8..649e774 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,8 @@
 require (
 	github.com/dgraph-io/badger/v3 v3.2103.5
 	github.com/gin-gonic/gin v1.8.1
+	github.com/joho/godotenv v1.4.0
+	github.com/mattn/go-jsonpointer v0.0.1
 	github.com/stretchr/testify v1.7.1
 )
 
@@ -25,7 +27,6 @@
 	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 0919672..566f1b9 100644
--- a/go.sum
+++ b/go.sum
@@ -82,6 +82,8 @@
 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
 github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
 github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
+github.com/mattn/go-jsonpointer v0.0.1 h1:j5m5P9BdP4B/zn6J7oH3KIQSOa2OHmcNAKEozUW7wuE=
+github.com/mattn/go-jsonpointer v0.0.1/go.mod h1:1s8vx7JSjlgVRF+LW16MPpWSRZAxyrc1/FYzOonxeao=
 github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
 github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
diff --git a/service.go b/service.go
index 0560786..6c332e3 100644
--- a/service.go
+++ b/service.go
@@ -2,6 +2,7 @@
 
 import (
 	"encoding/csv"
+	"encoding/json"
 	"io"
 	"log"
 	"net/http"
@@ -10,6 +11,7 @@
 	badger "github.com/dgraph-io/badger/v3"
 	"github.com/gin-gonic/gin"
 	"github.com/joho/godotenv"
+	"github.com/mattn/go-jsonpointer"
 )
 
 var db *badger.DB
@@ -78,6 +80,32 @@
 		korapServer = "https://korap.ids-mannheim.de"
 	}
 
+	var pluginManifest map[string]any
+	json.Unmarshal([]byte(`{
+		"name" : "External Provider",
+		"desc" : "Buy content from an external provider",
+		"embed" : [{
+			"panel" : "match",
+			"title" : "Full Text",
+			"classes" : ["plugin", "cart"],
+			"icon" : "\f07a",
+			"onClick" : {
+				"action" : "addWidget",
+				"template":"",
+				"permissions": [
+					"scripts",
+					"popups" 
+				]
+			}
+		}]
+	}`), &pluginManifest)
+
+	externalProvider := os.Getenv("KORAP_EXTERNAL_PROVIDER")
+	if externalProvider == "" {
+		externalProvider = "https://korap.ids-mannheim.de/plugin/external/"
+	}
+	jsonpointer.Set(pluginManifest, "/embed/0/onClick/template", externalProvider)
+
 	r.Use(func() gin.HandlerFunc {
 		return func(c *gin.Context) {
 			h := c.Writer.Header()
@@ -100,7 +128,10 @@
 	r.GET("/:corpus_id/:doc_id/:text_id", CheckSaleUrl)
 
 	// Return plugin manifest
-	r.StaticFile("/plugin.json", "./assets/plugin.json")
+	r.GET("/plugin.json", func(c *gin.Context) {
+		c.JSON(200, pluginManifest)
+	})
+
 	return r
 }
 
diff --git a/service_test.go b/service_test.go
index 08a576b..e32b821 100644
--- a/service_test.go
+++ b/service_test.go
@@ -75,7 +75,7 @@
 	assert.Contains(t, w.Body.String(), "<title>External Provider</title>")
 }
 
-func TestAssetRoute(t *testing.T) {
+func TestManifestRoute(t *testing.T) {
 
 	router := setupRouter()
 
@@ -85,6 +85,22 @@
 	router.ServeHTTP(w, req)
 
 	assert.Equal(t, http.StatusOK, w.Code)
-	assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
+	assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
 	assert.Contains(t, w.Body.String(), "permissions")
+	assert.Contains(t, w.Body.String(), "/plugin/external")
+
+	os.Setenv("KORAP_EXTERNAL_PROVIDER", "https://korap.ids-mannheim.de/plugin/fun")
+
+	router = setupRouter()
+
+	w = httptest.NewRecorder()
+	req, _ = http.NewRequest(http.MethodGet, "/plugin.json", nil)
+
+	router.ServeHTTP(w, req)
+
+	assert.Equal(t, http.StatusOK, w.Code)
+	assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
+	assert.Contains(t, w.Body.String(), "permissions")
+	assert.Contains(t, w.Body.String(), "/plugin/fun")
+
 }