commit | author | age
|
9c7249
|
1 |
/* |
678b07
|
2 |
mini-server - Ein minimalistischer HTTP-Server |
U |
3 |
Copyright (C) 2021 Ulrich Hilger |
9c7249
|
4 |
|
678b07
|
5 |
This program is free software: you can redistribute it and/or modify |
U |
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
9c7249
|
9 |
|
678b07
|
10 |
This program is distributed in the hope that it will be useful, |
U |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
9c7249
|
14 |
|
678b07
|
15 |
You should have received a copy of the GNU Affero General Public License |
U |
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
d0bb21
|
17 |
*/ |
9c7249
|
18 |
package de.uhilger.minsrv; |
U |
19 |
|
|
20 |
import java.io.IOException; |
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.logging.Level; |
|
23 |
import java.util.logging.Logger; |
|
24 |
|
|
25 |
/** |
678b07
|
26 |
* Die Hauptklasse des mini-server |
d0bb21
|
27 |
* |
8abbcf
|
28 |
* @author Ulrich Hilger |
9c7249
|
29 |
* @version 0.1, 25.03.2021 |
U |
30 |
*/ |
|
31 |
public class App { |
|
32 |
|
|
33 |
private static final Logger logger = Logger.getLogger(App.class.getName()); |
d0bb21
|
34 |
|
9c7249
|
35 |
public static final String IP_PORT = "port"; |
U |
36 |
public static final String IP_WWW_DATA = "www-data"; |
678b07
|
37 |
public static final String IP_CTX = "ctx"; |
d0bb21
|
38 |
|
U |
39 |
private static HashMap initParams; |
|
40 |
|
9c7249
|
41 |
/** |
678b07
|
42 |
* Start-Methode dieser Anwendung |
d0bb21
|
43 |
* |
U |
44 |
* Folgende Kommandozeilenparameter werden verarbeitet ctx - Kontext des |
|
45 |
* Servers www-data - lokales Datenverzeichnis port - Port |
|
46 |
* |
|
47 |
* Beispiel: java -jar mini-server.jar ctx="srv" www-data="/home/fred/www" |
|
48 |
* port=9090 |
|
49 |
* |
|
50 |
* Startet den Server auf http://localhost:9090/srv und liefert Inhalte aus |
|
51 |
* dem Verzeichnis /home/fred/www aus. |
|
52 |
* |
|
53 |
* Ein Aufruf von http://localhost:9090/srv/pfad/zum/inhalt/index.html liefert |
|
54 |
* also die Datei 'index.html' aus dem Ordner /home/fred/www/pfad/zum/inhalt |
|
55 |
* aus. |
|
56 |
* |
678b07
|
57 |
* @param args Kommandozeilenparameter |
9c7249
|
58 |
*/ |
U |
59 |
public static void main(String[] args) { |
|
60 |
initParams = new HashMap(); |
d0bb21
|
61 |
for (String arg : args) { |
9c7249
|
62 |
String[] argParts = arg.split("="); |
U |
63 |
initParams.put(argParts[0], argParts[1]); |
|
64 |
} |
d0bb21
|
65 |
|
U |
66 |
String portStr = getInitParameter(IP_PORT); |
|
67 |
if (portStr != null) { |
|
68 |
Server server = new Server(Integer.parseInt(portStr)); |
|
69 |
try { |
|
70 |
String ctxName = getInitParameter(IP_CTX); |
|
71 |
if (ctxName != null) { |
|
72 |
server.setContextName(ctxName); |
|
73 |
server.start(); |
|
74 |
} else { |
|
75 |
logger.severe("Der Parameter " + IP_CTX + " muss angegeben werden."); |
|
76 |
} |
|
77 |
} catch (IOException ex) { |
|
78 |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); |
|
79 |
} |
|
80 |
} else { |
|
81 |
logger.severe("Der Parameter " + IP_PORT + " muss angegeben werden."); |
9c7249
|
82 |
} |
U |
83 |
} |
d0bb21
|
84 |
|
678b07
|
85 |
/** |
U |
86 |
* Diese Anwendung stoppen |
|
87 |
*/ |
9c7249
|
88 |
public static void stop() { |
U |
89 |
System.exit(0); |
|
90 |
} |
d0bb21
|
91 |
|
678b07
|
92 |
/** |
U |
93 |
* Einen Kommandozeilenparameter ermitteln |
d0bb21
|
94 |
* |
678b07
|
95 |
* @param pname Names des Parameters |
d0bb21
|
96 |
* @return Inhalt des Parameters oder null, wenn der Parameter nicht gefunden |
U |
97 |
* wurde |
678b07
|
98 |
*/ |
9c7249
|
99 |
public static String getInitParameter(String pname) { |
U |
100 |
String param = null; |
|
101 |
Object o = initParams.get(pname); |
d0bb21
|
102 |
if (o != null) { |
9c7249
|
103 |
param = o.toString(); |
U |
104 |
} |
|
105 |
return param; |
d0bb21
|
106 |
} |
9c7249
|
107 |
} |