Skip to content

Commit

Permalink
Merge pull request #7 from owncloud/initial-phoenix
Browse files Browse the repository at this point in the history
Embed phoenix and serve it
  • Loading branch information
tboerger authored Sep 11, 2019
2 parents 1405272 + 39de6f6 commit 345a290
Show file tree
Hide file tree
Showing 26 changed files with 2,094 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ coverage.out

/bin
/dist

/phoenix
43 changes: 19 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ DIST := dist

ifeq ($(OS), Windows_NT)
EXECUTABLE := $(NAME).exe
HAS_GORUNPKG := $(shell where gorunpkg)
else
EXECUTABLE := $(NAME)
HAS_GORUNPKG := $(shell command -v gorunpkg)
endif

PACKAGES ?= $(shell go list ./...)
SOURCES ?= $(shell find . -name "*.go" -type f)
GENERATE ?= $(PACKAGES)
GENERATE ?= $(IMPORT)/pkg/assets

TAGS ?=

Expand Down Expand Up @@ -50,7 +48,7 @@ sync:
.PHONY: clean
clean:
go clean -i ./...
rm -rf $(BIN) $(DIST)
rm -rf $(BIN) $(DIST) phoenix/

.PHONY: fmt
fmt:
Expand All @@ -61,24 +59,24 @@ vet:
go vet $(PACKAGES)

.PHONY: staticcheck
staticcheck: gorunpkg
gorunpkg honnef.co/go/tools/cmd/staticcheck -tags '$(TAGS)' $(PACKAGES)
staticcheck:
go run honnef.co/go/tools/cmd/staticcheck -tags '$(TAGS)' $(PACKAGES)

.PHONY: lint
lint: gorunpkg
for PKG in $(PACKAGES); do gorunpkg golang.org/x/lint/golint -set_exit_status $$PKG || exit 1; done;
lint:
for PKG in $(PACKAGES); do go run golang.org/x/lint/golint -set_exit_status $$PKG || exit 1; done;

.PHONY: generate
generate: gorunpkg
generate: phoenix
go generate $(GENERATE)

.PHONY: changelog
changelog: gorunpkg
gorunpkg github.com/restic/calens >| CHANGELOG.md
changelog:
go run github.com/restic/calens >| CHANGELOG.md

.PHONY: test
test: gorunpkg
gorunpkg github.com/haya14busa/goverage -v -coverprofile coverage.out $(PACKAGES)
test:
go run github.com/haya14busa/goverage -v -coverprofile coverage.out $(PACKAGES)

.PHONY: install
install: $(SOURCES)
Expand All @@ -98,16 +96,16 @@ release-dirs:
mkdir -p $(DIST)/binaries $(DIST)/release

.PHONY: release-linux
release-linux: gorunpkg release-dirs
gorunpkg github.com/mitchellh/gox -tags 'netgo $(TAGS)' -ldflags '-extldflags "-static" $(LDFLAGS)' -os 'linux' -arch 'amd64 386 arm64 arm' -output '$(DIST)/binaries/$(EXECUTABLE)-$(OUTPUT)-{{.OS}}-{{.Arch}}' ./cmd/$(NAME)
release-linux: release-dirs
go run github.com/mitchellh/gox -tags 'netgo $(TAGS)' -ldflags '-extldflags "-static" $(LDFLAGS)' -os 'linux' -arch 'amd64 386 arm64 arm' -output '$(DIST)/binaries/$(EXECUTABLE)-$(OUTPUT)-{{.OS}}-{{.Arch}}' ./cmd/$(NAME)

.PHONY: release-windows
release-windows: gorunpkg release-dirs
gorunpkg github.com/mitchellh/gox -tags 'netgo $(TAGS)' -ldflags '-extldflags "-static" $(LDFLAGS)' -os 'windows' -arch 'amd64' -output '$(DIST)/binaries/$(EXECUTABLE)-$(OUTPUT)-{{.OS}}-{{.Arch}}' ./cmd/$(NAME)
release-windows: release-dirs
go run github.com/mitchellh/gox -tags 'netgo $(TAGS)' -ldflags '-extldflags "-static" $(LDFLAGS)' -os 'windows' -arch 'amd64' -output '$(DIST)/binaries/$(EXECUTABLE)-$(OUTPUT)-{{.OS}}-{{.Arch}}' ./cmd/$(NAME)

.PHONY: release-darwin
release-darwin: gorunpkg release-dirs
gorunpkg github.com/mitchellh/gox -tags 'netgo $(TAGS)' -ldflags '$(LDFLAGS)' -os 'darwin' -arch 'amd64' -output '$(DIST)/binaries/$(EXECUTABLE)-$(OUTPUT)-{{.OS}}-{{.Arch}}' ./cmd/$(NAME)
release-darwin: release-dirs
go run github.com/mitchellh/gox -tags 'netgo $(TAGS)' -ldflags '$(LDFLAGS)' -os 'darwin' -arch 'amd64' -output '$(DIST)/binaries/$(EXECUTABLE)-$(OUTPUT)-{{.OS}}-{{.Arch}}' ./cmd/$(NAME)

.PHONY: release-copy
release-copy:
Expand All @@ -120,8 +118,5 @@ release-check:
.PHONY: release-finish
release-finish: release-copy release-check

.PHONY: gorunpkg
gorunpkg:
ifndef HAS_GORUNPKG
go get -u github.com/vektah/gorunpkg
endif
phoenix:
mkdir phoenix/ && curl -slL -o- https://github.com/owncloud/phoenix/releases/download/0.2.1/phoenix.tar.gz | tar xvzf - -C phoenix/
15 changes: 12 additions & 3 deletions config/example.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
---
server:
debug:
addr: 0.0.0.0:8090
token:
pprof: false

