This commit is contained in:
lbenedar
2026-03-19 15:32:03 +03:00
parent 471393fa04
commit c4f290b389
2 changed files with 45 additions and 16 deletions

View File

@@ -4,9 +4,6 @@ import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
@@ -97,18 +94,8 @@ func main() {
models: data.NewModels(db),
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.port),
Handler: app.routes(),
ErrorLog: log.New(logger, "", 0),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
logger.PrintInfo("starting server", map[string]string{
"addr": srv.Addr,
"env": cfg.env,
})
err = srv.ListenAndServe()
err = app.serve()
if err != nil {
logger.PrintFatal(err, nil)
}
}

42
cmd/api/server.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func (app *application) serve() error {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.port),
Handler: app.routes(),
ErrorLog: log.New(app.logger, "", 0),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
app.logger.PrintInfo("caught signal", map[string]string{
"signal": s.String(),
})
os.Exit(0)
}()
app.logger.PrintInfo("starting server", map[string]string{
"addr": srv.Addr,
"env": app.config.env,
})
return srv.ListenAndServe()
}