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:
parent
6bc2e4e6b1
commit
f6af99bbee
30
api/main.go
30
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue