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 |
* |
9c7249
|
29 |
* @author ulrich |
U |
30 |
* @version 0.1, 25.03.2021 |
|
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 { |
|
71 |
server.start(); |
|
72 |
} catch (IOException ex) { |
|
73 |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); |
|
74 |
} |
|
75 |
} |
|
76 |
|
678b07
|
77 |
/** |
U |
78 |
* Diese Anwendung stoppen |
|
79 |
*/ |
9c7249
|
80 |
public static void stop() { |
U |
81 |
System.exit(0); |
|
82 |
} |
|
83 |
|
678b07
|
84 |
/** |
U |
85 |
* Einen Kommandozeilenparameter ermitteln |
|
86 |
* |
|
87 |
* @param pname Names des Parameters |
|
88 |
* @return Inhalt des Parameters oder null, wenn der Parameter |
|
89 |
* nicht gefunden wurde |
|
90 |
*/ |
9c7249
|
91 |
public static String getInitParameter(String pname) { |
U |
92 |
String param = null; |
|
93 |
Object o = initParams.get(pname); |
|
94 |
if(o != null) { |
|
95 |
param = o.toString(); |
|
96 |
} |
|
97 |
return param; |
|
98 |
} |
|
99 |
} |