Moving starting server part into new server.go file. Using the new serve() function from this file into main.go
This commit is contained in:
25
cmd/api/server.go
Normal file
25
cmd/api/server.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (app *application) serve() error {
|
||||
// Declare a HTTP server which listens on the port provided in the config struct, uses the servemux we created above as the handler, has some sensible timeout settings and writes any log messages to the structured logger at Error level.
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", app.config.port),
|
||||
Handler: app.routes(),
|
||||
IdleTimeout: time.Minute,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
ErrorLog: slog.NewLogLogger(app.logger.Handler(), slog.LevelError),
|
||||
}
|
||||
|
||||
// Start the HTTP server.
|
||||
app.logger.Info("starting server", "addr", srv.Addr, "env", app.config.env)
|
||||
|
||||
return srv.ListenAndServe()
|
||||
}
|
||||
Reference in New Issue
Block a user