Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Disable web UI via build tag or runtime flag (#47)
Browse files Browse the repository at this point in the history
* Optionally disable UI with build tag

* Add macOS and Windows to test matrix
  • Loading branch information
jlegrone authored Feb 28, 2022
1 parent 5a5ad37 commit 493a727
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 18 deletions.
22 changes: 14 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
# cgo is currently required by the upstream SQLite driver.
# This case should be un-commented if and when cgo is
# supported again.
# - cgo: "0"
- cgo: "1"
os: [macos-latest, windows-latest, ubuntu-18.04]
flags:
- ""
- -tags headless
cgo: ["0", "1"]
exclude:
# TODO: remove this exception after https://github.com/temporalio/ui-server/issues/104 is closed.
- os: windows-latest
flags: ""
# cgo is currently required by the upstream SQLite driver.
# This exception should be removed if/when non-cgo builds are supported again.
- cgo: "0"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Set up Go
Expand All @@ -43,4 +49,4 @@ jobs:
- name: Test
env:
CGO_ENABLED: ${{ matrix.cgo }}
run: go test -v ./...
run: go test -v ${{ matrix.flags }} ./...
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ temporalite start -h
### Namespace Registration

Namespaces can be pre-registered at startup so they're available to use right away:

```bash
temporalite start --namespace foo --namespace bar
```
Expand All @@ -86,3 +87,17 @@ An in-memory mode is also available. Note that all data will be lost on each res
```bash
temporalite start --ephemeral
```

### Web UI

The `temporalite` binary can be compiled to omit static assets for installations that will never use the UI:

```bash
go install -tags headless github.com/DataDog/temporalite/cmd/temporalite@latest
```

The UI can also be disabled via a runtime flag:

```bash
temporalite start --headless
```
25 changes: 15 additions & 10 deletions cmd/temporalite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"os"
"strings"

uiserver "github.com/temporalio/ui-server/server"
uiconfig "github.com/temporalio/ui-server/server/config"
uiserveroptions "github.com/temporalio/ui-server/server/server_options"
"github.com/urfave/cli/v2"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
Expand All @@ -28,6 +25,10 @@ import (
"github.com/DataDog/temporalite/internal/liteconfig"
)

// Name of the ui-server module, used in tests to verify that it is included/excluded
// as a dependency when building with the `headless` tag enabled.
const uiServerModule = "github.com/temporalio/ui-server"

var (
defaultCfg *liteconfig.Config
)
Expand All @@ -37,6 +38,7 @@ const (
dbPathFlag = "filename"
portFlag = "port"
uiPortFlag = "ui-port"
headlessFlag = "headless"
ipFlag = "ip"
logFormatFlag = "log-format"
namespaceFlag = "namespace"
Expand Down Expand Up @@ -93,6 +95,10 @@ func buildCLI() *cli.App {
Usage: "port for the temporal web UI",
DefaultText: fmt.Sprintf("--port + 1000, eg. %d", liteconfig.DefaultFrontendPort+1000),
},
&cli.BoolFlag{
Name: headlessFlag,
Usage: "disable the temporal web UI",
},
&cli.StringFlag{
Name: ipFlag,
Usage: `IPv4 address to bind the frontend service to instead of localhost`,
Expand Down Expand Up @@ -144,12 +150,6 @@ func buildCLI() *cli.App {
if c.IsSet(uiPortFlag) {
uiPort = c.Int(uiPortFlag)
}
uiOpts := uiconfig.Config{
TemporalGRPCAddress: fmt.Sprintf(":%d", c.Int(portFlag)),
Host: ip,
Port: uiPort,
EnableUI: true,
}

pragmas, err := getPragmaMap(c.StringSlice(pragmaFlag))
if err != nil {
Expand All @@ -166,7 +166,12 @@ func buildCLI() *cli.App {
temporalite.WithUpstreamOptions(
temporal.InterruptOn(temporal.InterruptCh()),
),
temporalite.WithUI(uiserver.NewServer(uiserveroptions.WithConfigProvider(&uiOpts))),
}
if !c.Bool(headlessFlag) {
opt := newUIOption(fmt.Sprintf(":%d", c.Int(portFlag)), ip, uiPort)
if opt != nil {
opts = append(opts, opt)
}
}
if c.Bool(ephemeralFlag) {
opts = append(opts, temporalite.WithPersistenceDisabled())
Expand Down
30 changes: 30 additions & 0 deletions cmd/temporalite/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build !headless

package main

import (
// This file should be the only one to import ui-server packages.
// This is to avoid embedding the UI's static assets in the binary when the `headless` build tag is enabled.
uiserver "github.com/temporalio/ui-server/server"
uiconfig "github.com/temporalio/ui-server/server/config"
uiserveroptions "github.com/temporalio/ui-server/server/server_options"

"github.com/DataDog/temporalite"
)

func newUIOption(frontendAddr string, uiIP string, uiPort int) temporalite.ServerOption {
return temporalite.WithUI(uiserver.NewServer(uiserveroptions.WithConfigProvider(newUIConfig(
frontendAddr,
uiIP,
uiPort,
))))
}

func newUIConfig(frontendAddr string, uiIP string, uiPort int) *uiconfig.Config {
return &uiconfig.Config{
TemporalGRPCAddress: frontendAddr,
Host: uiIP,
Port: uiPort,
EnableUI: true,
}
}
9 changes: 9 additions & 0 deletions cmd/temporalite/ui_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build headless

package main

import "github.com/DataDog/temporalite"

func newUIOption(frontendAddr string, uiIP string, uiPort int) temporalite.ServerOption {
return nil
}
19 changes: 19 additions & 0 deletions cmd/temporalite/ui_disabled_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build headless

package main

import (
"runtime/debug"
"testing"
)

// This test ensures that the ui-server module is not a dependency of Temporalite when built
// for headless mode.
func TestNoUIServerDependency(t *testing.T) {
info, _ := debug.ReadBuildInfo()
for _, dep := range info.Deps {
if dep.Path == uiServerModule {
t.Errorf("%s should not be a dependency when headless tag is enabled", uiServerModule)
}
}
}
30 changes: 30 additions & 0 deletions cmd/temporalite/ui_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build !headless

package main

import (
"runtime/debug"
"testing"
)

// This test ensures that ui-server is a dependency of Temporalite built in non-headless mode.
func TestHasUIServerDependency(t *testing.T) {
info, _ := debug.ReadBuildInfo()
for _, dep := range info.Deps {
if dep.Path == uiServerModule {
return
}
}
t.Errorf("%s should be a dependency when headless tag is not enabled", uiServerModule)
// If the ui-server module name is ever changed, this test should fail and indicate that the
// module name should be updated for this and the equivalent test case in ui_disabled_test.go
// to continue working.
t.Logf("Temporalite's %s dependency is missing. Was this module renamed recently?", uiServerModule)
}

func TestNewUIConfig(t *testing.T) {
cfg := newUIConfig("localhost:7233", "localhost", 8233)
if err := cfg.Validate(); err != nil {
t.Errorf("config not valid: %s", err)
}
}

0 comments on commit 493a727

Please sign in to comment.