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"
|
2021-08-06 23:23:51 +00:00
|
|
|
"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"
|
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()
|
|
|
|
|
2021-03-21 20:38:37 +00:00
|
|
|
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-04-06 06:10:15 +00:00
|
|
|
conn := db.NewConnPool(config.Config.Postgres.MinConn, config.Config.Postgres.MaxConn)
|
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,
|
2021-04-26 06:56:49 +00:00
|
|
|
Handler: NewWebsiteRoutes(conn, perfCollector),
|
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,
|
2021-08-06 23:23:51 +00:00
|
|
|
discord.RunDiscordBot(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")
|
2021-03-31 03:55:19 +00:00
|
|
|
go func() {
|
|
|
|
timeout, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2021-08-06 23:23:51 +00:00
|
|
|
logging.Info().Msg("shutting down web server")
|
2021-03-31 03:55:19 +00:00
|
|
|
server.Shutdown(timeout)
|
2021-08-06 23:23:51 +00:00
|
|
|
logging.Info().Msg("cancelling background jobs")
|
2021-03-31 03:55:19 +00:00
|
|
|
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
|
|
|
|
}
|