mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +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 (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
|
@ -19,10 +18,11 @@ import (
|
|||
const tokFile = "token.json"
|
||||
|
||||
type Service struct {
|
||||
calIds map[string]string
|
||||
*calendar.Service
|
||||
}
|
||||
|
||||
func NewService(ctx context.Context, credJson string) *Service {
|
||||
func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, error) {
|
||||
b := []byte(credJson)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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: Delete this. It is just for Proof of Concept
|
||||
func (s Service) ListNextEvents() (*calendar.Events, error) {
|
||||
t := time.Now().Format(time.RFC3339)
|
||||
events, err := s.Events.List("primary").ShowDeleted(false).
|
||||
SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do()
|
||||
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
|
||||
func (s *Service) Create(from, to time.Time) (*calendar.Event, error) {
|
||||
l := s.CalendarList.List()
|
||||
r, e := l.Do()
|
||||
log.Println(e)
|
||||
for _, c := range r.Items {
|
||||
log.Printf("%+v: %s", c.Summary, c.Id)
|
||||
}
|
||||
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.
|
||||
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
|
||||
// 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/internal/auth"
|
||||
"github.com/rjNemo/rentease/internal/booking"
|
||||
"github.com/rjNemo/rentease/internal/calendar"
|
||||
"github.com/rjNemo/rentease/internal/pdf"
|
||||
)
|
||||
|
||||
|
|
@ -28,6 +29,7 @@ type Server struct {
|
|||
bs *booking.Service
|
||||
as *auth.Service
|
||||
ps *pdf.PdfService
|
||||
cs *calendar.Service
|
||||
hc *config.Host
|
||||
addr 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)
|
||||
for _, opt := range opts {
|
||||
err := opt(option)
|
||||
|
|
@ -104,6 +106,7 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, hc *config.H
|
|||
bs: bs,
|
||||
as: as,
|
||||
ps: ps,
|
||||
cs: cs,
|
||||
hc: hc,
|
||||
addr: fmt.Sprintf("0.0.0.0:%d", *option.port),
|
||||
secretKey: *option.secretKey,
|
||||
|
|
|
|||
15
main.go
15
main.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
|
|
@ -17,6 +18,7 @@ import (
|
|||
"github.com/rjNemo/rentease/config"
|
||||
"github.com/rjNemo/rentease/internal/auth"
|
||||
"github.com/rjNemo/rentease/internal/booking"
|
||||
"github.com/rjNemo/rentease/internal/calendar"
|
||||
"github.com/rjNemo/rentease/internal/pdf"
|
||||
"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)
|
||||
}
|
||||
|
||||
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")
|
||||
port, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
|
|
@ -85,6 +99,7 @@ func run(c context.Context, getEnv func(string) string) error {
|
|||
booking.NewService(db),
|
||||
as,
|
||||
ps,
|
||||
cs,
|
||||
config.NewHost(),
|
||||
server.WithPort(port),
|
||||
server.WithFileSystem(static),
|
||||
|
|
|
|||
Loading…
Reference in a new issue