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
4 changed files with 47 additions and 6 deletions

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
}