2020-03-25 06:58:58 +00:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-13 12:41:43 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-22 11:17:29 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-05-13 12:41:43 +00:00
|
|
|
|
|
|
|
"github.com/rakyll/statik/fs"
|
2020-03-25 06:58:58 +00:00
|
|
|
|
2020-06-22 11:17:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/http/middleware"
|
2020-05-13 12:41:43 +00:00
|
|
|
_ "github.com/caos/zitadel/pkg/console/statik"
|
2020-03-25 06:58:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-06-09 06:44:55 +00:00
|
|
|
Port string
|
|
|
|
EnvOverwriteDir string
|
2020-06-22 11:17:29 +00:00
|
|
|
Cache middleware.CacheConfig
|
|
|
|
CSPDomain string
|
2020-05-13 12:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type spaHandler struct {
|
|
|
|
fileSystem http.FileSystem
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:38:44 +00:00
|
|
|
const (
|
|
|
|
envRequestPath = "/assets/environment.json"
|
2020-06-09 06:44:55 +00:00
|
|
|
envDefaultDir = "/console/"
|
2020-06-22 11:17:29 +00:00
|
|
|
|
|
|
|
manifestFile = "/manifest.webmanifest"
|
2020-06-09 05:38:44 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 12:41:43 +00:00
|
|
|
func (i *spaHandler) Open(name string) (http.File, error) {
|
|
|
|
ret, err := i.fileSystem.Open(name)
|
|
|
|
if !os.IsNotExist(err) || path.Ext(name) != "" {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.fileSystem.Open("/index.html")
|
2020-03-25 06:58:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 12:57:16 +00:00
|
|
|
func Start(ctx context.Context, config Config) error {
|
2020-06-05 05:50:04 +00:00
|
|
|
statikFS, err := fs.NewWithNamespace("console")
|
2020-05-13 12:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 06:44:55 +00:00
|
|
|
envDir := envDefaultDir
|
|
|
|
if config.EnvOverwriteDir != "" {
|
|
|
|
envDir = config.EnvOverwriteDir
|
2020-06-09 05:38:44 +00:00
|
|
|
}
|
2020-06-22 11:17:29 +00:00
|
|
|
cache := AssetsCacheInterceptorIgnoreManifest(config.Cache.MaxAge.Duration, config.Cache.SharedMaxAge.Duration)
|
|
|
|
security := middleware.SecurityHeaders(csp(config.CSPDomain), nil)
|
|
|
|
http.Handle("/", cache(security(http.FileServer(&spaHandler{statikFS}))))
|
|
|
|
http.Handle(envRequestPath, cache(security(http.StripPrefix("/assets", http.FileServer(http.Dir(envDir))))))
|
2020-05-13 12:41:43 +00:00
|
|
|
return http.ListenAndServe(":"+config.Port, nil)
|
2020-03-25 06:58:58 +00:00
|
|
|
}
|
2020-06-22 11:17:29 +00:00
|
|
|
|
|
|
|
func csp(zitadelDomain string) *middleware.CSP {
|
|
|
|
if !strings.HasPrefix(zitadelDomain, "*.") {
|
|
|
|
zitadelDomain = "*." + zitadelDomain
|
|
|
|
}
|
|
|
|
csp := middleware.DefaultSCP
|
|
|
|
csp.StyleSrc = csp.StyleSrc.AddInline().AddHost("fonts.googleapis.com").AddHost("maxst.icons8.com") //TODO: host it
|
|
|
|
csp.FontSrc = csp.FontSrc.AddHost("fonts.gstatic.com").AddHost("maxst.icons8.com") //TODO: host it
|
|
|
|
csp.ScriptSrc = csp.ScriptSrc.AddEval()
|
2020-06-23 06:43:41 +00:00
|
|
|
csp.ConnectSrc = csp.ConnectSrc.AddHost(zitadelDomain).
|
|
|
|
AddHost("fonts.googleapis.com").
|
|
|
|
AddHost("fonts.gstatic.com").
|
|
|
|
AddHost("maxst.icons8.com") //TODO: host it
|
2020-06-22 11:17:29 +00:00
|
|
|
return &csp
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssetsCacheInterceptorIgnoreManifest(maxAge, sharedMaxAge time.Duration) func(http.Handler) http.Handler {
|
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == manifestFile {
|
|
|
|
middleware.NoCacheInterceptor(handler).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
middleware.AssetsCacheInterceptor(maxAge, sharedMaxAge, handler).ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|