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/>. |
9c7249
|
17 |
*/ |
U |
18 |
|
|
19 |
package de.uhilger.minsrv; |
|
20 |
|
|
21 |
import java.io.IOException; |
|
22 |
import java.util.HashMap; |
|
23 |
import java.util.logging.Level; |
|
24 |
import java.util.logging.Logger; |
|
25 |
|
|
26 |
/** |
678b07
|
27 |
* Die Hauptklasse des mini-server |
U |
28 |
* |
8abbcf
|
29 |
* @author Ulrich Hilger |
9c7249
|
30 |
* @version 0.1, 25.03.2021 |
U |
31 |
*/ |
|
32 |
public class App { |
|
33 |
|
|
34 |
private static final Logger logger = Logger.getLogger(App.class.getName()); |
|
35 |
|
|
36 |
public static final String IP_PORT = "port"; |
|
37 |
public static final String IP_WWW_DATA = "www-data"; |
678b07
|
38 |
public static final String IP_CTX = "ctx"; |
9c7249
|
39 |
|
U |
40 |
private static HashMap initParams; |
|
41 |
|
|
42 |
/** |
678b07
|
43 |
* Start-Methode dieser Anwendung |
U |
44 |
* |
|
45 |
* Folgende Kommandozeilenparameter werden verarbeitet |
|
46 |
* ctx - Kontext des Servers |
|
47 |
* www-data - lokales Datenverzeichnis |
|
48 |
* port - Port |
|
49 |
* |
|
50 |
* Beispiel: |
|
51 |
* java -jar mini-server.jar ctx="srv" www-data="/home/fred/www" port=9090 |
|
52 |
* |
|
53 |
* Startet den Server auf http://localhost:9090/srv |
|
54 |
* und liefert Inhalte aus dem Verzeichnis /home/fred/www aus. |
|
55 |
* |
|
56 |
* Ein Aufruf von http://localhost:9090/srv/pfad/zum/inhalt/index.html |
|
57 |
* liefert also die Datei 'index.html' aus dem Ordner |
|
58 |
* /home/fred/www/pfad/zum/inhalt aus. |
|
59 |
* |
|
60 |
* @param args Kommandozeilenparameter |
9c7249
|
61 |
*/ |
U |
62 |
public static void main(String[] args) { |
|
63 |
initParams = new HashMap(); |
|
64 |
for(String arg: args) { |
|
65 |
String[] argParts = arg.split("="); |
|
66 |
initParams.put(argParts[0], argParts[1]); |
|
67 |
} |
|
68 |
|
|
69 |
Server server = new Server(Integer.parseInt(getInitParameter(IP_PORT))); |
|
70 |
try { |
51e1d5
|
71 |
server.setContextName(getInitParameter(IP_CTX)); |
9c7249
|
72 |
server.start(); |
U |
73 |
} catch (IOException ex) { |
|
74 |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); |
|
75 |
} |
|
76 |
} |
|
77 |
|
678b07
|
78 |
/** |
U |
79 |
* Diese Anwendung stoppen |
|
80 |
*/ |
9c7249
|
81 |
public static void stop() { |
U |
82 |
System.exit(0); |
|
83 |
} |
|
84 |
|
678b07
|
85 |
/** |
U |
86 |
* Einen Kommandozeilenparameter ermitteln |
|
87 |
* |
|
88 |
* @param pname Names des Parameters |
|
89 |
* @return Inhalt des Parameters oder null, wenn der Parameter |
|
90 |
* nicht gefunden wurde |
|
91 |
*/ |
9c7249
|
92 |
public static String getInitParameter(String pname) { |
U |
93 |
String param = null; |
|
94 |
Object o = initParams.get(pname); |
|
95 |
if(o != null) { |
|
96 |
param = o.toString(); |
|
97 |
} |
|
98 |
return param; |
|
99 |
} |
|
100 |
} |