zitadel/pkg/console/console.go
Livio Amstutz 5e931a3c13
fix: console env directory (#189)
* fix: serve console env from os (not statik)

* ZITADEL_CONSOLE_ENV_PATH for (local) overwrite possibility

* name EnvOverwritePath

* fix: console env directory

* fix mount path

* fix default dir
2020-06-09 08:44:55 +02:00

50 lines
1011 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
EnvOverwriteDir string
}
type spaHandler struct {
fileSystem http.FileSystem
}
const (
envRequestPath = "/assets/environment.json"
envDefaultDir = "/console/"
)
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
}
envDir := envDefaultDir
if config.EnvOverwriteDir != "" {
envDir = config.EnvOverwriteDir
}
http.Handle("/", http.FileServer(&spaHandler{statikFS}))
http.Handle(envRequestPath, http.StripPrefix("/assets", http.FileServer(http.Dir(envDir))))
return http.ListenAndServe(":"+config.Port, nil)
}