commit | author | age
|
00c9b9
|
1 |
/* |
U |
2 |
Proto - Ein Rumpf-Konstrukt fuer Webapps |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
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. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
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. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.proto; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpServer; |
|
21 |
import java.io.IOException; |
|
22 |
import java.net.InetSocketAddress; |
|
23 |
import java.util.HashMap; |
|
24 |
import java.util.ResourceBundle; |
|
25 |
import java.util.concurrent.Executors; |
|
26 |
import java.util.logging.Level; |
|
27 |
import java.util.logging.Logger; |
|
28 |
|
|
29 |
/** |
|
30 |
* Die Hauptklasse der Proto-App |
|
31 |
* |
|
32 |
* Starten mit |
|
33 |
* java -jar proto.jar ctx=/proto www=./www port=8008 |
|
34 |
* |
|
35 |
* @author Ulrich Hilger |
|
36 |
* @version 1, 11.5.2021 |
|
37 |
*/ |
|
38 |
public class App { |
|
39 |
|
|
40 |
/* Name des ResourceBundles dieser App */ |
|
41 |
public static final String RB_NAME = "proto"; |
|
42 |
|
|
43 |
public static final String RB_SERVER_START_MSG = "msgServerStart"; |
|
44 |
|
|
45 |
public static final String API_SERVER_STOP = "/api/server/stop"; |
|
46 |
public static final String STR_SLASH = "/"; |
|
47 |
|
|
48 |
public static final String ARG_WWW = "www"; |
|
49 |
public static final String ARG_CTX = "ctx"; |
|
50 |
public static final String ARG_PORT = "port"; |
|
51 |
|
|
52 |
/** |
|
53 |
* Parameter: |
|
54 |
* port=[portnr] (9123) |
|
55 |
* ctx=[context] (/proto) |
|
56 |
* www=[www-dir] ("./www") |
|
57 |
* |
|
58 |
* @param args the command line arguments |
|
59 |
*/ |
|
60 |
public static void main(String[] args) { |
|
61 |
|
|
62 |
try { |
|
63 |
ResourceBundle rb = ResourceBundle.getBundle(RB_NAME); |
|
64 |
HashMap<String,String> initParams = new HashMap(); |
|
65 |
for (String arg : args) { |
|
66 |
String[] argParts = arg.split("="); |
|
67 |
initParams.put(argParts[0], argParts[1]); |
|
68 |
} |
|
69 |
Logger.getLogger(App.class.getName()).log(Level.INFO, rb.getString(RB_SERVER_START_MSG), initParams.get(ARG_PORT)); |
|
70 |
|
|
71 |
String ctx = initParams.get(ARG_CTX); |
|
72 |
HttpServer server = HttpServer.create(new InetSocketAddress(Integer.parseInt(initParams.get(ARG_PORT))), 0); |
|
73 |
server.createContext(ctx + STR_SLASH, new FileHandler(initParams.get(ARG_WWW))); |
|
74 |
server.createContext(ctx + API_SERVER_STOP, new StopServerHandler()); |
|
75 |
server.setExecutor(Executors.newCachedThreadPool()); |
|
76 |
server.start(); |
|
77 |
} catch (IOException ex) { |
|
78 |
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
} |