mirror of
https://github.com/shouptech/tempgopher.git
synced 2026-02-03 08:39:43 +00:00
This adds CORS headers based on configuration.
This commit is contained in:
parent
71cf5fc46d
commit
8b3093d0f1
7 changed files with 24 additions and 3 deletions
|
|
@ -27,6 +27,7 @@ scp temp-gopher <raspberrypi>:~/somepath
|
|||
Create a `config.yml` file like this:
|
||||
|
||||
```
|
||||
baseurl: http://<pihostname>: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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -21,3 +21,4 @@ sensors:
|
|||
coolinvert: false
|
||||
coolminutes: 10
|
||||
verbose: true
|
||||
baseurl: https://foo.bar
|
||||
|
|
|
|||
|
|
@ -21,3 +21,4 @@ sensors:
|
|||
coolinvert: false
|
||||
coolminutes: 10
|
||||
verbose: true
|
||||
baseurl: https://foo.bar
|
||||
|
|
|
|||
|
|
@ -10,3 +10,4 @@ sensors:
|
|||
coolinvert: false
|
||||
coolminutes: 10
|
||||
verbose: true
|
||||
baseurl: https://foo.bar
|
||||
|
|
|
|||
17
web.go
17
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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue