ch12.3
This commit is contained in:
42
cmd/api/server.go
Normal file
42
cmd/api/server.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user