This commit is contained in:
genofire 2019-12-28 17:44:54 +01:00 committed by Timm
commit 89ae122fc3
9 changed files with 113 additions and 114 deletions

View file

@ -44,4 +44,4 @@ func mapEventObject(event *ics.Event) Event {
eventData.WholeDayEvent = event.GetWholeDayEvent()
return eventData
}
}

View file

@ -4,4 +4,4 @@ type ConfigFile struct {
SharedSecret string `yaml:"shared_secret,omitempty"`
MongoDbServer string `yaml:"mongodb_server,omitempty"`
MongoDbDatabase string `yaml:"mongodb_database,omitempty"`
}
}

View file

@ -1,52 +1,52 @@
package main
import (
"encoding/json"
"github.com/gofrs/uuid"
"github.com/gorilla/mux"
"github.com/robfig/cron"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"encoding/json"
"gopkg.in/yaml.v2"
"log"
"io/ioutil"
"os"
"time"
"github.com/gorilla/mux"
"os"
"time"
)
var config = ConfigFile{}
func main() {
data, _ := ioutil.ReadFile("config.yaml")
err := yaml.Unmarshal(data, &config)
if err != nil {
panic("Can't load config")
}
config.SharedSecret = os.Getenv("SHARED_SECRET")
data, _ := ioutil.ReadFile("config.yaml")
err := yaml.Unmarshal(data, &config)
if err != nil {
panic("Can't load config")
}
config.SharedSecret = os.Getenv("SHARED_SECRET")
c := cron.New()
err = c.AddFunc("@hourly", func() {
loadSpaceData()
getCalendars()
})
if err != nil {
log.Printf("Can't start cron %v", err)
} else {
c.Start()
}
c := cron.New()
err = c.AddFunc("@hourly", func() {
loadSpaceData()
getCalendars()
})
if err != nil {
log.Printf("Can't start cron %v", err)
} else {
c.Start()
}
router := NewRouter()
http.Handle("/", router)
log.Fatal(http.ListenAndServe(":8080", router))
router := NewRouter()
http.Handle("/", router)
log.Fatal(http.ListenAndServe(":8080", router))
}
func getJson(url string, target interface{}) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
return json.NewDecoder(r.Body).Decode(target)
}
func SpaceDataIndex(w http.ResponseWriter, r *http.Request) {
@ -108,7 +108,7 @@ func loadSpaceData() {
timestamp := time.Now().Unix()
for _, spaceUrl := range spaceUrls {
if spaceUrl.Validated && int64(spaceUrl.LastUpdated + 60) < timestamp {
if spaceUrl.Validated && int64(spaceUrl.LastUpdated+60) < timestamp {
spaceData := SpaceData{}
err := getJson(spaceUrl.Url, &spaceData)
if err != nil {
@ -125,8 +125,8 @@ func loadSpaceData() {
}
func refreshData(w http.ResponseWriter, r *http.Request) {
loadSpaceData()
getCalendars()
loadSpaceData()
getCalendars()
w.WriteHeader(204)
w.WriteHeader(204)
}

View file

@ -3,20 +3,20 @@ package main
import "time"
type Calendar struct {
Space string
Events []Event
Space string
Events []Event
}
type Event struct {
Start time.Time `json:"start"`
ImportedId string `json:"importId"`
Status string `json:"status"`
Description string `json:"description"`
Location string `json:"location"`
Summary string `json:"summary"`
Rrule string `json:"rrule"`
Class string `json:"class"`
Url string `json:"url"`
Sequence int `json:"sequence"`
WholeDayEvent bool `json:"wholeDayEvent"`
}
ImportedId string `json:"importId"`
Status string `json:"status"`
Description string `json:"description"`
Location string `json:"location"`
Summary string `json:"summary"`
Rrule string `json:"rrule"`
Class string `json:"class"`
Url string `json:"url"`
Sequence int `json:"sequence"`
WholeDayEvent bool `json:"wholeDayEvent"`
}

View file

@ -26,4 +26,4 @@ func logError(err error) {
if err != nil {
log.Fatal(err)
}
}
}

View file

@ -1,9 +1,9 @@
package main
import (
"net/http"
"encoding/json"
"fmt"
"net/http"
)
type DataObject interface {
@ -45,4 +45,4 @@ func createEntry(i interface{}, w http.ResponseWriter, r *http.Request) {
panic(err)
}
}
}
}

View file

@ -1,9 +1,9 @@
package main
import (
"net/http"
"github.com/gorilla/mux"
"log"
"net/http"
)
func NewRouter() *mux.Router {
@ -13,21 +13,21 @@ func NewRouter() *mux.Router {
handler = Logger(route.Handler, route.Name)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
router.
Methods("OPTIONS").
Name("Options Handler").
Handler(http.HandlerFunc(optionsHandler))
Methods("OPTIONS").
Name("Options Handler").
Handler(http.HandlerFunc(optionsHandler))
}
router.
Methods("OPTIONS").
Name("Options Handler").
Handler(http.HandlerFunc(optionsHandler))
Methods("OPTIONS").
Name("Options Handler").
Handler(http.HandlerFunc(optionsHandler))
router.NotFoundHandler = http.HandlerFunc(notFound)
@ -48,4 +48,4 @@ func optionsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE")
w.WriteHeader(200)
}
}

View file

@ -9,7 +9,6 @@ type Route struct {
Handler http.HandlerFunc
}
type Routes []Route
var IndexRoutes = Routes{
@ -61,4 +60,4 @@ var IndexRoutes = Routes{
"/refresh",
refreshData,
},
}
}

