Add cors middleware
This commit is contained in:
parent
8f3fe41798
commit
5e7fb58805
2 changed files with 20 additions and 13 deletions
|
@ -84,7 +84,7 @@ The name and related link can be provided as well.
|
|||
|
||||
```toml
|
||||
PORT = 4000
|
||||
PAGE_URL = "http://localhost:4000"
|
||||
ALLOWED_HOSTS = "*"
|
||||
TITLE = "GoDash"
|
||||
|
||||
LOG_LEVEL = "info"
|
||||
|
@ -115,7 +115,8 @@ services:
|
|||
- PUID=1000
|
||||
- PGID=1000
|
||||
- TZ=Europe/Berlin
|
||||
- PAGE_URL=https://home.example.com
|
||||
# allowed hosts for cors, seperated by comma
|
||||
- ALLOWED_HOSTS=https://home.example.com,https://another.example.com
|
||||
# change title to something else
|
||||
- TITLE=GoDash
|
||||
# available log-levels: debug,info,warn,error,panic,fatal
|
||||
|
|
28
main.go
28
main.go
|
@ -13,7 +13,6 @@ import (
|
|||
"godash/weather"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
@ -34,11 +33,11 @@ type info struct {
|
|||
}
|
||||
|
||||
type config struct {
|
||||
Title string `env:"TITLE" envDefault:"goDash"`
|
||||
Port int `env:"PORT" envDefault:"4000"`
|
||||
PageUrl url.URL `env:"PAGE_URL" envDefault:"http://localhost:4000"`
|
||||
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
|
||||
LiveSystem bool `env:"LIVE_SYSTEM" envDefault:"true"`
|
||||
Title string `env:"TITLE" envDefault:"goDash"`
|
||||
Port int `env:"PORT" envDefault:"4000"`
|
||||
AllowedHosts []string `env:"ALLOWED_HOSTS" envDefault:"*" envSeparator:","`
|
||||
LogLevel string `env:"LOG_LEVEL" envDefault:"info"`
|
||||
LiveSystem bool `env:"LIVE_SYSTEM" envDefault:"true"`
|
||||
}
|
||||
|
||||
func (g *goDash) createInfoServices() {
|
||||
|
@ -50,6 +49,17 @@ func (g *goDash) createInfoServices() {
|
|||
}
|
||||
}
|
||||
|
||||
func (g *goDash) setupMiddlewares() {
|
||||
g.router.Use(middleware.Recover())
|
||||
g.router.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
|
||||
g.router.Pre(middleware.RemoveTrailingSlash())
|
||||
g.router.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: g.config.AllowedHosts,
|
||||
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
|
||||
AllowMethods: []string{echo.GET},
|
||||
}))
|
||||
}
|
||||
|
||||
func main() {
|
||||
g := goDash{router: echo.New()}
|
||||
g.router.Renderer = &TemplateRenderer{
|
||||
|
@ -64,11 +74,7 @@ func main() {
|
|||
_ = logger.Sync()
|
||||
}(g.logger)
|
||||
g.setupEchoLogging()
|
||||
|
||||
g.router.Use(middleware.Recover())
|
||||
g.router.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
|
||||
g.router.Pre(middleware.RemoveTrailingSlash())
|
||||
|
||||
g.setupMiddlewares()
|
||||
g.createInfoServices()
|
||||
g.router.GET("/", g.index)
|
||||
g.router.GET("/ws", g.ws)
|
||||
|
|
Loading…
Add table
Reference in a new issue