feat: only fetch big list of all events every ten minutes instead of on every request
This commit is contained in:
parent
4d873e6574
commit
d5d2de3b26
42
main.go
42
main.go
|
@ -10,13 +10,18 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
API_URL = "https://api.events.ccc.de/congress/2024"
|
API_URL = "https://api.events.ccc.de/congress/2024"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ROOMS []Room
|
var (
|
||||||
|
SCHEDULE_MUTEX sync.Mutex
|
||||||
|
SCHEDULE []Event
|
||||||
|
ROOMS []Room
|
||||||
|
)
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
@ -91,6 +96,24 @@ func scrape_hub() ([]Event, error) {
|
||||||
return events, nil
|
return events, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cron_fetch() {
|
||||||
|
for {
|
||||||
|
fmt.Println("starting cron...")
|
||||||
|
schedule, err := scrape_hub()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Cron failed; retrying in a minute")
|
||||||
|
time.Sleep(1 * time.Minute)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
SCHEDULE_MUTEX.Lock()
|
||||||
|
SCHEDULE = schedule
|
||||||
|
SCHEDULE_MUTEX.Unlock()
|
||||||
|
|
||||||
|
time.Sleep(10 * time.Minute)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func get_rooms() ([]Room, error) {
|
func get_rooms() ([]Room, error) {
|
||||||
println("get rooms")
|
println("get rooms")
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/rooms", API_URL))
|
resp, err := http.Get(fmt.Sprintf("%s/rooms", API_URL))
|
||||||
|
@ -215,14 +238,12 @@ func get_schedule(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
schedule, err := scrape_hub()
|
|
||||||
if err != nil {
|
SCHEDULE_MUTEX.Lock()
|
||||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
defer SCHEDULE_MUTEX.Unlock()
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var my_events []Event
|
var my_events []Event
|
||||||
for _, event := range schedule {
|
for _, event := range SCHEDULE {
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
if event.ID == id {
|
if event.ID == id {
|
||||||
my_events = append(my_events, event)
|
my_events = append(my_events, event)
|
||||||
|
@ -263,6 +284,13 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ROOMS = rooms
|
ROOMS = rooms
|
||||||
|
schedule, err := scrape_hub()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
SCHEDULE = schedule
|
||||||
|
|
||||||
|
go cron_fetch()
|
||||||
|
|
||||||
println("Stating Server")
|
println("Stating Server")
|
||||||
// Start a web server to serve the iCal file
|
// Start a web server to serve the iCal file
|
||||||
|
|
Loading…
Reference in a new issue