View file

@ -3,71 +3,71 @@ package main
// non standard should start with ext_
type Location struct {
Address string `json:"address"`
// non standard
ExtFloor int `json:"ext_floor"`
Lon float32 `json:"lon"`
Lat float32 `json:"lat"`
Address string `json:"address"`
// non standard
ExtFloor int `json:"ext_floor"`
Lon float32 `json:"lon"`
Lat float32 `json:"lat"`
}
type SpaceFed struct {
SpaceNet bool `json:"spacenet"`
Spacesaml bool `json:"spacesaml"`
SpacePhone bool `json:"spacephone"`
SpaceNet bool `json:"spacenet"`
Spacesaml bool `json:"spacesaml"`
SpacePhone bool `json:"spacephone"`
}
type Stream struct {
M4 bool `json:"m4,omitempty"`
MJPEG bool `json:"mjpeg,omitempty"`
UStream bool `json:"ustream,omitempty"`
UStream bool `json:"ustream,omitempty"`
}
type State struct {
Open bool `json:"open"`
Lastchange float64 `json:"lastchange,omitempty"`
TriggerPerson string `json:"trigger_person,omitempty"`
Message string `json:"message,omitempty"`
Icon struct {
Open string `json:"open"`
Closed string `json:"closed"`
} `json:"icon,omitempty"`
Open bool `json:"open"`
Lastchange float64 `json:"lastchange,omitempty"`
TriggerPerson string `json:"trigger_person,omitempty"`
Message string `json:"message,omitempty"`
Icon struct {
Open string `json:"open"`
Closed string `json:"closed"`
} `json:"icon,omitempty"`
}
type Contact struct {
Twitter string `json:"twitter"`
Phone string `json:"phone"`
Irc string `json:"irc"`
Email string `json:"email"`
Ml string `json:"ml"`
IssueMail string `json:"issue_mail"`
Twitter string `json:"twitter"`
Phone string `json:"phone"`
Irc string `json:"irc"`
Email string `json:"email"`
Ml string `json:"ml"`
IssueMail string `json:"issue_mail"`
}
type Feeds struct {
Blog Feed `json:"blog"`
Wiki Feed `json:"wiki"`
Calendar Feed `json:"calendar"`
Blog Feed `json:"blog"`
Wiki Feed `json:"wiki"`
Calendar Feed `json:"calendar"`
}
// main struct
type SpaceData struct {
Api string `json:"api"`
Space string `json:"space"`
Logo string `json:"logo"`
Url string `json:"url"`
Location Location `json:"location"`
SpaceFed SpaceFed `json:"spacefed,omitempty"`
Cam []string `json:"cam,omitempty"`
Stream *Stream `json:"stream,omitempty"`
State State `json:"state"`
// missing: `json:"events,omitempty"`
Contact *Contact `json:"contact,omitempty"`
IssueReportChannels []string `json:"issue_report_channels"`
// missing: `json:"sensors,omitempty"`
Feeds Feeds `json:"feeds,omitempty"`
// missing: `json:"cache,omitempty"`
Projects []string `json:"projects,omitempty"`
// missing: `json:"radio_show,omitempty"`
// not in spaceapi.io/docs
Ext_ccc string `json:"ext_ccc"`
Api string `json:"api"`
Space string `json:"space"`
Logo string `json:"logo"`
Url string `json:"url"`
Location Location `json:"location"`
SpaceFed SpaceFed `json:"spacefed,omitempty"`
Cam []string `json:"cam,omitempty"`
Stream *Stream `json:"stream,omitempty"`
State State `json:"state"`
// missing: `json:"events,omitempty"`
Contact *Contact `json:"contact,omitempty"`
IssueReportChannels []string `json:"issue_report_channels"`
// missing: `json:"sensors,omitempty"`
Feeds Feeds `json:"feeds,omitempty"`
// missing: `json:"cache,omitempty"`
Projects []string `json:"projects,omitempty"`
// missing: `json:"radio_show,omitempty"`
// not in spaceapi.io/docs
Ext_ccc string `json:"ext_ccc"`
}