Add back http-root

This commit is contained in:
Thomas Boerger
2019-12-09 16:13:26 +01:00
parent 3b82714a78
commit 690e80aaec
8 changed files with 93 additions and 26 deletions
+6
View File
@@ -71,6 +71,9 @@ PHOENIX_DEBUG_ZPAGES
PHOENIX_HTTP_ADDR
: Address to bind http server, defaults to `0.0.0.0:9100`
PHOENIX_HTTP_ROOT
: Root path of http server, defaults to `/`
PHOENIX_ASSET_PATH
: Path to custom assets, empty default value
@@ -132,6 +135,9 @@ If you prefer to configure the service with commandline flags you can see the av
--http-addr
: Address to bind http server, defaults to `0.0.0.0:9100`
--http-root
: Root path of http server, defaults to `/`
--asset-path
: Path to custom assets, empty default value
+3 -3
View File
@@ -14,8 +14,8 @@ require (
github.com/ogier/pflag v0.0.1 // indirect
github.com/oklog/run v1.0.0
github.com/openzipkin/zipkin-go v0.2.2
github.com/owncloud/ocis-pkg v1.1.0
github.com/spf13/viper v1.5.0
github.com/owncloud/ocis-pkg v1.2.0
github.com/spf13/viper v1.6.1
go.opencensus.io v0.22.2
golang.org/x/net v0.0.0-20191206103017-1ddd1de85cb0
golang.org/x/net v0.0.0-20191207000613-e7e4b65ae663
)
+9 -6
View File
@@ -537,8 +537,8 @@ github.com/openzipkin/zipkin-go v0.2.2 h1:nY8Hti+WKaP0cRsSeQ026wU03QsM762XBeCXBb
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/oracle/oci-go-sdk v7.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukwStZIg5F66tcBccjip/j888=
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014/go.mod h1:joRatxRJaZBsY3JAOEMcoOp05CnZzsx4scTxi95DHyQ=
github.com/owncloud/ocis-pkg v1.1.0 h1:cwhzqQAHLA8fz94YKdmqSJguSCswx5TBNkgLAU8DbJI=
github.com/owncloud/ocis-pkg v1.1.0/go.mod h1:EfbeXoe60Me2lB/AWjYl8UFNv4isqCPP6lokd5R7nyM=
github.com/owncloud/ocis-pkg v1.2.0 h1:eP0AOSEXAgiblL2yOpNOmriKhDXN+mai+4belBJRkWU=
github.com/owncloud/ocis-pkg v1.2.0/go.mod h1:EfbeXoe60Me2lB/AWjYl8UFNv4isqCPP6lokd5R7nyM=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
@@ -617,6 +617,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
@@ -631,8 +632,8 @@ github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzu
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.5.0 h1:GpsTwfsQ27oS/Aha/6d1oD7tpKIqWnOA6tgOX9HHkt4=
github.com/spf13/viper v1.5.0/go.mod h1:AkYRkVJF8TkSG/xet6PzXX+l39KhhXa2pdqVSxnTcn4=
github.com/spf13/viper v1.6.1 h1:VPZzIkznI1YhVMRi6vNFLHSwhnhReBfgTxIPccpfdZk=
github.com/spf13/viper v1.6.1/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
@@ -771,8 +772,8 @@ golang.org/x/net v0.0.0-20191011234655-491137f69257/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191206103017-1ddd1de85cb0 h1:LxY/gQN/MrcW24/46nLyiip1GhN/Yi14QPbeNskTvQA=
golang.org/x/net v0.0.0-20191206103017-1ddd1de85cb0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191207000613-e7e4b65ae663 h1:Dd5RoEW+yQi+9DMybroBctIdyiwuNT7sJFMC27/6KxI=
golang.org/x/net v0.0.0-20191207000613-e7e4b65ae663/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -916,6 +917,8 @@ gopkg.in/h2non/gock.v1 v1.0.15/go.mod h1:sX4zAkdYX1TRGJ2JY156cFspQn4yRWn6p9EMdOD
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.44.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo=
gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eRhxkJMWSIz9Q=
gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mNfM5bm88whjWx4=
+8
View File
@@ -4,6 +4,7 @@ import (
"context"
"os"
"os/signal"
"strings"
"time"
"contrib.go.opencensus.io/exporter/jaeger"
@@ -28,6 +29,13 @@ func Server(cfg *config.Config) cli.Command {
Name: "server",
Usage: "Start integrated server",
Flags: flagset.ServerWithConfig(cfg),
Before: func(c *cli.Context) error {
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
+1
View File
@@ -18,6 +18,7 @@ type Debug struct {
// HTTP defines the available http configuration.
type HTTP struct {
Addr string
Root string
}
// Tracing defines the available tracing configuration.
+7
View File
@@ -120,6 +120,13 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVar: "PHOENIX_HTTP_ADDR",
Destination: &cfg.HTTP.Addr,
},
&cli.StringFlag{
Name: "http-root",
Value: "/",
Usage: "Root path of http server",
EnvVar: "PHOENIX_HTTP_ROOT",
Destination: &cfg.HTTP.Root,
},
&cli.StringFlag{
Name: "asset-path",
Value: "",
-7
View File
@@ -1,7 +1,6 @@
package http
import (
"github.com/owncloud/ocis-phoenix/pkg/assets"
"github.com/owncloud/ocis-phoenix/pkg/config"
"github.com/owncloud/ocis-phoenix/pkg/flagset"
"github.com/owncloud/ocis-phoenix/pkg/service/v0"
@@ -41,12 +40,6 @@ func Server(opts ...Option) (http.Service, error) {
middleware.Logger(
options.Logger,
),
middleware.Static(
assets.New(
assets.Logger(options.Logger),
assets.Config(options.Config),
),
),
),
)
+59 -10
View File
@@ -4,8 +4,10 @@ import (
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/go-chi/chi"
"github.com/owncloud/ocis-phoenix/pkg/assets"
"github.com/owncloud/ocis-phoenix/pkg/config"
"github.com/owncloud/ocis-pkg/log"
)
@@ -29,11 +31,15 @@ func NewService(opts ...Option) Service {
m.Use(options.Middleware...)
svc := Phoenix{
logger: options.Logger,
config: options.Config,
mux: m,
}
m.HandleFunc("/config.json", svc.Config)
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.Get("/config.json", svc.Config)
r.Mount("/", svc.Static())
})
return svc
}
@@ -46,28 +52,28 @@ type Phoenix struct {
}
// ServeHTTP implements the Service interface.
func (g Phoenix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r)
func (p Phoenix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.mux.ServeHTTP(w, r)
}
// Config implements the Service interface.
func (g Phoenix) Config(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(g.config.Phoenix.Path); os.IsNotExist(err) {
g.logger.Error().
func (p Phoenix) Config(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(p.config.Phoenix.Path); os.IsNotExist(err) {
p.logger.Error().
Err(err).
Str("config", g.config.Phoenix.Path).
Str("config", p.config.Phoenix.Path).
Msg("Phoenix config doesn't exist")
http.Error(w, ErrConfigInvalid, http.StatusUnprocessableEntity)
return
}
payload, err := ioutil.ReadFile(g.config.Phoenix.Path)
payload, err := ioutil.ReadFile(p.config.Phoenix.Path)
if err != nil {
g.logger.Error().
p.logger.Error().
Err(err).
Str("config", g.config.Phoenix.Path).
Str("config", p.config.Phoenix.Path).
Msg("Failed to read custom config")
http.Error(w, ErrConfigInvalid, http.StatusUnprocessableEntity)
@@ -78,3 +84,46 @@ func (g Phoenix) Config(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(payload)
}
// Static simply serves all static files.
func (p Phoenix) Static() http.HandlerFunc {
rootWithSlash := p.config.HTTP.Root
if !strings.HasSuffix(rootWithSlash, "/") {
rootWithSlash = rootWithSlash + "/"
}
static := http.StripPrefix(
rootWithSlash,
http.FileServer(
assets.New(
assets.Logger(p.logger),
assets.Config(p.config),
),
),
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if rootWithSlash != "/" && r.URL.Path == p.config.HTTP.Root {
http.Redirect(
w,
r,
rootWithSlash,
http.StatusMovedPermanently,
)
return
}
if r.URL.Path != rootWithSlash && strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(
w,
r,
)
return
}
static.ServeHTTP(w, r)
})
}