/* Calypso - Media Player Remote Control via HTTP for Raspberry Pi Copyright (C) 2021-2023 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.calypso.http; import com.sun.net.httpserver.HttpContext; import com.sun.net.httpserver.HttpServer; import de.uhilger.calypso.AppProperties; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Ulrich Hilger */ public class Server { //private static final Logger logger = Logger.getLogger(de.uhilger.calypso.Server.class.getName()); public static final String CONF = "conf"; public static final String PORT = "port"; public static final String SKRIPTE = "skripte"; public static final String CTX = "ctx"; public static final String EQUAL = "="; public static final String BLANK = " "; public static final String SKRIPT_DIR = "skript-dir"; public void start(AppProperties einst) { Logger logger = Logger.getLogger(de.uhilger.calypso.http.Server.class.getName()); logger.log(Level.INFO, "Server startet auf Port {0}", einst.getString(PORT)); String skripte = einst.getString(SKRIPTE); File skript = new File(skripte, "pause"); if(skript.exists()) { logger.log(Level.INFO, "Skripte gefunden in {0}", skript.getParentFile().getAbsolutePath()); } else { logger.log(Level.INFO, "Skripte nicht gefunden, Ordner laut Einstellungen: {0}", skripte); } try { HttpServer server = HttpServer.create(new InetSocketAddress(einst.getInt(PORT)), 0); String ctx = einst.getString(CTX); HttpContext context = server.createContext(ctx, new ApiHandler()); context.getAttributes().put(SKRIPT_DIR, einst.getString(SKRIPTE)); server.setExecutor(Executors.newFixedThreadPool(20)); server.start(); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } } }