2020-03-25 06:58:58 +00:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-13 12:41:43 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/rakyll/statik/fs"
|
2020-03-25 06:58:58 +00:00
|
|
|
|
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-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-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-05-13 12:41:43 +00:00
|
|
|
http.Handle("/", http.FileServer(&spaHandler{statikFS}))
|
2020-06-09 06:44:55 +00:00
|
|
|
http.Handle(envRequestPath, 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
|
|
|
}
|