mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
create calendar service at startup
This commit is contained in:
parent
e13d7f53ff
commit
4409facd34
4 changed files with 62 additions and 18 deletions
18
internal/calendar/option.go
Normal file
18
internal/calendar/option.go
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package calendar
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Option func(*Service) error
|
||||||
|
|
||||||
|
func WithCalendar(key string, calendarId string) Option {
|
||||||
|
return func(s *Service) error {
|
||||||
|
_, err := s.CalendarList.Get(calendarId).Do()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot find calendar entry with id %q: %w", calendarId, err)
|
||||||
|
}
|
||||||
|
s.calIds[key] = calendarId
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ package calendar
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -19,10 +18,11 @@ import (
|
||||||
const tokFile = "token.json"
|
const tokFile = "token.json"
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
calIds map[string]string
|
||||||
*calendar.Service
|
*calendar.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(ctx context.Context, credJson string) *Service {
|
func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, error) {
|
||||||
b := []byte(credJson)
|
b := []byte(credJson)
|
||||||
|
|
||||||
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
|
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
|
||||||
|
|
@ -36,27 +36,35 @@ func NewService(ctx context.Context, credJson string) *Service {
|
||||||
log.Fatalf("Unable to retrieve Calendar client: %v", err)
|
log.Fatalf("Unable to retrieve Calendar client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Service{srv}
|
s := &Service{
|
||||||
|
Service: srv,
|
||||||
|
calIds: make(map[string]string),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
err := opt(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Service{Service: srv}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement create event, list events in a period, delete event
|
// TODO: implement create event, list events in a period, delete event
|
||||||
|
func (s *Service) Create(from, to time.Time) (*calendar.Event, error) {
|
||||||
// TODO: Delete this. It is just for Proof of Concept
|
l := s.CalendarList.List()
|
||||||
func (s Service) ListNextEvents() (*calendar.Events, error) {
|
r, e := l.Do()
|
||||||
t := time.Now().Format(time.RFC3339)
|
log.Println(e)
|
||||||
events, err := s.Events.List("primary").ShowDeleted(false).
|
for _, c := range r.Items {
|
||||||
SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do()
|
log.Printf("%+v: %s", c.Summary, c.Id)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to retrieve next ten of the user's events: %w", err)
|
|
||||||
}
|
|
||||||
if len(events.Items) == 0 {
|
|
||||||
return nil, errors.New("no upcoming events found")
|
|
||||||
} else {
|
|
||||||
// TODO: return a custom type
|
|
||||||
return events, nil
|
|
||||||
}
|
}
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) List(from, to time.Time) (*calendar.Events, error) { return nil, nil }
|
||||||
|
func (s *Service) Delete(id int) error { return nil }
|
||||||
|
|
||||||
// Retrieve a token, saves the token, then returns the generated client.
|
// Retrieve a token, saves the token, then returns the generated client.
|
||||||
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
|
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
|
||||||
// The file token.json stores the user's access and refresh tokens, and is
|
// The file token.json stores the user's access and refresh tokens, and is
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"github.com/rjNemo/rentease/config"
|
"github.com/rjNemo/rentease/config"
|
||||||
"github.com/rjNemo/rentease/internal/auth"
|
"github.com/rjNemo/rentease/internal/auth"
|
||||||
"github.com/rjNemo/rentease/internal/booking"
|
"github.com/rjNemo/rentease/internal/booking"
|
||||||
|
"github.com/rjNemo/rentease/internal/calendar"
|
||||||
"github.com/rjNemo/rentease/internal/pdf"
|
"github.com/rjNemo/rentease/internal/pdf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,6 +29,7 @@ type Server struct {
|
||||||
bs *booking.Service
|
bs *booking.Service
|
||||||
as *auth.Service
|
as *auth.Service
|
||||||
ps *pdf.PdfService
|
ps *pdf.PdfService
|
||||||
|
cs *calendar.Service
|
||||||
hc *config.Host
|
hc *config.Host
|
||||||
addr string
|
addr string
|
||||||
secretKey string
|
secretKey string
|
||||||
|
|
@ -90,7 +92,7 @@ func WithApiKey(apiKey string) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, hc *config.Host, opts ...Option) (*Server, error) {
|
func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) {
|
||||||
option := new(options)
|
option := new(options)
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
err := opt(option)
|
err := opt(option)
|
||||||
|
|
@ -104,6 +106,7 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, hc *config.H
|
||||||
bs: bs,
|
bs: bs,
|
||||||
as: as,
|
as: as,
|
||||||
ps: ps,
|
ps: ps,
|
||||||
|
cs: cs,
|
||||||
hc: hc,
|
hc: hc,
|
||||||
addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
|
addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
|
||||||
secretKey: *option.secretKey,
|
secretKey: *option.secretKey,
|
||||||
|
|
|
||||||
15
main.go
15
main.go
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -17,6 +18,7 @@ import (
|
||||||
"github.com/rjNemo/rentease/config"
|
"github.com/rjNemo/rentease/config"
|
||||||
"github.com/rjNemo/rentease/internal/auth"
|
"github.com/rjNemo/rentease/internal/auth"
|
||||||
"github.com/rjNemo/rentease/internal/booking"
|
"github.com/rjNemo/rentease/internal/booking"
|
||||||
|
"github.com/rjNemo/rentease/internal/calendar"
|
||||||
"github.com/rjNemo/rentease/internal/pdf"
|
"github.com/rjNemo/rentease/internal/pdf"
|
||||||
"github.com/rjNemo/rentease/internal/server"
|
"github.com/rjNemo/rentease/internal/server"
|
||||||
)
|
)
|
||||||
|
|
@ -72,6 +74,18 @@ func run(c context.Context, getEnv func(string) string) error {
|
||||||
return fmt.Errorf("error starting auth service %s", err)
|
return fmt.Errorf("error starting auth service %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
creds := os.Getenv("CALENDAR_CREDENTIALS")
|
||||||
|
t2Id := os.Getenv("CALENDAR_ID_T2")
|
||||||
|
t3Id := os.Getenv("CALENDAR_ID_T3")
|
||||||
|
|
||||||
|
cs, err := calendar.NewService(ctx, creds,
|
||||||
|
calendar.WithCalendar("T2", t2Id),
|
||||||
|
calendar.WithCalendar("T3", t3Id),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("cannot build calendar service: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
p := getEnv("PORT")
|
p := getEnv("PORT")
|
||||||
port, err := strconv.Atoi(p)
|
port, err := strconv.Atoi(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -85,6 +99,7 @@ func run(c context.Context, getEnv func(string) string) error {
|
||||||
booking.NewService(db),
|
booking.NewService(db),
|
||||||
as,
|
as,
|
||||||
ps,
|
ps,
|
||||||
|
cs,
|
||||||
config.NewHost(),
|
config.NewHost(),
|
||||||
server.WithPort(port),
|
server.WithPort(port),
|
||||||
server.WithFileSystem(static),
|
server.WithFileSystem(static),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue