diff --git a/.air.toml b/.air.toml index c07fdbd..842a9eb 100644 --- a/.air.toml +++ b/.air.toml @@ -15,16 +15,17 @@ follow_symlink = false # full_bin = "" include_dir = ["handlers", "models", "services", "utils", "pkg", "template"] include_ext = ["go", "templ"] -include_file = ["main.go", "dist/assets/style-*.css"] -kill_delay = "0.3s" +include_file = ["main.go", "dist/assets/style-*.css", "dist/main.css"] +kill_delay = 5000000000 # nanosecond log = "build-errors.log" poll = false poll_interval = 0 -post_cmd = [] +# Workaround for shutting down the server (https://github.com/cosmtrek/air/issues/534) +post_cmd = ["lsof -i tcp:8080 | awk 'NR==2{print $2}' | xargs kill"] pre_cmd = [] rerun = false rerun_delay = 500 -send_interrupt = false +send_interrupt = true stop_on_error = true [color] diff --git a/.gitignore b/.gitignore index 22560ae..d0538bf 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ vendor/**/* *_templ.txt template**/*.html .data/ -.tasks/ \ No newline at end of file +.task/ \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml index da4b49a..bc6f616 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -47,7 +47,7 @@ tasks: - ./node_modules/ watch: - deps: [watch:ts, watch:css, watch:templ, watch:server] + deps: [watch:ts, watch:css, watch:templ, watch:server:wait] watch:templ: deps: [bin:templ] cmds: @@ -58,15 +58,20 @@ tasks: watch:ts: cmds: - npx vite build -w + watch:server:wait: + cmds: + - sleep 1 + - task: watch:server watch:server: deps: [bin:air] + # sources: + # - main.go + # - services/**/*.go + # generates: + # - ./tmp/main cmds: - - sleep 1 - .task/bin/air -c .air.toml - buildserver: - - go build -v -i main.go - db:start: cmds: - docker run --name mcmamina -e POSTGRES_PASSWORD=$POSTGRES_PASSWORD -e POSTGRES_DB=$POSTGRES_DB -e POSTGRES_USER=$POSTGRES_USER -v ./.data:/var/lib/postgresql/data -p 5432:5432 -d postgres:alpine diff --git a/go.mod b/go.mod index 9472312..c39edd5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module jirku.sk/mcmamina go 1.22.0 require ( - github.com/a-h/templ v0.2.648 + github.com/a-h/templ v0.2.663 github.com/alecthomas/chroma v0.10.0 github.com/gorilla/mux v1.8.1 github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index 5188d4c..bdf8731 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/a-h/templ v0.2.543 h1:8YyLvyUtf0/IE2nIwZ62Z/m2o2NqwhnMynzOL78Lzbk= github.com/a-h/templ v0.2.543/go.mod h1:jP908DQCwI08IrnTalhzSEH9WJqG/Q94+EODQcJGFUA= github.com/a-h/templ v0.2.648 h1:A1ggHGIE7AONOHrFaDTM8SrqgqHL6fWgWCijQ21Zy9I= github.com/a-h/templ v0.2.648/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= +github.com/a-h/templ v0.2.663 h1:aa0WMm27InkYHGjimcM7us6hJ6BLhg98ZbfaiDPyjHE= +github.com/a-h/templ v0.2.663/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/main.go b/main.go index 6cb0197..9bebdd8 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "embed" "flag" "fmt" @@ -10,8 +11,11 @@ import ( "net/http/httputil" "net/url" "os" + "os/signal" "path/filepath" "strings" + "syscall" + "time" "github.com/gorilla/mux" "github.com/joho/godotenv" @@ -124,9 +128,34 @@ func setupWebserver(log *slog.Logger, calendarService *services.CalendarService) // MCMAMINA <<-- GENERATED CODE handleFiles(router, http.FS(workingFolder)) + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT) + addr := fmt.Sprintf("%s:%d", config.host, config.port) + srv := &http.Server{ + Addr: addr, + Handler: router, + } log.Info(fmt.Sprintf("starting server at %s", addr)) - http.ListenAndServe(addr, router) + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Error(fmt.Errorf("failed to start server %s", err).Error()) + os.Exit(1) + } + }() + // Block until we receive our signal. + signal := <-sigs + log.Info(fmt.Sprintf("received \"%s\" signal, shutting down", signal)) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) + defer cancel() + + // Doesn't block if no connections, but will otherwise wait + // until the timeout deadline. + log.Info("shutting down...") + srv.Shutdown(ctx) + log.Info("server shutted down") + os.Exit(0) } func handleFiles(r *mux.Router, folder http.FileSystem) { diff --git a/services/calendar.go b/services/calendar.go index da2a86f..8270f75 100644 --- a/services/calendar.go +++ b/services/calendar.go @@ -3,6 +3,7 @@ package services import ( "context" "fmt" + "strings" "sync" "sync/atomic" "time" @@ -45,6 +46,8 @@ func (s *CalendarService) getEvents(ctx context.Context, timeMin, timeMax time.T var result []models.Event + eventTitleToIgnore := strings.ToLower("Prestávka - sme zatvorení") + var eventsToGet []string for _, event := range events.Items { if event == nil { @@ -52,6 +55,8 @@ func (s *CalendarService) getEvents(ctx context.Context, timeMin, timeMax time.T } if event.Recurrence != nil && len(event.Recurrence) > 0 { eventsToGet = append(eventsToGet, event.Id) + } else if strings.EqualFold(strings.ToLower(event.Summary), eventTitleToIgnore) { + continue } else { result = append(result, newEventFromGoogle(event)) } @@ -79,6 +84,9 @@ func (s *CalendarService) getEvents(ctx context.Context, timeMin, timeMax time.T close(gatheredEvents) }() for events := range gatheredEvents { + if len(events) > 0 && strings.EqualFold(strings.ToLower(events[0].Title), eventTitleToIgnore) { + continue + } result = append(result, events...) } return result, nil @@ -111,6 +119,7 @@ func (s *CalendarService) GetEvents(ctx context.Context, timeMin, timeMax time.T if ok { return events, nil } + } events, err := s.getEvents(ctx, timeMin, timeMax)