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 com.sun.net.httpserver.HttpServer; |
|
22 |
import de.uhilger.minsrv.handler.StopServerHandler; |
|
23 |
import de.uhilger.minsrv.handler.FileHandler; |
|
24 |
import java.io.IOException; |
|
25 |
import java.util.logging.Logger; |
|
26 |
import java.net.InetSocketAddress; |
|
27 |
import java.util.concurrent.Executors; |
|
28 |
|
|
29 |
/** |
678b07
|
30 |
* Die Klasse Server stellt Methoden zur Ausführung eines |
U |
31 |
* HTTP-Servers bereit |
|
32 |
* |
9c7249
|
33 |
* @author ulrich |
678b07
|
34 |
* @version 0.1, 25.03.2021 |
9c7249
|
35 |
*/ |
U |
36 |
public class Server { |
|
37 |
|
|
38 |
private static final Logger logger = Logger.getLogger(Server.class.getName()); |
|
39 |
|
|
40 |
private int port; |
|
41 |
|
678b07
|
42 |
private String ctxName; |
U |
43 |
|
9c7249
|
44 |
public Server(int port) { |
U |
45 |
this.port = port; |
|
46 |
} |
|
47 |
|
678b07
|
48 |
/** |
U |
49 |
* Den Port angeben, unter dem der Server erreichbar sein soll |
|
50 |
* |
|
51 |
* @param port der Port, unter dem der Server erreichbar sein soll |
|
52 |
*/ |
9c7249
|
53 |
public void setPort(int port) { |
U |
54 |
this.port = port; |
|
55 |
} |
|
56 |
|
678b07
|
57 |
public void setContextName(String ctxName) { |
U |
58 |
this.ctxName = ctxName; |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Die Endpunkte ('Context'e) einrichten, unter denen die Dienste |
|
63 |
* dieses Servers erreichbar sein sollen und den Server starten |
|
64 |
* |
|
65 |
* @throws IOException wenn etwas schief geht, finden sich Angaben |
|
66 |
* in diesem Objekt |
|
67 |
*/ |
9c7249
|
68 |
public void start() throws IOException { |
U |
69 |
logger.info("Server starting on port " + port); |
|
70 |
|
|
71 |
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
678b07
|
72 |
server.createContext(ctxName + "/av", new FileHandler(App.getInitParameter(App.IP_WWW_DATA))); |
U |
73 |
server.createContext(ctxName + "/server/stop", new StopServerHandler()); |
9c7249
|
74 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
U |
75 |
server.start(); |
|
76 |
} |
|
77 |
|
|
78 |
} |