58 lines
1.8 KiB
Java
58 lines
1.8 KiB
Java
package it.cavallium.strangedb.server;
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
import it.cavallium.strangedb.server.http.ExistsHandler;
|
|
import it.cavallium.strangedb.server.http.GetHandler;
|
|
import it.cavallium.strangedb.server.http.SetHandler;
|
|
import it.cavallium.strangedb.server.http.SizeHandler;
|
|
|
|
import java.io.IOException;
|
|
import java.net.InetSocketAddress;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
|
|
public class Server {
|
|
private final DatabaseSimple database;
|
|
|
|
public Server(DatabaseSimple database) throws IOException {
|
|
this.database = database;
|
|
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
|
server.createContext("/get", new GetHandler(this));
|
|
server.createContext("/set", new SetHandler(this));
|
|
server.createContext("/exists", new ExistsHandler(this));
|
|
server.createContext("/size", new SizeHandler(this));
|
|
server.setExecutor(null);
|
|
server.start();
|
|
}
|
|
|
|
/**
|
|
* <p>Start the server</p>
|
|
* <code>java -jar server.jar <database-directory></code>
|
|
* @param args
|
|
*/
|
|
public static void main(String[] args) throws IOException {
|
|
if (args.length != 1) {
|
|
System.err.println("Usage: java -jar server.jar <database-directory>");
|
|
}
|
|
Path databasePath = Paths.get(args[0]);
|
|
DatabaseSimple database = new DatabaseSimple(databasePath.resolve("database.db"), databasePath.resolve("database.blocks.dat"), databasePath.resolve("database.references.dat"));
|
|
new Server(database);
|
|
}
|
|
|
|
public boolean exists(CharSequence path) throws IOException {
|
|
return database.exists(path);
|
|
}
|
|
|
|
public int size(CharSequence path) throws IOException {
|
|
return database.size(path);
|
|
}
|
|
|
|
public String get(CharSequence path) throws IOException {
|
|
return database.get(path);
|
|
}
|
|
|
|
public void set(CharSequence path, String value) throws IOException {
|
|
database.set(path, value);
|
|
}
|
|
}
|