http:
addr: 0.0.0.0:8080
root: /

metrics:
addr: 0.0.0.0:8090
asset:
path:

config:
file:

...
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ module github.com/owncloud/ocis-phoenix
go 1.12

require (
github.com/UnnoTed/fileb0x v1.1.3 // indirect
github.com/go-chi/chi v4.0.2+incompatible
github.com/haya14busa/goverage v0.0.0-20180129164344-eec3514a20b5 // indirect
github.com/restic/calens v0.0.0-20190419101620-10f36cb4a529 // indirect
github.com/mitchellh/gox v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/oklog/run v1.0.0
github.com/prometheus/client_golang v1.1.0
github.com/rs/zerolog v1.15.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
)
87 changes: 77 additions & 10 deletions go.sum

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions pkg/assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package assets

import (
"net/http"
"os"
"path"

"github.com/rs/zerolog/log"
)

//go:generate go run github.com/UnnoTed/fileb0x embed.yml

// assets gets initialized by New and provides the handler.
type assets struct {
path string
}

// Open just implements the HTTP filesystem interface.
func (a assets) Open(original string) (http.File, error) {
if a.path != "" {
if stat, err := os.Stat(a.path); err == nil && stat.IsDir() {
custom := path.Join(
a.path,
original,
)

if _, err := os.Stat(custom); !os.IsNotExist(err) {
f, err := os.Open(custom)

if err != nil {
return nil, err
}

return f, nil
}
} else {
log.Warn().
Msg("Assets directory doesn't exist")
}
}

return FS.OpenFile(
CTX,
original,
os.O_RDONLY,
0644,
)
}

// New returns a new http filesystem to serve assets.
func New(opts ...Option) http.FileSystem {
a := new(assets)

for _, opt := range opts {
opt(a)
}

return a
}
9 changes: 9 additions & 0 deletions pkg/assets/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package assets

import (
// Fake the import to make the dep tree happy.
_ "golang.org/x/net/context"

// Fake the import to make the dep tree happy.
_ "golang.org/x/net/webdav"
)
1,248 changes: 1,248 additions & 0 deletions pkg/assets/embed.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions pkg/assets/embed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
pkg: "assets"
dest: "."
output: "embed.go"
fmt: true
noprefix: true

compression:
compress: true

custom:
- files:
- "../../phoenix/"
base: "../../phoenix/"
prefix: ""
exclude:
- "appinfo/**"
- "LICENSE/**"
- "CHANGELOG.md"
- "README.md"
- "config.json"

...
11 changes: 11 additions & 0 deletions pkg/assets/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package assets

// Option configures an assets option.
type Option func(*assets)

// WithPath returns an option to set custom assets path.
func WithPath(val string) Option {
return func(a *assets) {
a.path = val
}
}
18 changes: 18 additions & 0 deletions pkg/command/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package command

import (
"github.com/spf13/viper"
)

// init defined the default options for viper.
func init() {
viper.SetDefault("debug.addr", "0.0.0.0:8090")
viper.SetDefault("debug.token", "")
viper.SetDefault("debug.pprof", false)

viper.SetDefault("http.addr", "0.0.0.0:8080")
viper.SetDefault("http.root", "/")

viper.SetDefault("asset.path", "")
viper.SetDefault("config.file", "")
}
10 changes: 0 additions & 10 deletions pkg/command/defaults.go

This file was deleted.

39 changes: 33 additions & 6 deletions pkg/command/health.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package command

import (
"fmt"
"net/http"
"os"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -13,15 +17,38 @@ func Health() *cobra.Command {
Short: "Check health status",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
log.Info().
Str("addr", viper.GetString("metrics.addr")).
Msg("Executed health command")
resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
viper.GetString("debug.addr"),
),
)

if err != nil {
log.Error().
Err(err).
Msg("Failed to request health check")

os.Exit(1)
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Error().
Int("code", resp.StatusCode).
Msg("Health seems to be in bad state")

os.Exit(1)
}

os.Exit(0)
},
}

cmd.Flags().String("metrics-addr", "", "Address to metrics endpoint")
viper.BindPFlag("metrics.addr", cmd.Flags().Lookup("metrics-addr"))
viper.BindEnv("metrics.addr", "PHOENIX_METRICS_ADDR")
cmd.Flags().String("debug-addr", "", "Address to debug endpoint")
viper.BindPFlag("debug.addr", cmd.Flags().Lookup("debug-addr"))
viper.BindEnv("debug.addr", "PHOENIX_DEBUG_ADDR")

return cmd
}
4 changes: 3 additions & 1 deletion pkg/command/command.go → pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Root() *cobra.Command {
return cmd
}

// setupLogger prepares the logger.
func setupLogger() {
switch strings.ToLower(viper.GetString("log.level")) {
case "panic":
Expand Down Expand Up @@ -73,6 +74,7 @@ func setupLogger() {
}
}

// setupConfig prepares the config
func setupConfig() {
viper.SetConfigName("phoenix")

Expand All @@ -84,7 +86,7 @@ func setupConfig() {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Debug().
Msg("Continue without config")
Msg("Initializing without config file")
case viper.UnsupportedConfigError:
log.Fatal().
Msg("Unsupported config type")
Expand Down
Loading

0 comments on commit 345a290

Please sign in to comment.