-
Notifications
You must be signed in to change notification settings - Fork 2
/
Server.java
88 lines (59 loc) · 2.66 KB
/
Server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package umm3601;
import java.util.Arrays;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import io.javalin.Javalin;
import umm3601.owner.OwnerController;
import umm3601.note.NoteController;
public class Server {
static String appName = "CSCI 3601 Announce It!";
public static final String OWNER_DATA_FILE = "/owners.json";
public static final String POST_DATA_FILE = "/notes.json";
private static MongoDatabase database;
public static void main(String[] args) {
// Get the MongoDB address and database name from environment variables and
// if they aren't set, use the defaults of "localhost" and "dev".
String mongoAddr = System.getenv().getOrDefault("MONGO_ADDR", "localhost");
String databaseName = System.getenv().getOrDefault("MONGO_DB", "dev");
// Setup the MongoDB client object with the information we set earlier
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress(mongoAddr))))
.build());
// Get the database
database = mongoClient.getDatabase(databaseName);
// Initialize dependencies
OwnerController ownerController = new OwnerController(database);
//OwnerRequestHandler ownerRequestHandler = new OwnerRequestHandler(ownerController);
NoteController noteController = new NoteController(database);
Javalin server = Javalin.create().start(4567);
// Simple example route
server.get("hello", ctx -> ctx.result("Hello World"));
// Utility routes
server.get("api", ctx -> ctx.result(appName));
// Get specific owner
// this is wrong, need to fix this later
server.get("api/owners/:id", ownerController::getOwner);
server.delete("api/owners/:id", ownerController::deleteOwner);
// List owners, filtered using query parameters
server.get("api/owners", ownerController::getOwners);
// Add new owner
server.post("api/owners/new", ownerController::addNewOwner);
//Get all notes from an owner
server.get("api/owner/:id/notes", noteController::getOwnerNotes);
server.delete("api/notes/:id", noteController::deleteNote);
//List notes with filters
server.get("api/notes", noteController::getOwnerNotes);
//Add new note
//From the owner's doorboard
server.post("api/owner/:id/notes/new", noteController::addNewNote);
server.exception(Exception.class, (e, ctx) -> {
ctx.status(500);
ctx.json(e); // you probably want to remove this in production
});
}
}