diff --git a/README.md b/README.md index 2e4248e..9dfda12 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ scp temp-gopher :~/somepath Create a `config.yml` file like this: ``` +baseurl: http://:8080 # Base URL to find the app at. Usually your Pi's IP address or hostname, unless using a reverse proxy sensors: - id: 28-000008083108 # Id of the DS18b120 sensor alias: fermenter # An alias for the sensor diff --git a/config.go b/config.go index 9e8ffeb..070b40f 100644 --- a/config.go +++ b/config.go @@ -25,6 +25,7 @@ type Sensor struct { // Config contains the applications configuration type Config struct { Sensors []Sensor `yaml:"sensors"` + BaseURL string `yaml:"baseurl"` } // LoadConfig will loads a file and parses it into a Config struct diff --git a/config_test.go b/config_test.go index 90ad73f..02bf022 100644 --- a/config_test.go +++ b/config_test.go @@ -21,7 +21,10 @@ func Test_LoadConfig(t *testing.T) { Verbose: true, } - testConfig := Config{Sensors: []Sensor{testSensor}} + testConfig := Config{ + Sensors: []Sensor{testSensor}, + BaseURL: "https://foo.bar", + } loadedConfig, err := LoadConfig("tests/test_config.yml") assert.Equal(t, nil, err) diff --git a/tests/duplicate_alias.yml b/tests/duplicate_alias.yml index 7019745..91b1489 100644 --- a/tests/duplicate_alias.yml +++ b/tests/duplicate_alias.yml @@ -21,3 +21,4 @@ sensors: coolinvert: false coolminutes: 10 verbose: true +baseurl: https://foo.bar diff --git a/tests/duplicate_id.yml b/tests/duplicate_id.yml index f63867d..8eb2dd9 100644 --- a/tests/duplicate_id.yml +++ b/tests/duplicate_id.yml @@ -21,3 +21,4 @@ sensors: coolinvert: false coolminutes: 10 verbose: true +baseurl: https://foo.bar diff --git a/tests/test_config.yml b/tests/test_config.yml index f2ac9d2..fbe1ada 100644 --- a/tests/test_config.yml +++ b/tests/test_config.yml @@ -10,3 +10,4 @@ sensors: coolinvert: false coolminutes: 10 verbose: true +baseurl: https://foo.bar diff --git a/web.go b/web.go index 0bb7e1d..49bee31 100644 --- a/web.go +++ b/web.go @@ -10,6 +10,7 @@ import ( "syscall" "time" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) @@ -18,7 +19,7 @@ func PingHandler(c *gin.Context) { c.String(http.StatusOK, "pong") } -// ConfigHandler responds to get requests with the current configuration +// ConfigHandler responds to get requests with the current configuration. func ConfigHandler(config *Config) gin.HandlerFunc { fn := func(c *gin.Context) { if c.Param("alias") != "/" && c.Param("alias") != "" { @@ -57,9 +58,21 @@ func StatusHandler(states *map[string]State) gin.HandlerFunc { // SetupRouter initializes the gin router. func SetupRouter(config *Config, states *map[string]State) *gin.Engine { + // If not specified, put gin in release mode + if _, ok := os.LookupEnv("GIN_MODE"); !ok { + gin.SetMode(gin.ReleaseMode) + } r := gin.Default() - gin.SetMode(gin.ReleaseMode) + // Midleware + r.Use(gin.Recovery()) + if gin.Mode() != "release" { + r.Use(cors.Default()) + } else { + corsconf := cors.DefaultConfig() + corsconf.AllowOrigins = []string{config.BaseURL} + r.Use(cors.New(corsconf)) + } // Ping r.GET("/ping", PingHandler)