hivedav

A curlable free/busy scheduler with CalDAV integration
git clone https://git.in0rdr.ch/hivedav.git
Log | Files | Refs | Pull requests |Archive | README | LICENSE

main.go (3975B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"github.com/julienschmidt/httprouter"
      6 	"gopkg.in/robfig/cron.v2"
      7 	"hivedav/caldav"
      8 	"hivedav/config"
      9 	"log"
     10 	"net/http"
     11 	"time"
     12 )
     13 
     14 var hivedavVersion = "v0.4-nightly"
     15 
     16 func main() {
     17 	// If exists, load config file from pwd
     18 	conf := &config.Config{}
     19 	conf, err := conf.LoadConfig(".")
     20 	if err != nil {
     21 		log.Fatal("Error loading config: ", err)
     22 	}
     23 
     24 	var server *Server
     25 	server, err = server.NewServer(conf)
     26 	if err != nil {
     27 		log.Fatal("Error creating server with config: ", err)
     28 	}
     29 
     30 	log.Printf("HiveDAV %s, %s 🍯\n", hivedavVersion, conf.HiveDavHost)
     31 	log.Println("----------------------------------------------------")
     32 	log.Printf("Cron interval:\t\t%d minutes\n", conf.RefreshInterval)
     33 	log.Printf("Calendar horizon:\t\t%d year(s)\n", conf.Horizon)
     34 	log.Printf("CalDAV uri:\t\t\t%s\n", conf.CaldavUri)
     35 	log.Printf("CalDAV discovery host:\t%s\n", conf.CaldavHost)
     36 	log.Printf("CalDAV user:\t\t%s\n", conf.CaldavUser)
     37 
     38 	c := cron.New()
     39 	c.AddFunc(fmt.Sprintf("@every %dm", conf.RefreshInterval), func() { updateDatabase(server, c) })
     40 	go updateDatabase(server, c)
     41 	c.Start()
     42 
     43 	router := httprouter.New()
     44 	router.GET("/", server.Index)
     45 	router.GET("/week/:year/:week", server.Week)
     46 	router.GET("/list", server.ListCubicles)
     47 	router.GET("/list/:year/:week", server.ListCubiclesInWeek)
     48 	router.POST("/book/:dtstart", server.BookCubicle)
     49 	router.GET("/book/:dtstart", server.CubicleForm)
     50 	router.ServeFiles("/css/*filepath", http.Dir("./css"))
     51 	router.GET("/healthz", server.Healthz)
     52 	log.Printf("Listening:\t\t\t%s:%d\n", conf.ListenAddress, conf.ListenPort)
     53 	log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", conf.ListenAddress, conf.ListenPort), router))
     54 }
     55 
     56 func updateDatabase(s *Server, c *cron.Cron) {
     57 	var calData []caldav.CalData
     58 	var calendarPath string
     59 	var err error
     60 	// discover calendar based on HIVEDAV_CALENDAR when no CALDAV_URI is set
     61 	if s.config.CaldavUri == "" {
     62 		log.Println("----------------------------------------------------")
     63 		log.Printf("Discovering calendars on host %s...\n", s.config.CaldavHost)
     64 		// Find current user principal
     65 		userPrincipal, err := caldav.UserPrincipal(s.config.CaldavHost, s.config.CaldavUser, s.config.CaldavPassword)
     66 		if err != nil {
     67 			log.Fatal("Error reading current user principal: ", err)
     68 		}
     69 		log.Printf("User principal:\t\t%s\n", userPrincipal)
     70 
     71 		// Find home set of current user principal
     72 		homeSetPath, err := caldav.CalendarHome(s.config.CaldavHost, s.config.CaldavUser, s.config.CaldavPassword, userPrincipal, s.config.Calendar)
     73 		if err != nil {
     74 			log.Fatal("Error reading home set URI: ", err)
     75 		}
     76 		log.Printf("Calendar homeset:\t\t%s", homeSetPath)
     77 
     78 		// Find calendar URI for the calendar specified in the config
     79 		calendarPath, err = caldav.CalendarFromHomeSet(s.config.CaldavHost, s.config.CaldavUser, s.config.CaldavPassword, homeSetPath, s.config.Calendar)
     80 		if err != nil {
     81 			log.Fatal("Error reading calendar URI: ", err)
     82 		}
     83 		log.Printf("Calendar %d path:\t\t%s\n", s.config.Calendar, calendarPath)
     84 	}
     85 
     86 	log.Println("----------------------------------------------------")
     87 	log.Println("Fetching availability from CalDAV server, please wait..")
     88 
     89 	if s.config.CaldavUri == "" {
     90 		// Get availability data for the calendar specified in the config
     91 		calData, err = caldav.GetAvailability(s.config.CaldavHost, s.config.CaldavUser, s.config.CaldavPassword, calendarPath)
     92 	} else {
     93 		// don't perform discovery, use the CALDAV_URI as calendar url
     94 		calData, err = caldav.GetAvailabilityByUrl(s.config.CaldavUri, s.config.CaldavUser, s.config.CaldavPassword)
     95 	}
     96 
     97 	if err != nil {
     98 		log.Fatal("Error reading calendar URI: ", err)
     99 	}
    100 
    101 	err = s.UpdateAvailability(calData)
    102 	if err != nil {
    103 		log.Fatalf("Error fetching availability: %v", err)
    104 	}
    105 
    106 	log.Println("Database initialized")
    107 	log.Printf("Next db refresh (cron): \t%v\n", c.Entries()[0].Schedule.Next(time.Now()).Format("2006-01-02 15:04"))
    108 	log.Println("----------------------------------------------------")
    109 }