Skip to content

Commit

Permalink
Use port from config
Browse files Browse the repository at this point in the history
  • Loading branch information
reindert-vetter authored Aug 19, 2023
1 parent 80281a5 commit 7769e8d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions foundation/console/app_serve.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package console

import (
"github.com/confetti-framework/framework/foundation/http"
"github.com/confetti-framework/framework/inter"
"errors"
net "net/http"
"strconv"
"time"

"github.com/confetti-framework/framework/foundation/http"
"github.com/confetti-framework/framework/inter"
)

// AppServe starts the http server to handle requests.
Expand All @@ -28,6 +30,7 @@ func (s AppServe) Description() string {
func (s AppServe) Handle(c inter.Cli) inter.ExitCode {
app := c.App()
name := app.Make("config.App.Name").(string)
timeout := getTimeout(app)
appProvider := app.Make(inter.AppProvider).(func() inter.App)

// This bootstraps the framework and gets it ready for use, then it will load up
Expand All @@ -42,11 +45,12 @@ func (s AppServe) Handle(c inter.Cli) inter.ExitCode {
server := &net.Server{
Addr: s.getListenAddr(app),
Handler: net.HandlerFunc(handler),
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: timeout,
ReadTimeout: timeout,
IdleTimeout: timeout,
}

if err := server.ListenAndServe(); err != nil && err != net.ErrServerClosed {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, net.ErrServerClosed) {
c.Error("Could not %s", err)
return inter.Failure
}
Expand All @@ -56,6 +60,14 @@ func (s AppServe) Handle(c inter.Cli) inter.ExitCode {
return inter.Success
}

func getTimeout(app inter.App) time.Duration {
timeoutRaw, err := app.MakeE("config.App.Timeout")
if err != nil {
return 30 * time.Second
}
return timeoutRaw.(time.Duration)
}

func (s AppServe) getPortAddr(app inter.App) string {
var port int
if s.Port != 0 {
Expand Down Expand Up @@ -91,3 +103,4 @@ func (s AppServe) getHumanAddr(app inter.App) interface{} {
}
return "http://localhost:" + s.getPortAddr(app)
}

0 comments on commit 7769e8d

Please sign in to comment.