generated from UMM-CSci-3601/3601-iteration-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.java
68 lines (46 loc) · 2 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
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.user.UserController;
public class Server {
static String appName = "CSCI 3601 Iteration Template";
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
UserController userController = new UserController(database);
//UserRequestHandler userRequestHandler = new UserRequestHandler(userController);
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 user
server.get("api/users/:id", userController::getUser);
server.delete("api/users/:id", userController::deleteUser);
// List users, filtered using query parameters
server.get("api/users", userController::getUsers);
// Add new user
server.post("api/users/new", userController::addNewUser);
server.exception(Exception.class, (e, ctx) -> {
ctx.status(500);
ctx.json(e); // you probably want to remove this in production
});
}
}