-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from UMM-CSci-3601/update-server-to-match-iter…
…ation-template Bring in changes to the server from Lab 3 and iteration template
- Loading branch information
Showing
9 changed files
with
556 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package umm3601; | ||
|
||
import io.javalin.Javalin; | ||
|
||
/** | ||
* Interface for classes that can add routes to a Javalin server. | ||
* | ||
* Any class that implements this interface can be used to add routes to the | ||
* server via the specified `addRoutes()` method. | ||
* | ||
* This is useful for organizing routes into separate files, and for testing | ||
* routes without starting the server (except that the inability to compare | ||
* lambdas in fact makes this very hard to test). | ||
* | ||
* Any controller class that provides routes for the Javalin server | ||
* must implement this interface since the `Server` class | ||
* is just handed an array of `Controller` objects in its constructor. This | ||
* allows us to add routes to the server without having to modify the `Server`, | ||
* and without having the server know about any specific controller implementations. | ||
* | ||
* Note that this interface definition is _complete_ and you shouldn't need to | ||
* add anything to it. You just need to make sure that any new controllers | ||
* you implement also implement this interface, providing their own `addRoutes()` | ||
* method. | ||
*/ | ||
public interface Controller { | ||
/** | ||
* Add routes to the server. | ||
* | ||
* If you have a controller that implements this interface, for example, | ||
* your implementation of `addRoutes()` would add all the routes for your | ||
* controller's datatype, by calling | ||
* `server.get(...)`, `server.post(...)`, etc. | ||
* | ||
* @param server The Javalin server to add routes to | ||
*/ | ||
void addRoutes(Javalin server); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package umm3601; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoDatabase; | ||
|
||
import umm3601.user.UserController; | ||
|
||
public class Main { | ||
|
||
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 = Main.getEnvOrDefault("MONGO_ADDR", "localhost"); | ||
String databaseName = Main.getEnvOrDefault("MONGO_DB", "dev"); | ||
|
||
// Set up the MongoDB client | ||
MongoClient mongoClient = Server.configureDatabase(mongoAddr); | ||
// Get the database | ||
MongoDatabase database = mongoClient.getDatabase(databaseName); | ||
|
||
// The implementations of `Controller` used for the server. These will presumably | ||
// be one or more controllers, each of which implements the `Controller` interface. | ||
// You'll add your own controllers in `getControllers` as you create them. | ||
final Controller[] controllers = Main.getControllers(database); | ||
|
||
// Construct the server | ||
Server server = new Server(mongoClient, controllers); | ||
|
||
// Start the server | ||
server.startServer(); | ||
} | ||
|
||
/** | ||
* Get the value of an environment variable, or return a default value if it's not set. | ||
* | ||
* @param envName The name of the environment variable to get | ||
* @param defaultValue The default value to use if the environment variable isn't set | ||
* | ||
* @return The value of the environment variable, or the default value if it's not set | ||
*/ | ||
static String getEnvOrDefault(String envName, String defaultValue) { | ||
return System.getenv().getOrDefault(envName, defaultValue); | ||
} | ||
|
||
/** | ||
* Get the implementations of `Controller` used for the server. | ||
* | ||
* These will presumably be one or more controllers, each of which | ||
* implements the `Controller` interface. You'll add your own controllers | ||
* in to the array returned by this method as you create them. | ||
* | ||
* @param database The MongoDB database object used by the controllers | ||
* to access the database. | ||
* @return An array of implementations of `Controller` for the server. | ||
*/ | ||
static Controller[] getControllers(MongoDatabase database) { | ||
Controller[] controllers = new Controller[] { | ||
// You would add additional controllers here, as you create them, | ||
// although you need to make sure that each of your new controllers implements | ||
// the `Controller` interface. | ||
// | ||
// You can also remove this UserController once you don't need it. | ||
new UserController(database) | ||
}; | ||
return controllers; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.