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
|
|
|
|
2023-05-17 19:33:27 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/assets"
|
2021-03-28 04:22:29 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/auth"
|
2024-05-30 04:19:31 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/buildscss"
|
2024-01-28 17:12:59 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/calendar"
|
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"
|
2022-05-14 05:33:00 +00:00
|
|
|
"git.handmade.network/hmn/hmn/src/hmns3"
|
|
|
|
"git.handmade.network/hmn/hmn/src/jobs"
|
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()
|
|
|
|
|
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())
|
|
|
|
|
2022-05-07 18:58:00 +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,
|
2022-06-24 21:38:11 +00:00
|
|
|
Handler: NewWebsiteRoutes(conn),
|
2021-03-11 03:29:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 05:33:00 +00:00
|
|
|
backgroundJobsDone := jobs.Zip(
|
Add Discord login (#106)
This leverages our existing Discord OAuth implementation. Any users with a linked Discord account will be able to log in immediately. When logging in, we request the `email` scope in addition to `identity`, so existing users will be prompted one time to accept the new permissions. On subsequent logins, Discord will skip the prompt.
When linking your Discord account to an existing HMN account, we continue to only request the `identity` scope, so we do not receive the user's Discord email.
Both login and linking go through the same Discord OAuth callback. All flows through the callback try to achieve the same end goal: a logged-in HMN user with a linked Discord account.
Linking works the same as it ever has. Login, however, is different because we do not have a session ID to use as the OAuth state. To account for this, I have added a `pending_login` table that stores a secure unique ID and the eventual destination URL. These pending logins expire after 10 minutes. When we receive the OAuth callback, we look up the pending login by the OAuth `state` and immediately delete it. The destination URL will be used to redirect the user to the right place.
If we have a `discord_user` entry for the OAuth'd Discord user, we immediately log the user into the associated HMN account. This is the typical login case. If we do not have a `discord_user`, but there is exactly one HMN user with the same email address as the Discord user, we will link the two accounts and log into the HMN account.
(It is possible for multiple HMN accounts to have the same email, because we don't have a uniqueness constraint there. We fail the login in this case rather than link to the wrong account.)
Finally, if no associated HMN user exists, a new one will be created. It will use the Discord user's username, email, and avatar. This user will have no password, but they can set or reset a password through the usual flows.
Co-authored-by: Ben Visness <bvisness@gmail.com>
Reviewed-on: https://git.handmade.network/hmn/hmn/pulls/106
2023-05-06 19:38:50 +00:00
|
|
|
auth.PeriodicallyDeleteExpiredStuff(backgroundJobContext, conn),
|
2021-08-08 20:05:52 +00:00
|
|
|
auth.PeriodicallyDeleteInactiveUsers(backgroundJobContext, conn),
|
2022-05-14 05:33:00 +00:00
|
|
|
perfCollector.Job,
|
2021-08-06 23:23:51 +00:00
|
|
|
discord.RunDiscordBot(backgroundJobContext, conn),
|
2021-08-27 03:59:12 +00:00
|
|
|
discord.RunHistoryWatcher(backgroundJobContext, conn),
|
2022-03-22 18:07:43 +00:00
|
|
|
twitch.MonitorTwitchSubscriptions(backgroundJobContext, conn),
|
2022-05-14 05:33:00 +00:00
|
|
|
hmns3.StartServer(backgroundJobContext),
|
2023-05-17 19:33:27 +00:00
|
|
|
assets.BackgroundPreviewGeneration(backgroundJobContext, conn),
|
2024-01-28 17:12:59 +00:00
|
|
|
calendar.MonitorCalendars(backgroundJobContext),
|
2024-05-30 04:19:31 +00:00
|
|
|
buildscss.RunServer(backgroundJobContext),
|
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("cancelling background jobs")
|
2021-03-31 03:55:19 +00:00
|
|
|
cancelBackgroundJobs()
|
2024-05-30 04:19:31 +00:00
|
|
|
logging.Info().Msg("shutting down web server")
|
|
|
|
server.Shutdown(timeout)
|
2021-03-31 03:55:19 +00:00
|
|
|
}()
|
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
|
|
|
|
2022-05-14 05:33:00 +00:00
|
|
|
<-backgroundJobsDone.C
|
2021-03-09 08:05:07 +00:00
|
|
|
},
|
|
|
|
}
|