From 7b50cbf0acf384af5cdc30c2cbb5531dc3972cee Mon Sep 17 00:00:00 2001 From: Rui Yang Date: Wed, 14 Oct 2020 21:19:23 -0400 Subject: [PATCH] use pkger for embedding static contents Co-authored-by: Vikram Yadav Signed-off-by: Rui Yang --- Makefile | 1 + cmd/dex/config_test.go | 3 ++ cmd/dex/main.go | 2 + go.mod | 1 + go.sum | 16 +++----- server/handlers_test.go | 2 +- server/server.go | 13 ++++++- server/server_test.go | 4 +- server/templates.go | 82 +++++---------------------------------- server/templates_test.go | 51 ------------------------ web/templates/header.html | 8 ++-- 11 files changed, 39 insertions(+), 144 deletions(-) delete mode 100644 server/templates_test.go diff --git a/Makefile b/Makefile index 82b266a6..c5bb237a 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ build: bin/dex bin/dex: @mkdir -p bin/ + @go generate ./... @go install -v -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/dex examples: bin/grpc-client bin/example-app diff --git a/cmd/dex/config_test.go b/cmd/dex/config_test.go index 9a36afcf..c62d18b5 100644 --- a/cmd/dex/config_test.go +++ b/cmd/dex/config_test.go @@ -74,6 +74,7 @@ web: http: 127.0.0.1:5556 frontend: + dir: /web extra: foo: bar @@ -143,6 +144,7 @@ logger: HTTP: "127.0.0.1:5556", }, Frontend: server.WebConfig{ + Dir: "/web", Extra: map[string]string{ "foo": "bar", }, @@ -349,6 +351,7 @@ logger: HTTP: "127.0.0.1:5556", }, Frontend: server.WebConfig{ + Dir: "/web", Extra: map[string]string{ "foo": "bar", }, diff --git a/cmd/dex/main.go b/cmd/dex/main.go index be334d92..978bcea1 100644 --- a/cmd/dex/main.go +++ b/cmd/dex/main.go @@ -1,3 +1,5 @@ +//go:generate pkger + package main import ( diff --git a/go.mod b/go.mod index 6291284c..6fd434c6 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/kylelemons/godebug v1.1.0 github.com/lib/pq v1.10.0 + github.com/markbates/pkger v0.17.1 github.com/mattermost/xml-roundtrip-validator v0.0.0-20201219040909-8fd2afad43d1 github.com/mattn/go-sqlite3 v1.14.6 github.com/oklog/run v1.1.0 diff --git a/go.sum b/go.sum index 69b3bcf4..36971d11 100644 --- a/go.sum +++ b/go.sum @@ -86,8 +86,9 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI= +github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= @@ -188,6 +189,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+ github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E= github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno= +github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= github.com/mattermost/xml-roundtrip-validator v0.0.0-20201219040909-8fd2afad43d1 h1:x37Q11fexMtlhecRnkdzLL6dgnS1NF1nzAJ1vic22BY= github.com/mattermost/xml-roundtrip-validator v0.0.0-20201219040909-8fd2afad43d1/go.mod h1:qccnGMcpgwcNaBnxqpJpWWUiPNr5H3O8eDgGV9gT5To= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -264,7 +267,6 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= @@ -280,7 +282,6 @@ github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M= github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= 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= @@ -293,7 +294,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -339,7 +339,6 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= @@ -363,13 +362,11 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200505041828-1ed23360d12c h1:zJ0mtu4jCalhKg6Oaukv6iIkb+cOvDrajDH9DH46Q4M= golang.org/x/net v0.0.0-20200505041828-1ed23360d12c/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 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-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -378,7 +375,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180117170059-2c42eef0765b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -408,7 +404,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20171227012246-e19ae1496984/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -456,7 +451,6 @@ google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRn google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a h1:Ob5/580gVHBJZgXnff1cZDbG+xLtMVE5mDRTe+nIsX4= @@ -493,6 +487,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= @@ -501,7 +496,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= sigs.k8s.io/testing_frameworks v0.1.2 h1:vK0+tvjF0BZ/RYFeZ1E6BYBwHJJXhjuZ3TdsEKH+UQM= diff --git a/server/handlers_test.go b/server/handlers_test.go index 96d228af..d52a45c0 100644 --- a/server/handlers_test.go +++ b/server/handlers_test.go @@ -135,7 +135,7 @@ func TestHandleInvalidSAMLCallbacks(t *testing.T) { func TestConnectorLoginDoesNotAllowToChangeConnectorForAuthRequest(t *testing.T) { memStorage := memory.New(logger) - templates, err := loadTemplates(WebConfig{Dir: http.Dir("../web")}, "../web/templates") + templates, err := loadTemplates(WebConfig{Dir: "/web"}, "/web/templates") if err != nil { t.Fatal("failed to load templates") } diff --git a/server/server.go b/server/server.go index baf2b30a..df81a253 100644 --- a/server/server.go +++ b/server/server.go @@ -19,6 +19,7 @@ import ( "github.com/felixge/httpsnoop" "github.com/gorilla/handlers" "github.com/gorilla/mux" + "github.com/markbates/pkger" "github.com/prometheus/client_golang/prometheus" "golang.org/x/crypto/bcrypt" @@ -108,7 +109,7 @@ type WebConfig struct { // * templates - HTML templates controlled by dex. // * themes/(theme) - Static static served at "( issuer URL )/theme". // - Dir http.FileSystem + Dir string // Defaults to "( issuer URL )/theme/logo.png" LogoURL string @@ -206,6 +207,10 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) supported[respType] = true } + if c.Web.Dir == "" { + c.Web.Dir = pkger.Include("/web") + } + tmpls, err := loadTemplates(c.Web, issuerURL.Path) if err != nil { return nil, fmt.Errorf("server: failed to load templates: %v", err) @@ -337,7 +342,11 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy) } fmt.Fprintf(w, "Health check passed") })) - handlePrefix("/", http.FileServer(c.Web.Dir)) + + staticDir := path.Join(c.Web.Dir, "static") + themeDir := path.Join(c.Web.Dir, "themes", c.Web.Theme) + handlePrefix("/static", http.FileServer(pkger.Dir(staticDir))) + handlePrefix(path.Join("/themes", c.Web.Theme), http.FileServer(pkger.Dir(themeDir))) s.mux = r diff --git a/server/server_test.go b/server/server_test.go index b0bacfd3..03bedbe6 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -93,7 +93,7 @@ func newTestServer(ctx context.Context, t *testing.T, updateConfig func(c *Confi Issuer: s.URL, Storage: memory.New(logger), Web: WebConfig{ - Dir: http.Dir("../web"), + Dir: "/web", }, Logger: logger, PrometheusRegistry: prometheus.NewRegistry(), @@ -132,7 +132,7 @@ func newTestServerMultipleConnectors(ctx context.Context, t *testing.T, updateCo Issuer: s.URL, Storage: memory.New(logger), Web: WebConfig{ - Dir: http.Dir("../web"), + Dir: "/web", }, Logger: logger, PrometheusRegistry: prometheus.NewRegistry(), diff --git a/server/templates.go b/server/templates.go index 1c04a68e..49f70d1d 100644 --- a/server/templates.go +++ b/server/templates.go @@ -6,11 +6,12 @@ import ( "html/template" "io" "net/http" - "net/url" "path" "path/filepath" "sort" "strings" + + "github.com/markbates/pkger" ) const ( @@ -61,9 +62,11 @@ func loadTemplates(c WebConfig, issuerPath string) (*templates, error) { funcs := template.FuncMap{ "issuer": func() string { return c.Issuer }, "logo": func() string { return c.LogoURL }, - "url": func(reqPath, assetPath string) string { return relativeURL(hostURL, reqPath, assetPath) }, - "theme": func(reqPath, assetPath string) string { - return relativeURL(hostURL, reqPath, path.Join("themes", c.Theme, assetPath)) + "static": func(assetPath string) string { + return path.Join(hostURL, "static", assetPath) + }, + "theme": func(assetPath string) string { + return path.Join(hostURL, "themes", c.Theme, assetPath) }, "lower": strings.ToLower, "extra": func(k string) string { return c.Extra[k] }, @@ -130,76 +133,9 @@ func loadTemplates(c WebConfig, issuerPath string) (*templates, error) { }, nil } -// relativeURL returns the URL of the asset relative to the URL of the request path. -// The serverPath is consulted to trim any prefix due in case it is not listening -// to the root path. -// -// Algorithm: -// 1. Remove common prefix of serverPath and reqPath -// 2. Remove common prefix of assetPath and reqPath -// 3. For each part of reqPath remaining(minus one), go up one level (..) -// 4. For each part of assetPath remaining, append it to result -// -// eg -// server listens at localhost/dex so serverPath is dex -// reqPath is /dex/auth -// assetPath is static/main.css -// relativeURL("/dex", "/dex/auth", "static/main.css") = "../static/main.css" -func relativeURL(serverPath, reqPath, assetPath string) string { - if u, err := url.ParseRequestURI(assetPath); err == nil && u.Scheme != "" { - // assetPath points to the external URL, no changes needed - return assetPath - } - - splitPath := func(p string) []string { - res := []string{} - parts := strings.Split(path.Clean(p), "/") - for _, part := range parts { - if part != "" { - res = append(res, part) - } - } - return res - } - - stripCommonParts := func(s1, s2 []string) ([]string, []string) { - min := len(s1) - if len(s2) < min { - min = len(s2) - } - - splitIndex := min - for i := 0; i < min; i++ { - if s1[i] != s2[i] { - splitIndex = i - break - } - } - return s1[splitIndex:], s2[splitIndex:] - } - - server, req, asset := splitPath(serverPath), splitPath(reqPath), splitPath(assetPath) - - // Remove common prefix of request path with server path - _, req = stripCommonParts(server, req) - - // Remove common prefix of request path with asset path - asset, req = stripCommonParts(asset, req) - - // For each part of the request remaining (minus one) -> go up one level (..) - // For each part of the asset remaining -> append it - var relativeURL string - for i := 0; i < len(req)-1; i++ { - relativeURL = path.Join("..", relativeURL) - } - relativeURL = path.Join(relativeURL, path.Join(asset...)) - - return relativeURL -} - // load a template by name from the templates dir -func loadTemplate(dir http.FileSystem, name string, funcs template.FuncMap, group *template.Template) (*template.Template, error) { - file, err := dir.Open(filepath.Join("templates", name)) +func loadTemplate(dir string, name string, funcs template.FuncMap, group *template.Template) (*template.Template, error) { + file, err := pkger.Open(filepath.Join(dir, "templates", name)) if err != nil { return nil, err } diff --git a/server/templates_test.go b/server/templates_test.go deleted file mode 100644 index defb2e5e..00000000 --- a/server/templates_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package server - -import "testing" - -func TestRelativeURL(t *testing.T) { - tests := []struct { - name string - serverPath string - reqPath string - assetPath string - expected string - }{ - { - name: "server-root-req-one-level-asset-two-level", - serverPath: "/", - reqPath: "/auth", - assetPath: "/theme/main.css", - expected: "theme/main.css", - }, - { - name: "server-one-level-req-one-level-asset-two-level", - serverPath: "/dex", - reqPath: "/dex/auth", - assetPath: "/theme/main.css", - expected: "theme/main.css", - }, - { - name: "server-root-req-two-level-asset-three-level", - serverPath: "/dex", - reqPath: "/dex/auth/connector", - assetPath: "assets/css/main.css", - expected: "../assets/css/main.css", - }, - { - name: "external-url", - serverPath: "/dex", - reqPath: "/dex/auth/connector", - assetPath: "https://kubernetes.io/images/favicon.png", - expected: "https://kubernetes.io/images/favicon.png", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - actual := relativeURL(test.serverPath, test.reqPath, test.assetPath) - if actual != test.expected { - t.Fatalf("Got '%s'. Expected '%s'", actual, test.expected) - } - }) - } -} diff --git a/web/templates/header.html b/web/templates/header.html index 78dde15e..bbd1c13f 100644 --- a/web/templates/header.html +++ b/web/templates/header.html @@ -5,15 +5,15 @@ {{ issuer }} - - - + + +
- +