Online Judge from Scratch(1) – Frontend

The frontend of Justice contains two sites: the web UI for users and the admin panel for administrators, the main reason to choose Yii2 is the Advanced Application Template provides both succinct project structure and great convenience to share the same logic between the two sites:

Besides, we improved Yii2’s MVC pattern by adding an additional service layer and an abstract presenter layer. The main purpose of adding a service layer is to avoid service dependent codes or logic calculations appear in controllers or views. Nowadays adding a service layer between controllers and models becomes more and more popular, since modern Web frameworks written in Java or PHP can easily manipulate controller-service dependencies by IoC(which is mainly implemented by reflection). Best practices on service layer can be found easily, so we won’t talk much here. In Justice, we also add a presenter layer to translate constant variables into HTML elements(to get rid of the annoying if-else statement), which can be embedded in layouts, views, widgets, etc:

Another interesting thing I’d like to talk about is how to limit the frequency of users’ submissions in Justice. By default, Yii2 provides \yii\filters\RateLimiter for us. RateLimiter implements a rate limiting algorithm based on the leaky bucket algorithm, we can use RateLimiter by attaching it as a behavior to a controller or module. However, RateLimiter requires a model instance to implement the yii\filters\RateLimitInterface, or it will do nothing. This implement looks like a little cumbersome, so we found another alternate limiter: bandwidth-throttle, which is more decoupled from service and flexible.

There is still some work left to do,  pull-requests are welcome:

1. We didn’t provide register feature for new users, this can be fixed by either adding register logic or updating getData() in UserFixture to seed the database.
2. Submissions now are available for everyone to review, maybe this feature should be open to administrators only due to security(privacy?) concerns.

1 response to "Online Judge from Scratch(1) – Frontend"

Leave a Reply

Your email address will not be published. Required fields are marked *

*