Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve web-ui for listing of repositories #114

Merged
merged 4 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/artipie/front/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ void start(final int port, final String rest) {
);
}
);
this.ignite.path(
"/signout",
() -> {
this.ignite.get(
"",
(req, rsp) -> {
if (req.session() != null) {
req.session().invalidate();
}
rsp.redirect("/dashboard");
return "Ok";
}
);
}
);
this.ignite.path(
"/api",
() -> {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/artipie/front/rest/RepositoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public List<String> list(final String token, final String uname) {
* @return Repository content.
*/
public String repo(final String token, final RepositoryName rname) {
return this.repo(token, rname.toString());
}

/**
* Obtain repository content.
* @param token Token.
* @param rname Repository name.
* @return Repository content.
*/
public String repo(final String token, final String rname) {
return BaseService.handle(
this.httpGet(
Optional.of(token),
Expand Down
55 changes: 48 additions & 7 deletions src/main/java/com/artipie/front/rest/SettingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@
import com.artipie.front.Layout;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import javax.json.JsonObject;

/**
* Settings-service.
*
* @since 1.0
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
public class SettingsService extends BaseService {
/**
* Path to port rest-api.
*/
private static final String PORT_PATH = "/api/v1/settings/port";

/**
* Path to layout rest-api.
*/
private static final String LAYOUT_PATH = "/api/v1/settings/layout";

/**
* Port.
*/
private final AtomicReference<Integer> port;

/**
* Layout.
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
private final AtomicReference<Layout> layout;

/**
Expand All @@ -32,24 +43,54 @@ public class SettingsService extends BaseService {
*/
public SettingsService(final String rest) {
super(rest);
this.port = new AtomicReference<>();
this.layout = new AtomicReference<>();
}

/**
* Obtain Artipie server port.
* @return Artipie server port
*/
public int port() {
return this.value(
this.port,
SettingsService.PORT_PATH,
json -> json.getInt("port")
);
}

/**
* Obtain Artipie layout.
* @return Artipie layout
*/
public Layout layout() {
if (this.layout.get() == null) {
final Layout value = BaseService.handle(
this.httpGet(Optional.empty(), SettingsService.LAYOUT_PATH),
return this.value(
this.layout,
SettingsService.LAYOUT_PATH,
json -> Layout.byName(json.getString("layout"))
);
}

/**
* Obtain Artipie setting's value.
* @param ref Reference to setting value
* @param path Path to rest service
* @param handler Handler of json content
* @param <T> Resulting type of handler
* @return Artipie layout
*/
private <T> T value(final AtomicReference<T> ref, final String path,
final Function<JsonObject, T> handler) {
if (ref.get() == null) {
final T value = BaseService.handle(
this.httpGet(Optional.empty(), path),
res -> {
final JsonObject json = BaseService.jsonObject(res);
return Layout.byName(json.getString("layout"));
return handler.apply(json);
}
);
this.layout.compareAndSet(null, value);
ref.compareAndSet(null, value);
}
return this.layout.get();
return ref.get();
}
}
93 changes: 90 additions & 3 deletions src/main/java/com/artipie/front/ui/repository/RepoList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
*/
package com.artipie.front.ui.repository;

import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import com.artipie.front.Layout;
import com.artipie.front.misc.RouteWrap;
import com.artipie.front.rest.RepositoryService;
import com.artipie.front.rest.SettingsService;
import com.artipie.front.ui.HbPage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* List of repositories page.
Expand All @@ -33,12 +38,23 @@ public RepoList(final RepositoryService repository, final SettingsService settin
req -> {
final String uid = req.session().attribute("uid");
final String token = req.session().attribute("token");
final List<String> repos;
final List<String> names;
if (settings.layout() == Layout.FLAT) {
repos = repository.list(token);
names = repository.list(token);
} else {
repos = repository.list(token, uid);
names = repository.list(token, uid);
}
final List<Repo> repos = new ArrayList<>(names.size());
names.stream().sorted().forEach(
name ->
repos.add(
new Repo(
Integer.toString(settings.port()),
name,
repository.repo(token, name)
)
)
);
return Map.of(
"title", "Repository list",
"repos", repos
Expand All @@ -47,4 +63,75 @@ public RepoList(final RepositoryService repository, final SettingsService settin
)
);
}

/**
* Repository information.
* @since 0.1.3
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
public static class Repo {
/**
* Repository name.
*/
private final String name;

/**
* Artipie server port.
*/
private final String port;

/**
* Repository configuration.
*/
private Optional<YamlMapping> conf;

/**
* Ctor.
* @param port Artipie's default port
* @param name Name of repository.
* @param conf Repository configuration content
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public Repo(final String port, final String name, final String conf) {
this.port = port;
this.name = name;
try {
this.conf = Optional.of(Yaml.createYamlInput(conf).readYamlMapping());
} catch (final IOException exc) {
this.conf = Optional.empty();
}
}

/**
* Name of repository.
* @return Name of repository.
*/
public String name() {
return this.name;
}

/**
* Type of repository defined in configuration.
* @return Repository type or empty string
*/
public String type() {
return this.repo().map(repo -> repo.string("type")).orElse("");
}

/**
* Port of repository defined in configuration.
* @return Repository port or default Artipie port
*/
public String port() {
return this.repo().map(repo -> repo.string("port")).orElse(this.port);
}

/**
* Repository repo-section in yaml.
* @return Repository repo-configuration
*/
private Optional<YamlMapping> repo() {
return this.conf.map(value -> value.yamlMapping("repo"));
}
}
}
50 changes: 26 additions & 24 deletions src/main/resources/html/base
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="shortcut icon" href="/logo.png"/>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/yegor256/tacit@gh-pages/tacit-css.min.css"/>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/yegor256/drops@gh-pages/drops.min.css"/>
<link rel="stylesheet" href="/css/style.css"/>
<script src="/scripts/jquery-3.6.1.min.js"></script>
</head>
<body>
Expand All @@ -19,33 +20,34 @@
</ul>
</nav>
</header>
<table>
<tr>
<td>
<ul id="navMenu">
<li><a href="/dashboard/repository/list">Repositories</a>
<ul>
<main>
<div class="main">
<div class="menu">
<ul id="navMenu">
<li><a href="/dashboard/repository/list">Repositories</a>
<ul>
<li><a href="/dashboard/repository/create">Create</a></li>
</ul>
</li>
<li>Artipie
<ul>
</ul>
</li>
<li>Artipie
<ul>
<li><a href="https://github.com/artipie">Github</a></li>
<li><a href="https://github.com/artipie/artipie/wiki">Wiki</a></li>
</ul>
</li>
</ul>
</td>
<td>
<section style="width:90%">
<article>
{{#block "content"}}
{{/block}}
</article>
</section>
</td>
</tr>
</table>
</ul>
</li>
<li><a href="/signout">Sign out</a></li>
</ul>
</div>
<div class="content">
<section style="width:100%">
<article>
{{#block "content"}}
{{/block}}
</article>
</section>
</div>
</div>
</main>
<footer>
<nav>
<ul>
Expand Down
Loading