Retrieve time through X-FIC-Time header instead of time.json
This commit is contained in:
parent
c33390fa80
commit
6034246015
11 changed files with 107 additions and 131 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
|
|
@ -60,6 +61,8 @@ func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, htt
|
|||
resStatus = http.StatusNotFound
|
||||
}
|
||||
|
||||
w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
|
||||
|
||||
if str, found := ret.(string); found {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(resStatus)
|
||||
|
|
|
|||
|
|
@ -432,19 +432,31 @@ angular.module("FICApp")
|
|||
|
||||
.controller("SettingsController", function($scope, $rootScope, Settings, ROSettings, $location, $http) {
|
||||
$scope.config = Settings.get();
|
||||
$scope.config.$promise.then(function(response) {
|
||||
$rootScope.settings.start = new Date(response.start);
|
||||
$rootScope.settings.end = new Date(response.end);
|
||||
$rootScope.settings.generation = new Date(response.generation);
|
||||
})
|
||||
$scope.configro = ROSettings.get();
|
||||
$scope.duration = 240;
|
||||
|
||||
$scope.saveSettings = function(msg) {
|
||||
if (msg === undefined) { msg = 'New settings saved!'; }
|
||||
var nStart = this.config.start;
|
||||
var nEnd = this.config.end;
|
||||
var nGen = this.config.generation;
|
||||
this.config.$update(function() {
|
||||
$rootScope.newBox('success', msg);
|
||||
$rootScope.settings.start = new Date(nStart);
|
||||
$rootScope.settings.end = new Date(nEnd);
|
||||
$rootScope.settings.generation = new Date(nGen);
|
||||
}, function(response) {
|
||||
$rootScope.newBox('danger', 'An error occurs when saving settings:', response.data);
|
||||
});
|
||||
}
|
||||
$scope.regenerate = function() {
|
||||
this.config.generation = (new Date()).toISOString();
|
||||
$rootScope.settings.generation = new Date(this.config.generation);
|
||||
$scope.saveSettings("Regeneration in progress...");
|
||||
}
|
||||
$scope.launchChallenge = function() {
|
||||
|
|
@ -850,7 +862,7 @@ angular.module("FICApp")
|
|||
"id_assignee": $scope.whoami,
|
||||
"content": $scope.ndescription
|
||||
}
|
||||
}).then(function() {
|
||||
}).then(function(response) {
|
||||
$location.url("/claims/" + $scope.claim.id + "/");
|
||||
});
|
||||
}
|
||||
|
|
@ -1288,21 +1300,26 @@ angular.module("FICApp")
|
|||
presenceCal($scope, "#presenceCal", res);
|
||||
});
|
||||
})
|
||||
.controller("CountdownController", function($scope, $rootScope, $http, $timeout) {
|
||||
.controller("CountdownController", function(Settings, $scope, $rootScope, $http, $interval) {
|
||||
$scope.time = {};
|
||||
|
||||
function updTime() {
|
||||
$timeout.cancel($scope.cbm);
|
||||
$scope.cbm = $timeout(updTime, 1000);
|
||||
if (sessionStorage.userService) {
|
||||
if (sessionStorage.userService && $rootScope.settings) {
|
||||
var time = angular.fromJson(sessionStorage.userService);
|
||||
var srv_cur = (Date.now() + (time.cu * 1000 - time.he)) / 1000;
|
||||
var remain = time.du;
|
||||
if (time.st > 0 && time.st <= srv_cur) {
|
||||
var settings = $rootScope.settings;
|
||||
var srv_cur = new Date(Date.now() + (time.cu - time.he));
|
||||
|
||||
var remain = 0;
|
||||
if (settings.start > 0 && settings.start > srv_cur) {
|
||||
$scope.startIn = Math.floor((settings.start - srv_cur) / 1000);
|
||||
remain = settings.end - settings.start;
|
||||
} else if (settings.end > srv_cur) {
|
||||
$scope.startIn = 0;
|
||||
remain = time.st + time.du - srv_cur;
|
||||
} else if (time.st > 0) {
|
||||
$scope.startIn = Math.floor(time.st - srv_cur);
|
||||
remain = settings.end - srv_cur;
|
||||
}
|
||||
|
||||
remain = remain / 1000;
|
||||
|
||||
if (remain < 0) {
|
||||
remain = 0;
|
||||
$scope.time.end = true;
|
||||
|
|
@ -1314,22 +1331,35 @@ angular.module("FICApp")
|
|||
$scope.time.end = false;
|
||||
$scope.time.expired = false;
|
||||
}
|
||||
$scope.time.start = time.st * 1000;
|
||||
$scope.time.duration = time.du * 1000;
|
||||
|
||||
$scope.time.remaining = remain;
|
||||
$scope.time.hours = Math.floor(remain / 3600);
|
||||
$scope.time.minutes = Math.floor((remain % 3600) / 60);
|
||||
$scope.time.seconds = Math.floor(remain % 60);
|
||||
$rootScope.time = $scope.time;
|
||||
}
|
||||
};
|
||||
$interval(updTime, 1000);
|
||||
|
||||
$rootScope.updTime = function(response) {
|
||||
sessionStorage.userService = angular.toJson({
|
||||
"cu": Math.floor(response.headers("x-fic-time") * 1000),
|
||||
"he": (new Date()).getTime(),
|
||||
});
|
||||
updTime();
|
||||
}
|
||||
|
||||
$http.get("/time.json").then(function(response) {
|
||||
var time = response.data;
|
||||
time.he = (new Date()).getTime();
|
||||
sessionStorage.userService = angular.toJson(time);
|
||||
updTime();
|
||||
});
|
||||
function refresh() {
|
||||
$http.get("/api/settings.json").then(function(response) {
|
||||
response.data.start = new Date(response.data.start);
|
||||
response.data.end = new Date(response.data.end);
|
||||
response.data.generation = new Date(response.data.generation);
|
||||
$rootScope.settings = response.data;
|
||||
$rootScope.updTime(response);
|
||||
})
|
||||
}
|
||||
refresh();
|
||||
$interval(refresh, 10000);
|
||||
});
|
||||
|
||||
function solvedByLevelPie(location, data) {
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"srs.epita.fr/fic-server/admin/api"
|
||||
"srs.epita.fr/fic-server/frontend/time"
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
api.Router().GET("/time.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if config, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
|
||||
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
|
||||
} else {
|
||||
time.ChallengeStart = config.Start
|
||||
time.ChallengeEnd = config.End
|
||||
time.TimeHandler{}.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in a new issue