1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

integrate the web server into the startup logic

This commit is contained in:
Lukas Schulte Pelkum 2023-06-08 19:44:32 +02:00
parent a24be8b2ff
commit dc16506932
No known key found for this signature in database
GPG Key ID: AB3985CECFAFC962
4 changed files with 47 additions and 6 deletions

5
.gitignore vendored
View File

@ -1,4 +1,3 @@
# Created by https://www.toptal.com/developers/gitignore/api/jetbrains+all,go
# Edit at https://www.toptal.com/developers/gitignore?templates=jetbrains+all,go
@ -114,6 +113,4 @@ modules.xml
# End of https://www.toptal.com/developers/gitignore/api/jetbrains+all,go
web/*.gz
data/
.env
.env

View File

@ -2,12 +2,15 @@ package main
import (
"context"
"errors"
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/meta"
"github.com/lus/pasty/internal/storage"
"github.com/lus/pasty/internal/storage/postgres"
"github.com/lus/pasty/internal/web"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"net/http"
"os"
"os/signal"
"strings"
@ -65,6 +68,32 @@ func main() {
}
}()
// Start the web server
log.Info().Str("address", cfg.WebAddress).Msg("Starting the web server...")
webServer := &web.Server{
Address: cfg.WebAddress,
Storage: driver,
HastebinSupport: cfg.HastebinSupport,
PasteIDLength: cfg.IDLength,
PasteIDCharset: cfg.IDCharacters,
PasteLengthCap: cfg.LengthCap,
ModificationTokensEnabled: cfg.ModificationTokens,
ModificationTokenLength: cfg.ModificationTokenLength,
ModificationTokenCharset: cfg.ModificationTokenCharacters,
AdminTokens: []string{cfg.ModificationTokenMaster},
}
go func() {
if err := webServer.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("Could not start the web server.")
}
}()
defer func() {
log.Info().Msg("Shutting down the web server...")
if err := webServer.Shutdown(context.Background()); err != nil {
log.Err(err).Msg("Could not shut down the web server.")
}
}()
// Wait for an interrupt signal
log.Info().Msg("The application has been started. Use Ctrl+C to shut it down.")
shutdownChan := make(chan os.Signal, 1)

View File

@ -54,7 +54,6 @@ func (driver *Driver) Initialize(ctx context.Context) error {
pool.Close()
return err
}
log.Info().Msg("Successfully performed PostgreSQL database migrations.")
driver.connPool = pool
driver.pastes = &pasteRepository{

View File

@ -1,6 +1,7 @@
package web
import (
"context"
"github.com/go-chi/chi/v5"
"github.com/lus/pasty/internal/pastes"
"github.com/lus/pasty/internal/storage"
@ -35,6 +36,8 @@ type Server struct {
// The administration tokens.
AdminTokens []string
httpServer *http.Server
}
func (server *Server) Start() error {
@ -60,5 +63,18 @@ func (server *Server) Start() error {
router.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Patch("/api/v2/pastes/{paste_id}", server.v2EndpointModifyPaste)
router.Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
return http.ListenAndServe(server.Address, router)
// Start the HTTP server
server.httpServer = &http.Server{
Addr: server.Address,
Handler: router,
}
return server.httpServer.ListenAndServe()
}
func (server *Server) Shutdown(ctx context.Context) error {
if err := server.httpServer.Shutdown(ctx); err != nil {
return err
}
server.httpServer = nil
return nil
}