zitadel/pkg/console/console.go
Livio Amstutz 0da6dc1d66
fix: serve console env from os (not statik) (#187)
* fix: serve console env from os (not statik)

* ZITADEL_CONSOLE_ENV_PATH for (local) overwrite possibility

* name EnvOverwritePath
2020-06-09 07:38:44 +02:00

49 lines
960 B
Go

package console
import (
"context"
"net/http"
"os"
"path"
"github.com/rakyll/statik/fs"
_ "github.com/caos/zitadel/pkg/console/statik"
)
type Config struct {
Port string
EnvOverwritePath string
}
type spaHandler struct {
fileSystem http.FileSystem
}
const (
envRequestPath = "/assets/environment.json"
)
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")
}
func Start(ctx context.Context, config Config) error {
statikFS, err := fs.NewWithNamespace("console")
if err != nil {
return err
}
envPath := envRequestPath
if config.EnvOverwritePath != "" {
envPath = config.EnvOverwritePath
}
http.Handle("/", http.FileServer(&spaHandler{statikFS}))
http.Handle(envRequestPath, http.FileServer(http.Dir(envPath)))
return http.ListenAndServe(":"+config.Port, nil)
}