generated from example/golang-server-template
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func (app *application) serve() error {
|
|
app.initAuth()
|
|
srv := &http.Server{
|
|
Addr: fmt.Sprintf(":%d", app.config.port),
|
|
Handler: app.routes(),
|
|
IdleTimeout: time.Minute,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 30 * time.Second,
|
|
}
|
|
|
|
shutdownError := make(chan error)
|
|
|
|
go func() {
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
s := <-quit
|
|
app.logger.PrintInfo("shutting down server", map[string]string{
|
|
"signal": s.String(),
|
|
})
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
err := srv.Shutdown(ctx)
|
|
if err != nil {
|
|
shutdownError <- err
|
|
}
|
|
app.logger.PrintInfo("completing background tasks", map[string]string{
|
|
"addr": srv.Addr,
|
|
})
|
|
app.wg.Wait()
|
|
shutdownError <- nil
|
|
}()
|
|
app.logger.PrintInfo("starting server", map[string]string{
|
|
"addr": srv.Addr,
|
|
"env": app.config.env,
|
|
})
|
|
err := srv.ListenAndServe()
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
err = <-shutdownError
|
|
if err != nil {
|
|
return err
|
|
}
|
|
app.logger.PrintInfo("stopped server", map[string]string{
|
|
"addr": srv.Addr,
|
|
})
|
|
return nil
|
|
}
|