hmn/src/website/website.go

98 lines
2.5 KiB
Go
Raw Normal View History

2021-03-09 08:05:07 +00:00
package website
import (
"context"
2021-03-11 03:29:32 +00:00
"errors"
2021-06-24 13:10:44 +00:00
"log"
2021-03-09 08:05:07 +00:00
"net/http"
2021-06-24 13:10:44 +00:00
_ "net/http/pprof"
2021-03-11 03:29:32 +00:00
"os"
"os/signal"
"time"
2021-03-09 08:05:07 +00:00
2021-03-28 04:22:29 +00:00
"git.handmade.network/hmn/hmn/src/auth"
2021-03-11 03:39:24 +00:00
"git.handmade.network/hmn/hmn/src/config"
"git.handmade.network/hmn/hmn/src/db"
"git.handmade.network/hmn/hmn/src/discord"
2021-03-11 03:39:24 +00:00
"git.handmade.network/hmn/hmn/src/logging"
2021-04-26 06:56:49 +00:00
"git.handmade.network/hmn/hmn/src/perf"
2021-03-14 20:49:58 +00:00
"git.handmade.network/hmn/hmn/src/templates"
2022-03-22 18:07:43 +00:00
"git.handmade.network/hmn/hmn/src/twitch"
2021-03-09 08:05:07 +00:00
"github.com/spf13/cobra"
)
var WebsiteCommand = &cobra.Command{
Short: "Run the HMN website",
Run: func(cmd *cobra.Command, args []string) {
2021-03-14 20:49:58 +00:00
templates.Init()
defer logging.LogPanics(nil)
2021-03-11 03:19:39 +00:00
logging.Info().Msg("Hello, HMN!")
2021-03-09 08:05:07 +00:00
2021-04-26 06:56:49 +00:00
backgroundJobContext, cancelBackgroundJobs := context.WithCancel(context.Background())
2021-08-17 06:08:33 +00:00
longRequestContext, cancelLongRequests := context.WithCancel(context.Background())
2021-04-26 06:56:49 +00:00
conn := db.NewConnPool()
2021-04-26 06:56:49 +00:00
perfCollector := perf.RunPerfCollector(backgroundJobContext)
2021-03-09 08:05:07 +00:00
2021-03-11 03:29:32 +00:00
server := http.Server{
Addr: config.Config.Addr,
Handler: NewWebsiteRoutes(longRequestContext, conn),
2021-03-11 03:29:32 +00:00
}
2021-03-28 04:22:29 +00:00
backgroundJobsDone := zipJobs(
auth.PeriodicallyDeleteExpiredSessions(backgroundJobContext, conn),
2021-08-08 20:05:52 +00:00
auth.PeriodicallyDeleteInactiveUsers(backgroundJobContext, conn),
2021-04-26 06:56:49 +00:00
perfCollector.Done,
discord.RunDiscordBot(backgroundJobContext, conn),
discord.RunHistoryWatcher(backgroundJobContext, conn),
2022-03-22 18:07:43 +00:00
twitch.MonitorTwitchSubscriptions(backgroundJobContext, conn),
2021-03-28 04:22:29 +00:00
)
2021-03-11 03:29:32 +00:00
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go func() {
<-signals
logging.Info().Msg("Shutting down the website")
go func() {
2021-08-17 06:08:33 +00:00
logging.Info().Msg("cancelling long requests")
cancelLongRequests()
timeout, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
logging.Info().Msg("shutting down web server")
server.Shutdown(timeout)
logging.Info().Msg("cancelling background jobs")
cancelBackgroundJobs()
}()
2021-03-11 03:29:32 +00:00
<-signals
logging.Warn().Msg("Forcibly killed the website")
os.Exit(1)
}()
2021-06-24 13:10:44 +00:00
go func() {
log.Println(http.ListenAndServe(config.Config.PrivateAddr, nil))
}()
2021-03-11 03:19:39 +00:00
logging.Info().Str("addr", config.Config.Addr).Msg("Serving the website")
2021-03-11 03:29:32 +00:00
serverErr := server.ListenAndServe()
if !errors.Is(serverErr, http.ErrServerClosed) {
logging.Error().Err(serverErr).Msg("Server shut down unexpectedly")
}
2021-03-28 04:22:29 +00:00
<-backgroundJobsDone
2021-03-09 08:05:07 +00:00
},
}
2021-03-28 04:22:29 +00:00
func zipJobs(cs ...<-chan struct{}) <-chan struct{} {
out := make(chan struct{})
go func() {
for _, c := range cs {
<-c
}
close(out)
}()
return out
}