Add id's to space urls

This commit is contained in:
gidsi 2019-04-03 22:29:12 +02:00
commit fcb31bdd10
No known key found for this signature in database
GPG key ID: B47291090A6E5604
3 changed files with 73 additions and 38 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"github.com/gofrs/uuid"
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"log" "log"
@ -95,10 +96,22 @@ func readSpaceurl() []SpaceUrl {
result := []SpaceUrl{} result := []SpaceUrl{}
c.Find(bson.M{}).Iter().All(&result) c.Find(bson.M{}).Iter().All(&result)
for _, spaceUrl := range result {
if spaceUrl.Id == "" {
generatedUuid, err := uuid.NewV4()
if err != nil {
log.Printf("%v", err)
}
spaceUrl.Id = generatedUuid.String()
updateSpaceurl(spaceUrl)
}
}
return result return result
} }
func deleteSpaceurl(url string) { func deleteSpaceurl(id string) error {
session, err := mgo.Dial(config.MongoDbServer) session, err := mgo.Dial(config.MongoDbServer)
if err != nil { if err != nil {
panic(err) panic(err)
@ -108,7 +121,13 @@ func deleteSpaceurl(url string) {
session.SetMode(mgo.Monotonic, true) session.SetMode(mgo.Monotonic, true)
c := session.DB(config.MongoDbDatabase).C("spaceurl") c := session.DB(config.MongoDbDatabase).C("spaceurl")
c.Remove(bson.M{"url": url}) err = c.Remove(bson.M{"id": id})
if err != nil {
log.Println(err)
return err
}
return nil
} }
func readCalendar() []Calendar { func readCalendar() []Calendar {

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"github.com/gofrs/uuid"
"github.com/robfig/cron" "github.com/robfig/cron"
"net/http" "net/http"
"encoding/json" "encoding/json"
@ -49,63 +50,78 @@ func getJson(url string, target interface{}) error {
} }
func SpaceDataIndex(w http.ResponseWriter, r *http.Request) { func SpaceDataIndex(w http.ResponseWriter, r *http.Request) {
ReturnJson(w, readSpacedata()) ReturnJson(w, readSpacedata())
} }
func SpaceUrlIndex(w http.ResponseWriter, r *http.Request) { func SpaceUrlIndex(w http.ResponseWriter, r *http.Request) {
ReturnJson(w, readSpaceurl()) ReturnJson(w, readSpaceurl())
} }
func CalendarIndex(w http.ResponseWriter, r *http.Request) { func CalendarIndex(w http.ResponseWriter, r *http.Request) {
ReturnJson(w, readCalendar()) ReturnJson(w, readCalendar())
} }
func SpaceUrlAdd(w http.ResponseWriter, r *http.Request) { func SpaceUrlAdd(w http.ResponseWriter, r *http.Request) {
spaceUrl := SpaceUrl{} spaceUrl := SpaceUrl{}
createEntry(&spaceUrl, w, r) createEntry(&spaceUrl, w, r)
spaceUrl.Validated = false spaceUrl.Validated = false
writeSpaceurl(spaceUrl)
generatedUuid, err := uuid.NewV4()
if err != nil {
log.Printf("%v", err)
w.WriteHeader(500)
} else {
spaceUrl.Id = generatedUuid.String()
writeSpaceurl(spaceUrl)
}
} }
func SpaceUrlUpdate(w http.ResponseWriter, r *http.Request) { func SpaceUrlUpdate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
SharedSecret := vars["SharedSecret"] SharedSecret := vars["SharedSecret"]
if SharedSecret == config.SharedSecret { if SharedSecret == config.SharedSecret {
spaceUrl := SpaceUrl{} spaceUrl := SpaceUrl{}
createEntry(&spaceUrl, w, r) createEntry(&spaceUrl, w, r)
updateSpaceurl(spaceUrl) updateSpaceurl(spaceUrl)
} }
} }
func SpaceUrlDelete(w http.ResponseWriter, r *http.Request) { func SpaceUrlDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
SharedSecret := vars["SharedSecret"] SharedSecret := vars["SharedSecret"]
Url := vars["url"] Id := vars["id"]
if SharedSecret == config.SharedSecret { if SharedSecret == config.SharedSecret {
deleteSpaceurl(Url) err := deleteSpaceurl(Id)
if err != nil {
w.WriteHeader(500)
} else {
w.WriteHeader(204)
}
} else {
w.WriteHeader(401)
} }
} }
func loadSpaceData() { func loadSpaceData() {
spaceUrls := readSpaceurl() spaceUrls := readSpaceurl()
timestamp := time.Now().Unix() timestamp := time.Now().Unix()
for _, spaceUrl := range spaceUrls { for _, spaceUrl := range spaceUrls {
if spaceUrl.Validated && int64(spaceUrl.LastUpdated + 60) < timestamp { if spaceUrl.Validated && int64(spaceUrl.LastUpdated + 60) < timestamp {
spaceData := SpaceData{} spaceData := SpaceData{}
err := getJson(spaceUrl.Url, &spaceData) err := getJson(spaceUrl.Url, &spaceData)
if err != nil { if err != nil {
log.Println(spaceUrl.Url) log.Println(spaceUrl.Url)
log.Println(err) log.Println(err)
} else { } else {
writeSpaceData(spaceData) writeSpaceData(spaceData)
spaceUrl.LastUpdated = timestamp spaceUrl.LastUpdated = timestamp
updateSpaceurl(spaceUrl) updateSpaceurl(spaceUrl)
} }
} }
} }
} }
func refreshData(w http.ResponseWriter, r *http.Request) { func refreshData(w http.ResponseWriter, r *http.Request) {

View file

@ -52,7 +52,7 @@ var IndexRoutes = Routes{
Route{ Route{
"SpaceUrlDelete", "SpaceUrlDelete",
"DELETE", "DELETE",
"/urls/{url}/{SharedSecret}", "/urls/{id}/{SharedSecret}",
SpaceUrlDelete, SpaceUrlDelete,
}, },
Route{ Route{