Write docs!

This commit is contained in:
nemunaire 2018-03-09 19:07:08 +01:00
parent c460bb7bf5
commit bcc598ebd5
37 changed files with 478 additions and 188 deletions

View file

@ -1,3 +1,5 @@
// Package settings is shared across multiple services for easy parsing and
// retrieval of the challenge settings.
package settings
import (
@ -10,33 +12,50 @@ import (
"gopkg.in/fsnotify.v1"
)
// SettingsFile is the expected name of the file containing the settings.
const SettingsFile = "settings.json"
// SettingsDir is the relative location where the SettingsFile lies.
var SettingsDir string = "./SETTINGS"
// FICSettings represents the settings panel.
type FICSettings struct {
// Title is the displayed name of the challenge.
Title string `json:"title"`
// Authors is the group name of people making the challenge.
Authors string `json:"authors"`
// Start is the departure time (expected or effective).
Start time.Time `json:"start"`
// End is the expected end time.
End time.Time `json:"end"`
// Generation is a value used to regenerate static files.
Generation time.Time `json:"generation"`
// FirstBlood is the coefficient applied to each first team who solve a challenge.
FirstBlood float64 `json:"firstBlood"`
// SubmissionCostBase is a complex number representing the cost of each attempts.
SubmissionCostBase float64 `json:"submissionCostBase"`
// AllowRegistration permits unregistered Team to register themselves.
AllowRegistration bool `json:"allowRegistration"`
// DenyNameChange disallow Team to change their name.
DenyNameChange bool `json:"denyNameChange"`
// EnableResolutionRoute activates the route displaying resolution movies.
EnableResolutionRoute bool `json:"enableResolutionRoute"`
// PartialValidation validates each correct given answers, don't expect Team to give all correct answer in a try.
PartialValidation bool `json:"partialValidation"`
// EnableExerciceDepend don't show (or permit to solve) to team challenges they are not unlocked through dependancies.
EnableExerciceDepend bool `json:"enableExerciceDepend"`
}
// ExistsSettings checks if the settings file can by found at the given path.
func ExistsSettings(settingsPath string) bool {
_, err := os.Stat(settingsPath)
return !os.IsNotExist(err)
}
// ReadSettings parses the file at the given location.
func ReadSettings(path string) (FICSettings, error) {
var s FICSettings
if fd, err := os.Open(path); err != nil {
@ -53,6 +72,7 @@ func ReadSettings(path string) (FICSettings, error) {
}
}
// SaveSettings saves settings at the given location.
func SaveSettings(path string, s FICSettings) error {
if fd, err := os.Create(path); err != nil {
return err
@ -68,6 +88,7 @@ func SaveSettings(path string, s FICSettings) error {
}
}
// ForceRegeneration makes a small change to the settings structure in order to force the regeneration of all static files.
func ForceRegeneration() error {
location := path.Join(SettingsDir, SettingsFile)
if settings, err := ReadSettings(location); err != nil {
@ -78,6 +99,10 @@ func ForceRegeneration() error {
}
}
// LoadAndWatchSettings is the function you are looking for!
// Giving the location and a callback, this function will first call your reload function
// before returning (if the file can be parsed); then it starts watching modifications made to
// this file. Your callback is then run each time the file is modified.
func LoadAndWatchSettings(settingsPath string, reload func (FICSettings)) {
// First load of configuration if it exists
if _, err := os.Stat(settingsPath); !os.IsNotExist(err) {