From f6af99bbeec6fb94fe6a5f508f6289ae49755142 Mon Sep 17 00:00:00 2001 From: pizalord22 Date: Fri, 27 Mar 2020 22:21:29 +0100 Subject: [PATCH] keep the languages in memory rather then writing it to an file, added versions end point that returns all versions in json format --- api/main.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/api/main.go b/api/main.go index d0fc6c9..72ab427 100644 --- a/api/main.go +++ b/api/main.go @@ -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) +}