commit | author | age
|
929228
|
1 |
/* |
U |
2 |
Calypso - Media Player Remote Control via HTTP for Raspberry Pi |
|
3 |
Copyright (C) 2021-2023 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 |
|
e27ab1
|
19 |
package de.uhilger.calypso.http; |
929228
|
20 |
|
U |
21 |
import com.sun.net.httpserver.HttpContext; |
|
22 |
import com.sun.net.httpserver.HttpServer; |
e27ab1
|
23 |
import de.uhilger.calypso.AppProperties; |
929228
|
24 |
import java.io.File; |
U |
25 |
import java.io.IOException; |
|
26 |
import java.net.InetSocketAddress; |
|
27 |
import java.util.concurrent.Executors; |
|
28 |
import java.util.logging.Level; |
|
29 |
import java.util.logging.Logger; |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
* @author Ulrich Hilger |
|
34 |
*/ |
|
35 |
public class Server { |
|
36 |
|
|
37 |
//private static final Logger logger = Logger.getLogger(de.uhilger.calypso.Server.class.getName()); |
|
38 |
|
|
39 |
public static final String CONF = "conf"; |
|
40 |
public static final String PORT = "port"; |
|
41 |
public static final String SKRIPTE = "skripte"; |
|
42 |
public static final String CTX = "ctx"; |
|
43 |
public static final String EQUAL = "="; |
|
44 |
public static final String BLANK = " "; |
|
45 |
public static final String SKRIPT_DIR = "skript-dir"; |
|
46 |
|
|
47 |
|
|
48 |
public void start(AppProperties einst) { |
e27ab1
|
49 |
Logger logger = Logger.getLogger(de.uhilger.calypso.http.Server.class.getName()); |
929228
|
50 |
logger.log(Level.INFO, "Server startet auf Port {0}", einst.getString(PORT)); |
U |
51 |
String skripte = einst.getString(SKRIPTE); |
|
52 |
File skript = new File(skripte, "pause"); |
|
53 |
if(skript.exists()) { |
|
54 |
logger.log(Level.INFO, "Skripte gefunden in {0}", skript.getParentFile().getAbsolutePath()); |
|
55 |
} else { |
|
56 |
logger.log(Level.INFO, "Skripte nicht gefunden, Ordner laut Einstellungen: {0}", skripte); |
|
57 |
} |
|
58 |
try { |
|
59 |
HttpServer server = HttpServer.create(new InetSocketAddress(einst.getInt(PORT)), 0); |
|
60 |
String ctx = einst.getString(CTX); |
|
61 |
HttpContext context = server.createContext(ctx, new ApiHandler()); |
|
62 |
context.getAttributes().put(SKRIPT_DIR, einst.getString(SKRIPTE)); |
|
63 |
server.setExecutor(Executors.newFixedThreadPool(20)); |
|
64 |
server.start(); |
|
65 |
} catch (IOException ex) { |
|
66 |
logger.log(Level.SEVERE, null, ex); |
|
67 |
} |
|
68 |
} |
|
69 |
} |