diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..926abf6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/pkg +/src/fileserver/data +/src/github.com +/src/fileserver/public/node_modules/ \ No newline at end of file diff --git a/src/fileserver/main.go b/src/fileserver/main.go index dd18a13..47e6f46 100644 --- a/src/fileserver/main.go +++ b/src/fileserver/main.go @@ -6,8 +6,12 @@ import ( "log" "net/http" "strings" + "sync" ) +var lock sync.Mutex +var port = ":8080" + func main() { router := httprouter.New() router.GET("/dictionary/:dictionaryName", GetDictionary) @@ -16,7 +20,8 @@ func main() { router.GET("/add/:dictionaryName/:key/:value", AddTranslation) router.NotFound = http.FileServer(http.Dir("public")) - log.Fatal(http.ListenAndServe(":8080", router)) + print("running on port", port) + log.Fatal(http.ListenAndServe(port, router)) } func GetDictionaries(res http.ResponseWriter, req *http.Request, _ httprouter.Params) { @@ -45,6 +50,8 @@ func GetDictionary(res http.ResponseWriter, req *http.Request, ps httprouter.Par } func AddTranslation(res http.ResponseWriter, req *http.Request, ps httprouter.Params) { + lock.Lock() + dictionary := ps.ByName("dictionaryName") data, err := ioutil.ReadFile("data/" + dictionary + ".json") check(err) @@ -52,6 +59,8 @@ func AddTranslation(res http.ResponseWriter, req *http.Request, ps httprouter.Pa addition := ",\n\"" + ps.ByName("key") + "\":\"" + ps.ByName("value") + "\"}" jsonText = strings.Replace(jsonText, "}", addition, 1) ioutil.WriteFile("data/"+dictionary+".json", []byte(jsonText), 0644) + + lock.Unlock() } func NewDictionary(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {