keep the languages in memory rather then writing it to an file, added versions end point that returns all versions in json format

This commit is contained in:
pizalord22 2020-03-27 22:21:29 +01:00
parent 6bc2e4e6b1
commit f6af99bbee
1 changed files with 21 additions and 9 deletions

View File

@ -38,11 +38,16 @@ type Language struct {
var instance int var instance int
var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)") var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
var javaRegex = regexp.MustCompile("([0-9]+)") var javaRegex = regexp.MustCompile("([0-9]+)")
var languages []Language
func main() { func main() {
port := "2000" port := "2000"
var err error
updateVersions() languages, err = updateVersions()
if err != nil {
log.Println(err)
return
}
fmt.Println("starting api on port", port) fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute) http.HandleFunc("/execute", Execute)
@ -132,24 +137,22 @@ func launch(request inbound, res http.ResponseWriter) {
res.Write(response) res.Write(response)
} }
func updateVersions() { func updateVersions() ([]Language, error) {
f, err := os.Create("versions.json") f, err := os.Create("versions.json")
if err != nil { if err != nil {
log.Println(err) return nil, err
return
} }
defer f.Close() defer f.Close()
langs, err := getVersions() langs, err := getVersions()
if err != nil { if err != nil {
log.Println(err) return nil, err
return
} }
res, err := json.Marshal(langs) res, err := json.Marshal(langs)
if err != nil { if err != nil {
log.Println(err) return nil, err
return
} }
f.Write(res) f.Write(res)
return langs, nil
} }
// get all the language and their current version // get all the language and their current version
@ -194,3 +197,12 @@ func getVersion(s string) (string, string) {
} }
return lines[1], versionRegex.FindString(s) return lines[1], versionRegex.FindString(s)
} }
func versions(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(languages)
if err != nil {
log.Println(err)
return
}
w.Write(data)
}