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:
parent
a24be8b2ff
commit
dc16506932
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
# Created by https://www.toptal.com/developers/gitignore/api/jetbrains+all,go
|
# Created by https://www.toptal.com/developers/gitignore/api/jetbrains+all,go
|
||||||
# Edit at https://www.toptal.com/developers/gitignore?templates=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
|
# End of https://www.toptal.com/developers/gitignore/api/jetbrains+all,go
|
||||||
|
|
||||||
web/*.gz
|
.env
|
||||||
data/
|
|
||||||
.env
|
|
||||||
|
@ -2,12 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"github.com/lus/pasty/internal/config"
|
"github.com/lus/pasty/internal/config"
|
||||||
"github.com/lus/pasty/internal/meta"
|
"github.com/lus/pasty/internal/meta"
|
||||||
"github.com/lus/pasty/internal/storage"
|
"github.com/lus/pasty/internal/storage"
|
||||||
"github.com/lus/pasty/internal/storage/postgres"
|
"github.com/lus/pasty/internal/storage/postgres"
|
||||||
|
"github.com/lus/pasty/internal/web"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"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
|
// Wait for an interrupt signal
|
||||||
log.Info().Msg("The application has been started. Use Ctrl+C to shut it down.")
|
log.Info().Msg("The application has been started. Use Ctrl+C to shut it down.")
|
||||||
shutdownChan := make(chan os.Signal, 1)
|
shutdownChan := make(chan os.Signal, 1)
|
||||||
|
@ -54,7 +54,6 @@ func (driver *Driver) Initialize(ctx context.Context) error {
|
|||||||
pool.Close()
|
pool.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Info().Msg("Successfully performed PostgreSQL database migrations.")
|
|
||||||
|
|
||||||
driver.connPool = pool
|
driver.connPool = pool
|
||||||
driver.pastes = &pasteRepository{
|
driver.pastes = &pasteRepository{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/lus/pasty/internal/pastes"
|
"github.com/lus/pasty/internal/pastes"
|
||||||
"github.com/lus/pasty/internal/storage"
|
"github.com/lus/pasty/internal/storage"
|
||||||
@ -35,6 +36,8 @@ type Server struct {
|
|||||||
|
|
||||||
// The administration tokens.
|
// The administration tokens.
|
||||||
AdminTokens []string
|
AdminTokens []string
|
||||||
|
|
||||||
|
httpServer *http.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) Start() error {
|
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.With(server.v2MiddlewareInjectPaste, server.v2MiddlewareAuthorize).Patch("/api/v2/pastes/{paste_id}", server.v2EndpointModifyPaste)
|
||||||
router.Delete("/api/v2/pastes/{paste_id}", server.v2EndpointDeletePaste)
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user