Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pleroma
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Maksim
pleroma
Commits
ad5c4262
Commit
ad5c4262
authored
Jul 28, 2020
by
lain
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FrontendStatic: Add plug to serve frontends based on configuration.
parent
14c28dcb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
122 additions
and
5 deletions
+122
-5
lib/pleroma/plugs/frontend_static.ex
lib/pleroma/plugs/frontend_static.ex
+54
-0
lib/pleroma/plugs/instance_static.ex
lib/pleroma/plugs/instance_static.ex
+4
-4
lib/pleroma/web/endpoint.ex
lib/pleroma/web/endpoint.ex
+11
-0
test/plugs/frontend_static_test.exs
test/plugs/frontend_static_test.exs
+30
-0
test/plugs/instance_static_test.exs
test/plugs/instance_static_test.exs
+23
-1
No files found.
lib/pleroma/plugs/frontend_static.ex
0 → 100644
View file @
ad5c4262
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule
Pleroma
.
Plugs
.
FrontendStatic
do
require
Pleroma
.
Constants
@moduledoc
"""
This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
"""
@behaviour
Plug
def
file_path
(
path
,
frontend_type
\\
:primary
)
do
if
configuration
=
Pleroma
.
Config
.
get
([
:frontends
,
frontend_type
])
do
instance_static_path
=
Pleroma
.
Config
.
get
([
:instance
,
:static_dir
],
"instance/static"
)
Path
.
join
([
instance_static_path
,
"frontends"
,
configuration
[
"name"
],
configuration
[
"ref"
],
path
])
else
nil
end
end
def
init
(
opts
)
do
opts
|>
Keyword
.
put
(
:from
,
"__unconfigured_frontend_static_plug"
)
|>
Plug
.
Static
.
init
()
end
def
call
(
conn
,
opts
)
do
frontend_type
=
Map
.
get
(
opts
,
:frontend_type
,
:primary
)
path
=
file_path
(
""
,
frontend_type
)
if
path
do
conn
|>
call_static
(
opts
,
path
)
else
conn
end
end
defp
call_static
(
conn
,
opts
,
from
)
do
opts
=
opts
|>
Map
.
put
(
:from
,
from
)
Plug
.
Static
.
call
(
conn
,
opts
)
end
end
lib/pleroma/plugs/instance_static.ex
View file @
ad5c4262
...
...
@@ -16,11 +16,11 @@ def file_path(path) do
instance_path
=
Path
.
join
(
Pleroma
.
Config
.
get
([
:instance
,
:static_dir
],
"instance/static/"
),
path
)
if
File
.
exists?
(
instance_path
)
do
instance_path
else
frontend_path
=
Pleroma
.
Plugs
.
FrontendStatic
.
file_path
(
path
,
:primary
)
(
File
.
exists?
(
instance_path
)
&&
instance_path
)
||
(
frontend_path
&&
File
.
exists?
(
frontend_path
)
&&
frontend_path
)
||
Path
.
join
(
Application
.
app_dir
(
:pleroma
,
"priv/static/"
),
path
)
end
end
def
init
(
opts
)
do
...
...
lib/pleroma/web/endpoint.ex
View file @
ad5c4262
...
...
@@ -28,6 +28,17 @@ defmodule Pleroma.Web.Endpoint do
}
)
# Careful! No `only` restriction here, as we don't know what frontends contain.
plug
(
Pleroma
.
Plugs
.
FrontendStatic
,
at:
"/"
,
frontend_type:
:primary
,
gzip:
true
,
cache_control_for_etags:
@static_cache_control
,
headers:
%{
"cache-control"
=>
@static_cache_control
}
)
# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
...
...
test/plugs/frontend_static_test.exs
0 → 100644
View file @
ad5c4262
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule
Pleroma
.
Web
.
FrontendStaticPlugTest
do
use
Pleroma
.
Web
.
ConnCase
@dir
"test/tmp/instance_static"
setup
do
File
.
mkdir_p!
(
@dir
)
on_exit
(
fn
->
File
.
rm_rf
(
@dir
)
end
)
end
setup
do
:
clear_config
([
:instance
,
:static_dir
],
@dir
)
test
"overrides existing static files"
,
%{
conn:
conn
}
do
name
=
"pelmora"
ref
=
"uguu"
clear_config
([
:frontends
,
:primary
],
%{
"name"
=>
name
,
"ref"
=>
ref
})
path
=
"
#{
@dir
}
/frontends/
#{
name
}
/
#{
ref
}
"
File
.
mkdir_p!
(
path
)
File
.
write!
(
"
#{
path
}
/index.html"
,
"from frontend plug"
)
index
=
get
(
conn
,
"/"
)
assert
html_response
(
index
,
200
)
==
"from frontend plug"
end
end
test/plugs/instance_static_test.exs
View file @
ad5c4262
...
...
@@ -2,7 +2,7 @@
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule
Pleroma
.
Web
.
Runtim
eStaticPlugTest
do
defmodule
Pleroma
.
Web
.
Instanc
eStaticPlugTest
do
use
Pleroma
.
Web
.
ConnCase
@dir
"test/tmp/instance_static"
...
...
@@ -24,6 +24,28 @@ test "overrides index" do
assert
html_response
(
index
,
200
)
==
"hello world"
end
test
"also overrides frontend files"
,
%{
conn:
conn
}
do
name
=
"pelmora"
ref
=
"uguu"
clear_config
([
:frontends
,
:primary
],
%{
"name"
=>
name
,
"ref"
=>
ref
})
bundled_index
=
get
(
conn
,
"/"
)
refute
html_response
(
bundled_index
,
200
)
==
"from frontend plug"
path
=
"
#{
@dir
}
/frontends/
#{
name
}
/
#{
ref
}
"
File
.
mkdir_p!
(
path
)
File
.
write!
(
"
#{
path
}
/index.html"
,
"from frontend plug"
)
index
=
get
(
conn
,
"/"
)
assert
html_response
(
index
,
200
)
==
"from frontend plug"
File
.
write!
(
@dir
<>
"/index.html"
,
"from instance static"
)
index
=
get
(
conn
,
"/"
)
assert
html_response
(
index
,
200
)
==
"from instance static"
end
test
"overrides any file in static/static"
do
bundled_index
=
get
(
build_conn
(),
"/static/terms-of-service.html"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment