| 1 | /* Copyright 2001,2006,2012 Daniel F. Savarese |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * https://www.savarese.org/software/ApacheLicense-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package org.savarese.barehttp; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.io.File; |
| 20 | |
| 21 | /** |
| 22 | * The BareHTTP server, a sample program that starts an {@link HTTPServer} |
| 23 | * instance. The program expects two arguments: a port |
| 24 | * number and a fully-qualified directory pathname specifying the |
| 25 | * document root. It could easily be expanded to allow the |
| 26 | * specification of a binding address and maximum number of |
| 27 | * connections. |
| 28 | * |
| 29 | * @author <a href="https://www.savarese.org/">Daniel F. Savarese</a> |
| 30 | */ |
| 31 | public final class Main { |
| 32 | |
| 33 | private Main() {} |
| 34 | |
| 35 | // Static only so we can stop the server in a unit test. |
| 36 | static HTTPServer server = null; |
| 37 | |
| 38 | /** |
| 39 | * Starts an {@link HTTPServer} instance on a specified port |
| 40 | * allowing {@link HTTPServer#DEFAULT_MAX_CONNECTIONS}. No |
| 41 | * provisions are made for terminating the program. The program |
| 42 | * must be terminated by SIGKILL, Ctrl-C, or whatever OS-specific |
| 43 | * mechanism exists to kill a running process. |
| 44 | * |
| 45 | * @param args The program arguments: a port number and document |
| 46 | * root directory. The root directory is converted to a canonical |
| 47 | * path prior to starting the server. |
| 48 | * @throws Exception if the {@link HTTPServer} cannot be instantiated |
| 49 | * or started. |
| 50 | */ |
| 51 | public static void main(String[] args) throws IOException { |
| 52 | if(args.length != 2) { |
| 53 | System.err.println("usage: HTTPServer port documentRoot"); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | server = |
| 58 | new HTTPServer(new File(args[1]).getCanonicalPath(), |
| 59 | Integer.parseInt(args[0]), |
| 60 | HTTPServer.DEFAULT_MAX_CONNECTIONS); |
| 61 | |
| 62 | server.start(); |
| 63 | } |
| 64 | |
| 65 | } |