Skip to content

Commit 2b7d15a

Browse files
Update main/app.js to use vue-router in 'history' mode
- Also add support for the old URLs - Add a new "serve" goal to sitegen (I.e. mvn sitegen:serve) - Update vuetify backend to produce absolute resources and link by default
1 parent 9373804 commit 2b7d15a

File tree

9 files changed

+492
-29
lines changed

9 files changed

+492
-29
lines changed
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.build.common;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Optional;
21+
22+
/**
23+
* Mapping of file extension to mime-type.
24+
*/
25+
public final class MediaTypes {
26+
27+
private static final Map<String, String> MEDIA_TYPES = new HashMap<>();
28+
29+
static {
30+
MEDIA_TYPES.put("3gp", "video/3gpp");
31+
MEDIA_TYPES.put("3g2", "video/3gpp2");
32+
MEDIA_TYPES.put("7z", "application/x-7z-compressed");
33+
MEDIA_TYPES.put("aac", "audio/aac");
34+
MEDIA_TYPES.put("abs", "audio/x-mpeg");
35+
MEDIA_TYPES.put("abw", "application/x-abiword");
36+
MEDIA_TYPES.put("ai", "application/postscript");
37+
MEDIA_TYPES.put("aif", "audio/x-aiff");
38+
MEDIA_TYPES.put("aifc", "audio/x-aiff");
39+
MEDIA_TYPES.put("aiff", "audio/x-aiff");
40+
MEDIA_TYPES.put("aim", "application/x-aim");
41+
MEDIA_TYPES.put("arc", "application/x-freearc");
42+
MEDIA_TYPES.put("art", "image/x-jg");
43+
MEDIA_TYPES.put("asf", "video/x-ms-asf");
44+
MEDIA_TYPES.put("asx", "video/x-ms-asf");
45+
MEDIA_TYPES.put("au", "audio/basic");
46+
MEDIA_TYPES.put("avi", "video/x-msvideo");
47+
MEDIA_TYPES.put("avx", "video/x-rad-screenplay");
48+
MEDIA_TYPES.put("azw", "application/vnd.amazon.ebook");
49+
MEDIA_TYPES.put("bcpio", "application/x-bcpio");
50+
MEDIA_TYPES.put("bin", "application/octet-stream");
51+
MEDIA_TYPES.put("bmp", "image/bmp");
52+
MEDIA_TYPES.put("body", "text/html");
53+
MEDIA_TYPES.put("bz", "application/x-bzip");
54+
MEDIA_TYPES.put("bz2", "application/x-bzip2");
55+
MEDIA_TYPES.put("cdf", "application/x-cdf");
56+
MEDIA_TYPES.put("cer", "application/x-x509-ca-cert");
57+
MEDIA_TYPES.put("class", "application/java");
58+
MEDIA_TYPES.put("conf", "application/hocon");
59+
MEDIA_TYPES.put("cpio", "application/x-cpio");
60+
MEDIA_TYPES.put("csh", "application/x-csh");
61+
MEDIA_TYPES.put("css", "text/css");
62+
MEDIA_TYPES.put("csv", "text/csv");
63+
MEDIA_TYPES.put("dib", "image/bmp");
64+
MEDIA_TYPES.put("doc", "application/msword");
65+
MEDIA_TYPES.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
66+
MEDIA_TYPES.put("dtd", "application/xml-dtd");
67+
MEDIA_TYPES.put("dv", "video/x-dv");
68+
MEDIA_TYPES.put("dvi", "application/x-dvi");
69+
MEDIA_TYPES.put("eot", "application/vnd.ms-fontobject");
70+
MEDIA_TYPES.put("epub", "application/epub+zip");
71+
MEDIA_TYPES.put("eps", "application/postscript");
72+
MEDIA_TYPES.put("etx", "text/x-setext");
73+
MEDIA_TYPES.put("exe", "application/octet-stream");
74+
MEDIA_TYPES.put("gif", "image/gif");
75+
MEDIA_TYPES.put("gk", "application/octet-stream");
76+
MEDIA_TYPES.put("gtar", "application/x-gtar");
77+
MEDIA_TYPES.put("gz", "application/gzip");
78+
MEDIA_TYPES.put("hdf", "application/x-hdf");
79+
MEDIA_TYPES.put("hqx", "application/mac-binhex40");
80+
MEDIA_TYPES.put("htc", "text/x-component");
81+
MEDIA_TYPES.put("htm", "text/html");
82+
MEDIA_TYPES.put("html", "text/html");
83+
MEDIA_TYPES.put("ief", "image/ief");
84+
MEDIA_TYPES.put("ico", "image/x-icon");
85+
MEDIA_TYPES.put("ics", "text/calendar");
86+
MEDIA_TYPES.put("jad", "text/vnd.sun.j2me.app-descriptor");
87+
MEDIA_TYPES.put("jar", "application/java-archive");
88+
MEDIA_TYPES.put("java", "text/plain");
89+
MEDIA_TYPES.put("jnlp", "application/x-java-jnlp-file");
90+
MEDIA_TYPES.put("jpe", "image/jpeg");
91+
MEDIA_TYPES.put("jpeg", "image/jpeg");
92+
MEDIA_TYPES.put("jpg", "image/jpeg");
93+
MEDIA_TYPES.put("js", "text/javascript");
94+
MEDIA_TYPES.put("json", "application/json");
95+
MEDIA_TYPES.put("jsonld", "application/ld+json");
96+
MEDIA_TYPES.put("kar", "audio/x-midi");
97+
MEDIA_TYPES.put("latex", "application/x-latex");
98+
MEDIA_TYPES.put("m3u", "audio/x-mpegurl");
99+
MEDIA_TYPES.put("mac", "image/x-macpaint");
100+
MEDIA_TYPES.put("man", "application/x-troff-man");
101+
MEDIA_TYPES.put("mathml", "application/mathml+xml");
102+
MEDIA_TYPES.put("me", "application/x-troff-me");
103+
MEDIA_TYPES.put("mid", "audio/midi");
104+
MEDIA_TYPES.put("midi", "audio/midi");
105+
MEDIA_TYPES.put("mif", "application/x-mif");
106+
MEDIA_TYPES.put("mjs", "text/javascript");
107+
MEDIA_TYPES.put("mov", "video/quicktime");
108+
MEDIA_TYPES.put("movie", "video/x-sgi-movie");
109+
MEDIA_TYPES.put("mp1", "audio/mpeg");
110+
MEDIA_TYPES.put("mp2", "audio/mpeg");
111+
MEDIA_TYPES.put("mp3", "audio/mpeg");
112+
MEDIA_TYPES.put("mpa", "audio/mpeg");
113+
MEDIA_TYPES.put("mpe", "video/mpeg");
114+
MEDIA_TYPES.put("mpeg", "video/mpeg");
115+
MEDIA_TYPES.put("mpega", "audio/mpeg");
116+
MEDIA_TYPES.put("mpkg", "application/vnd.apple.installer+xml");
117+
MEDIA_TYPES.put("mpg", "video/mpeg");
118+
MEDIA_TYPES.put("mpv2", "video/mpeg2");
119+
MEDIA_TYPES.put("ms", "application/x-wais-source");
120+
MEDIA_TYPES.put("nc", "application/x-netcdf");
121+
MEDIA_TYPES.put("oda", "application/oda");
122+
MEDIA_TYPES.put("odp", "application/vnd.oasis.opendocument.presentation");
123+
MEDIA_TYPES.put("ods", "application/vnd.oasis.opendocument.spreadsheet");
124+
MEDIA_TYPES.put("odt", "application/vnd.oasis.opendocument.text");
125+
MEDIA_TYPES.put("oga", "audio/ogg");
126+
MEDIA_TYPES.put("ogg", "application/ogg");
127+
MEDIA_TYPES.put("ogv", "video/ogg");
128+
MEDIA_TYPES.put("ogx", "application/ogg");
129+
MEDIA_TYPES.put("opus", "audio/opus");
130+
MEDIA_TYPES.put("otf", "font/otf");
131+
MEDIA_TYPES.put("pbm", "image/x-portable-bitmap");
132+
MEDIA_TYPES.put("pct", "image/pict");
133+
MEDIA_TYPES.put("pdf", "application/pdf");
134+
MEDIA_TYPES.put("pgm", "image/x-portable-graymap");
135+
MEDIA_TYPES.put("php", "appliction/php");
136+
MEDIA_TYPES.put("pic", "image/pict");
137+
MEDIA_TYPES.put("pict", "image/pict");
138+
MEDIA_TYPES.put("pls", "audio/x-scpls");
139+
MEDIA_TYPES.put("png", "image/png");
140+
MEDIA_TYPES.put("pnm", "image/x-portable-anymap");
141+
MEDIA_TYPES.put("pnt", "image/x-macpaint");
142+
MEDIA_TYPES.put("ppm", "image/x-portable-pixmap");
143+
MEDIA_TYPES.put("ppt", "application/vnd.ms-powerpoint");
144+
MEDIA_TYPES.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
145+
MEDIA_TYPES.put("properties", "text/x-java-properties");
146+
MEDIA_TYPES.put("ps", "application/postscript");
147+
MEDIA_TYPES.put("psd", "image/x-photoshop");
148+
MEDIA_TYPES.put("qt", "video/quicktime");
149+
MEDIA_TYPES.put("qti", "image/x-quicktime");
150+
MEDIA_TYPES.put("qtif", "image/x-quicktime");
151+
MEDIA_TYPES.put("rar", "application/x-rar-compressed");
152+
MEDIA_TYPES.put("ras", "image/x-cmu-raster");
153+
MEDIA_TYPES.put("rdf", "application/rdf+xml");
154+
MEDIA_TYPES.put("rgb", "image/x-rgb");
155+
MEDIA_TYPES.put("rm", "application/vnd.rn-realmedia");
156+
MEDIA_TYPES.put("roff", "application/x-troff");
157+
MEDIA_TYPES.put("rtf", "application/rtf");
158+
MEDIA_TYPES.put("rtx", "text/richtext");
159+
MEDIA_TYPES.put("sh", "application/x-sh");
160+
MEDIA_TYPES.put("shar", "application/x-shar");
161+
MEDIA_TYPES.put("shtml", "text/x-server-parsed-html");
162+
MEDIA_TYPES.put("sit", "application/x-stuffit");
163+
MEDIA_TYPES.put("smf", "audio/x-midi");
164+
MEDIA_TYPES.put("snd", "audio/basic");
165+
MEDIA_TYPES.put("src", "application/x-wais-source");
166+
MEDIA_TYPES.put("sv4cpio", "application/x-sv4cpio");
167+
MEDIA_TYPES.put("sv4crc", "application/x-sv4crc");
168+
MEDIA_TYPES.put("svg", "image/svg+xml");
169+
MEDIA_TYPES.put("svgz", "image/svg+xml");
170+
MEDIA_TYPES.put("swf", "application/x-shockwave-flash");
171+
MEDIA_TYPES.put("t", "application/x-troff");
172+
MEDIA_TYPES.put("tar", "application/x-tar");
173+
MEDIA_TYPES.put("tcl", "application/x-tcl");
174+
MEDIA_TYPES.put("tex", "application/x-tex");
175+
MEDIA_TYPES.put("texi", "application/x-texinfo");
176+
MEDIA_TYPES.put("texinfo", "application/x-texinfo");
177+
MEDIA_TYPES.put("tif", "image/tiff");
178+
MEDIA_TYPES.put("tiff", "image/tiff");
179+
MEDIA_TYPES.put("tr", "application/x-troff");
180+
MEDIA_TYPES.put("ts", "video/mp2t");
181+
MEDIA_TYPES.put("tsv", "text/tab-separated-values");
182+
MEDIA_TYPES.put("ttf", "font/ttf");
183+
MEDIA_TYPES.put("txt", "text/plain");
184+
MEDIA_TYPES.put("ulw", "audio/basic");
185+
MEDIA_TYPES.put("ustar", "application/x-ustar");
186+
MEDIA_TYPES.put("vsd", "application/vnd.visio");
187+
MEDIA_TYPES.put("vxml", "application/voicexml+xml");
188+
MEDIA_TYPES.put("wav", "audio/wav");
189+
MEDIA_TYPES.put("wbmp", "image/vnd.wap.wbmp");
190+
MEDIA_TYPES.put("weba", "audio/webm");
191+
MEDIA_TYPES.put("webm", "video/webm");
192+
MEDIA_TYPES.put("webp", "image/webp");
193+
MEDIA_TYPES.put("wml", "text/vnd.wap.wml");
194+
MEDIA_TYPES.put("wmlc", "application/vnd.wap.wmlc");
195+
MEDIA_TYPES.put("wmls", "text/vnd.wap.wmls");
196+
MEDIA_TYPES.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
197+
MEDIA_TYPES.put("woff", "font/woff");
198+
MEDIA_TYPES.put("woff2", "font/woff2");
199+
MEDIA_TYPES.put("wrl", "x-world/x-vrml");
200+
MEDIA_TYPES.put("xbm", "image/x-xbitmap");
201+
MEDIA_TYPES.put("xht", "application/xhtml+xml");
202+
MEDIA_TYPES.put("xhtml", "application/xhtml+xml");
203+
MEDIA_TYPES.put("xls", "application/vnd.ms-excel");
204+
MEDIA_TYPES.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
205+
MEDIA_TYPES.put("xml", "application/xml");
206+
MEDIA_TYPES.put("xpm", "image/x-xpixmap");
207+
MEDIA_TYPES.put("xsl", "application/xml");
208+
MEDIA_TYPES.put("xslt", "application/xslt+xml");
209+
MEDIA_TYPES.put("xul", "application/vnd.mozilla.xul+xml");
210+
MEDIA_TYPES.put("xwd", "image/x-xwindowdump");
211+
MEDIA_TYPES.put("yaml", "application/x-yaml");
212+
MEDIA_TYPES.put("yml", "application/x-yaml");
213+
MEDIA_TYPES.put("Z", "application/x-compress");
214+
MEDIA_TYPES.put("z", "application/x-compress");
215+
MEDIA_TYPES.put("zip", "application/zip");
216+
}
217+
218+
private MediaTypes() {
219+
// cannot be instanciated
220+
}
221+
222+
/**
223+
* Get the media type for the given file extension.
224+
*
225+
* @param ext file extension E.g. {@code "png"}
226+
* @return optional of media type
227+
*/
228+
public static Optional<String> of(String ext) {
229+
return Optional.ofNullable(MEDIA_TYPES.get(ext));
230+
}
231+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.build.maven.sitegen;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.OutputStream;
21+
import java.io.UncheckedIOException;
22+
import java.net.InetSocketAddress;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.concurrent.Executors;
26+
import java.util.concurrent.locks.LockSupport;
27+
28+
import io.helidon.build.common.FileUtils;
29+
import io.helidon.build.common.MediaTypes;
30+
import io.helidon.build.common.Strings;
31+
import io.helidon.build.common.logging.Log;
32+
33+
import com.sun.net.httpserver.HttpExchange;
34+
import com.sun.net.httpserver.HttpHandler;
35+
import com.sun.net.httpserver.HttpServer;
36+
37+
/**
38+
* Site server.
39+
*/
40+
public final class SiteServer {
41+
42+
private final int port;
43+
private final HttpServer server;
44+
private volatile Thread blockerThread;
45+
46+
/**
47+
* Create a new server instance.
48+
*
49+
* @param port port
50+
* @param dir directory
51+
*/
52+
public SiteServer(int port, Path dir) {
53+
this.port = port;
54+
try {
55+
server = HttpServer.create(new InetSocketAddress(8080), 0);
56+
server.createContext("/", new Handler(dir));
57+
server.setExecutor(Executors.newWorkStealingPool());
58+
} catch (IOException ex) {
59+
throw new UncheckedIOException(ex);
60+
}
61+
}
62+
63+
/**
64+
* Get the port.
65+
*
66+
* @return port
67+
*/
68+
@SuppressWarnings("unused")
69+
public int port() {
70+
return port;
71+
}
72+
73+
/**
74+
* Start the server.
75+
*/
76+
public void start() {
77+
server.start();
78+
Log.info("Site server is up! $(blue http://localhost:%d)", port);
79+
blockerThread = Thread.currentThread();
80+
LockSupport.park();
81+
}
82+
83+
/**
84+
* Stop the server.
85+
*/
86+
@SuppressWarnings("unused")
87+
public void stop() {
88+
LockSupport.unpark(blockerThread);
89+
server.stop(0);
90+
}
91+
92+
/**
93+
* Main entry-point.
94+
*
95+
* @param args args
96+
*/
97+
public static void main(String[] args) {
98+
new SiteServer(8080, Path.of(args[0])).start();
99+
}
100+
101+
private static final class Handler implements HttpHandler {
102+
103+
private final Path dir;
104+
105+
Handler(Path dir) {
106+
this.dir = dir;
107+
}
108+
109+
@Override
110+
public void handle(HttpExchange exchange) throws IOException {
111+
if (exchange.getRequestMethod().equalsIgnoreCase("GET")) {
112+
String path = exchange.getRequestURI().getPath();
113+
if (path.matches("^/.*\\.\\w{2,}$")) {
114+
serveFile(exchange, path);
115+
} else {
116+
serveFile(exchange, "/index.html");
117+
}
118+
} else {
119+
// bad request
120+
exchange.sendResponseHeaders(400, 0);
121+
}
122+
exchange.close();
123+
}
124+
125+
private void serveFile(HttpExchange exchange, String path) throws IOException {
126+
Path file = dir.resolve(Strings.stripLeading(path, '/'));
127+
if (Files.exists(file)) {
128+
String mediaType = MediaTypes.of(FileUtils.fileExt(file)).orElse("application/octet-stream");
129+
exchange.getResponseHeaders().add("Content-Type", mediaType);
130+
exchange.sendResponseHeaders(200, Files.size(file));
131+
try (OutputStream os = exchange.getResponseBody(); InputStream is = Files.newInputStream(file)) {
132+
is.transferTo(os);
133+
}
134+
} else {
135+
exchange.sendResponseHeaders(404, 0);
136+
}
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)