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 versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
var javaRegex = regexp.MustCompile("([0-9]+)")
var languages []Language
func main() {
port := "2000"
updateVersions()
var err error
languages, err = updateVersions()
if err != nil {
log.Println(err)
return
}
fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute)
@ -132,24 +137,22 @@ func launch(request inbound, res http.ResponseWriter) {
res.Write(response)
}
func updateVersions() {
func updateVersions() ([]Language, error) {
f, err := os.Create("versions.json")
if err != nil {
log.Println(err)
return
return nil, err
}
defer f.Close()
langs, err := getVersions()
if err != nil {
log.Println(err)
return
return nil, err
}
res, err := json.Marshal(langs)
if err != nil {
log.Println(err)
return
return nil, err
}
f.Write(res)
return langs, nil
}
// get all the language and their current version
@ -194,3 +197,12 @@ func getVersion(s string) (string, string) {
}
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)
}