view calsync/main.go @ 57:24f662799710

WIP incremental sync now runs
author drewp@bigasterisk.com
date Thu, 05 Sep 2024 15:03:05 -0700
parents 635ff76f867c
children 1f8e66cb0108
line wrap: on
line source

package main

/*
go build && ./gcalendarwatch


let python continue to serve these:
		Route('/', getRoot),
		Route('/graph/calendar/upcoming', StaticGraph(agendaGraph)),
		use https://github.com/cayleygraph/quad
		Route('/graph/calendar/upcoming/events', GraphEvents(agendaGraph)),
		use https://github.com/tmaxmax/go-sse
		Route('/graph/calendar/countdown', StaticGraph(countdownGraph)),
		Route('/graph/calendar/countdown/events', GraphEvents(countdownGraph)),
		Route('/graph/currentEvents', StaticGraph(currentEventsGraph)),
		Route('/graph/currentEvents/events', GraphEvents(currentEventsGraph)),
*/

import (
	"context"
	"log"
	"net/http"
	"time"

	"bigasterisk.com/go/gcalendarwatch/gcalclient"
	"bigasterisk.com/go/gcalendarwatch/mongoclient"
	"github.com/gorilla/mux"
)

func main() {
	ctx := context.Background()

	log.SetFlags(log.LstdFlags | log.Lshortfile)
	gc, err := gcalclient.New(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer gc.Close()

	mc, err := mongoclient.New(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer mc.Close()

	// todo: if a cal is deleted, nothing touches its db events ever again.
	err = updateMongoCalsToMatchGoogleOnce(mc, gc)
	if err != nil {
		log.Fatal(err)
	}

	err = updateMongoEventsToMatchGoogleForever(mc, gc,
		time.Duration(7*24)*time.Hour, 
		time.Duration(14*24)*time.Hour)
	if err != nil {
		log.Fatal(err)
	}

	r := mux.NewRouter()
	http.Handle("/", r)

	home := func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("calsync service for calendar updates"))
	}
	r.HandleFunc("/", home)
	r.HandleFunc("/gcalendarwatch", home)

	notificationHandler := func(w http.ResponseWriter, r *http.Request) {
	}
	r.HandleFunc("/gcalendarwatch/notification", notificationHandler).Methods("POST")
	log.Println(("serving /gcalendarwatch/notification on :8080"))
	log.Fatal(http.ListenAndServe(":8080", nil))
}