Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/lavajuno/fsuvius
Browse files Browse the repository at this point in the history
  • Loading branch information
lavajuno committed Sep 8, 2023
2 parents 5721c51 + cf31298 commit ab0a528
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 341 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
users.dat
data/photos/*
.idea/workspace.xml
.idea/vcs.xml

Expand Down
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 56 additions & 18 deletions src/main/java/org/jmeifert/fsuvius/FsuviusController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Refill;
import org.jmeifert.fsuvius.data.DatabaseController;
import org.jmeifert.fsuvius.error.NotFoundException;
import org.jmeifert.fsuvius.error.RateLimitException;
import org.jmeifert.fsuvius.user.User;
import org.jmeifert.fsuvius.user.UserRegistry;
import org.jmeifert.fsuvius.util.Log;
import org.springframework.web.bind.annotation.*;

Expand All @@ -18,23 +19,25 @@
@RestController
public class FsuviusController {
private final Log log;
private final int MAX_REQUESTS_PER_5S = 100;

private final Bucket bucket;
private UserRegistry userRegistry;
private DatabaseController databaseController;

/**
* Instantiates a FsuviusController.
*/
public FsuviusController() {
log = new Log("FsuviusController");
log.print("Starting up...");
userRegistry = new UserRegistry();
Bandwidth limit= Bandwidth.classic(MAX_REQUESTS_PER_5S,
Refill.greedy(MAX_REQUESTS_PER_5S, Duration.ofSeconds(5)));
databaseController = new DatabaseController();
Bandwidth limit= Bandwidth.classic(FsuviusMap.MAX_REQUESTS_PER_SECOND,
Refill.greedy(FsuviusMap.MAX_REQUESTS_PER_SECOND, Duration.ofSeconds(1)));
this.bucket = Bucket.builder().addLimit(limit).build();
log.print("Initialization complete. Welcome to Mount Fsuvius.");
log.print("===== Init complete. Welcome to Mount Fsuvius. =====");
}

/* ===== BANK TOTALS ===== */

/**
* Gets the total amount of FSU in the bank.
* @return The total amount of FSU in the bank
Expand All @@ -43,25 +46,26 @@ public FsuviusController() {
public float getBankBalance() {
if(bucket.tryConsume(1)) {
float bal = 0.0F;
for(User i : userRegistry.getAll()) {
for(User i : databaseController.getUsers()) {
bal += i.getBalance();
}
return bal;
}
throw new RateLimitException();
}

/* ===== USERS ===== */

/**
* Gets all registered Users.
* @return All Users
*/
@GetMapping("/api/users")
public List<User> getUsers() {
if(bucket.tryConsume(1)) {
return userRegistry.getAll();
return databaseController.getUsers();
}
throw new RateLimitException();

}

/**
Expand All @@ -73,21 +77,20 @@ public List<User> getUsers() {
public User newUser(@RequestBody String name) {
if(bucket.tryConsume(1)) {
log.print("Handling request to create new user with name \"" + name + "\".");
return userRegistry.createUser(name);
return databaseController.createUser(name);
}
throw new RateLimitException();

}

/**
* Gets a single user.
* @param id The ID of the user to get
* @return The User with the specified ID
*/
@GetMapping("/api/user/{id}")
@GetMapping("/api/users/{id}")
public User getUser(@PathVariable String id) {
if(bucket.tryConsume(1)) {
return userRegistry.getUser(id);
return databaseController.getUser(id);
}
throw new RateLimitException();
}
Expand All @@ -98,11 +101,11 @@ public User getUser(@PathVariable String id) {
* @param id ID of the User to edit
* @return The edited User
*/
@PutMapping("/api/user/{id}")
@PutMapping("/api/users/{id}")
public User editUser(@RequestBody User newUser, @PathVariable String id) {
if(bucket.tryConsume(1)) {
log.print("Handling request to edit user at ID \"" + id + "\".");
return userRegistry.editUser(id, newUser);
return databaseController.editUser(id, newUser);
}
throw new RateLimitException();
}
Expand All @@ -111,13 +114,48 @@ public User editUser(@RequestBody User newUser, @PathVariable String id) {
* Deletes a User with the given ID.
* @param id The ID of the user to delete
*/
@DeleteMapping("/api/user/{id}")
@DeleteMapping("/api/users/{id}")
public void deleteUser(@PathVariable String id) {
if(bucket.tryConsume(1)) {
log.print("Handling request to delete user at ID \"" + id + "\".");
userRegistry.deleteUser(id);
databaseController.deleteUser(id);
} else {
throw new RateLimitException();
}
}

/* ===== PHOTOS ===== */

/**
* Gets a photo by ID.
* Will result in an HTTP 404 if the photo cannot be found.
* @param id ID of the photo to get
* @return The photo with the specified ID
*/
@GetMapping(value = "api/photos/{id}")
public byte[] getPhoto(@PathVariable String id) {
if(bucket.tryConsume(1)) {
try {
return databaseController.readPhoto(id);
} catch(NotFoundException e) {
log.print(1, "Couldn't find photo " + id + " in database.");
throw new NotFoundException();
}
}
throw new RateLimitException();
}

/**
* Updates a photo by ID. Will create it if it does not already exist.
* @param item New content of the photo
* @param id ID of the photo to update
*/
@PostMapping("api/photos/{id}")
public void putPhoto(@RequestBody String item, @PathVariable String id) {
if(bucket.tryConsume(1)) {
databaseController.writePhoto(item, id);
return;
}
throw new RateLimitException();
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/jmeifert/fsuvius/FsuviusMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jmeifert.fsuvius;

/**
* FsuviusMap contains user-modifiable constants that are used elsewhere in the program.
*/
public class FsuviusMap {
/**
* The maximum amount of requests per second the server will handle
* before refusing additional requests.
*/
public static final int MAX_REQUESTS_PER_SECOND = 50;

/**
* The default photo for new users as base64.
*/
public static final String DEFAULT_PHOTO = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAC4jAAAuIwF4pT92AAAE1ElEQVR42u2aSSxzXRjH/17S1xxi2hQxJKKhK7ERmyKIjQS7mrc2iAgLiaRiQcSiIcFGKjEtiBJi2phJDA2q2mKB1jxEWzqdb9d8l1YRXrTnn9zFc+5z7rnnl3Oe+5yndSGEEDix/sDJRQFQABQABUABUAAUAAVAAVAAFIBzyu21m2azGTKZDKurq1hfX4dMJsPGxgbOz88tPru7u4iNjf29BIgNXV1dkaqqKgLg1Wt3d5f8ZlkFoNPpSGlpqd3JOyyAqakpxiRLS0uJVColWq2WmM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ss7MzotfricFgINfX12RtbY1UVFRY/Pr7+20WRD4dwEeu5uZm8vT0ZHeci4sLkpmZ+a5ni0QiotPpbAL41hiQlpaGubk5lJeXg8Vi2fUPDAyESCRCZWWlXd+kpCTMz8+Dz+fDxcXla/OA3Nxc8Hg8qNVqnJyc4PDwEGq1GpeXlzg9PYVUKoW3tzciIiLA4XDA4XAQHx//ocNQQEAAmpqaUFJSgtXVVSwtLUEqlUKj0VgOWMnJyYiPj3+RP1iTC/27vJOLAqAAKAAKgAKgACgACoACoAAoAAqAAqAAnFD/AZZ5oHgkh6rAAAAAAElFTkSuQmCC";

/**
* The maximum allowed size for photos. 1MB recommended.
*/
public static final int MAX_PHOTO_SIZE = 1024 * 1024;
}
Loading

0 comments on commit ab0a528

Please sign in to comment.