Implement a basic configuration system

This commit is contained in:
Colin McDonald 2016-04-28 15:58:58 -04:00
parent e8bdcdc6a5
commit a07d9f524b
2 changed files with 29 additions and 12 deletions

View File

@ -8,7 +8,6 @@ import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import lombok.Getter;
import net.frozenorb.apiv3.models.Rank;
import net.frozenorb.apiv3.models.Server;
import net.frozenorb.apiv3.models.ServerGroup;
import net.frozenorb.apiv3.routes.GETDump;
import net.frozenorb.apiv3.routes.GETRoutes;
@ -29,17 +28,16 @@ import net.frozenorb.apiv3.routes.serverGroups.GETServerGroups;
import net.frozenorb.apiv3.routes.servers.GETServer;
import net.frozenorb.apiv3.routes.servers.GETServers;
import net.frozenorb.apiv3.routes.users.*;
import net.frozenorb.apiv3.weirdStuff.ActorAttributeFilter;
import net.frozenorb.apiv3.weirdStuff.ContentTypeFilter;
import net.frozenorb.apiv3.weirdStuff.FollowAnnotationExclusionStrategy;
import net.frozenorb.apiv3.weirdStuff.ObjectIdTypeAdapter;
import net.frozenorb.apiv3.weirdStuff.*;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import spark.Request;
import spark.Spark;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static spark.Spark.*;
@ -47,8 +45,15 @@ public final class APIv3 {
@Getter private static Datastore datastore;
private final Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).setExclusionStrategies(new FollowAnnotationExclusionStrategy()).create();
private final Properties config = new Properties();
APIv3() {
try {
config.load(APIv3.class.getClassLoader().getResourceAsStream("apiv3.properties"));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
setupDatabase();
setupHttp();
@ -84,20 +89,25 @@ public final class APIv3 {
}
private void setupDatabase() {
MongoClient mongoClient = new MongoClient(new ServerAddress("ds055505.mongolab.com", 55505),
MongoClient mongoClient = new MongoClient(new ServerAddress((String) config.get("mongo.address"), (Integer) config.get("mongo.port")),
ImmutableList.of(
MongoCredential.createCredential("test", "minehqapi", "test".toCharArray())
MongoCredential.createCredential(
(String) config.get("mongo.username"),
(String) config.get("mongo.database"),
((String) config.get("mongo.password")).toCharArray())
));
Morphia morphia = new Morphia();
morphia.mapPackage("net.frozenorb.apiv3.accessor");
datastore = morphia.createDatastore(mongoClient, "minehqapi");
datastore = morphia.createDatastore(mongoClient, (String) config.get("mongo.database"));
datastore.ensureIndexes();
}
private void setupHttp() {
port(80);
ipAddress((String) config.get("http.address"));
port((Integer) config.get("http.port"));
before(new ContentTypeFilter());
before(new ActorAttributeFilter());
get("/announcements", new GETAnnouncements(), gson::toJson);
@ -135,13 +145,13 @@ public final class APIv3 {
get("/dump/:type", new GETDump(), gson::toJson);
get("/routes", new GETRoutes());
after(new ContentTypeFilter());
}
public static void requireAuthorizedActor(Request req) {
Actor actor = req.attribute("actor");
if (!req.attribute("actor").)
if (!actor.isAuthorized()) {
Spark.halt();
}
}
}

View File

@ -0,0 +1,7 @@
mongo.address=ds055505.mongolab.com
mongo.port=55505
mongo.database=minehqapi
mongo.username=test
mongo.password=test
http.address=
http.port=80