Jobs & Workflows https://armen.surge.sh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.9 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### server ####### Copyright (c) 2021 mls-361 ################################################## MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package server
import (
"context"
"fmt"
"log"
"net/http"
"time"
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/components"
"forge.chapril.org/armen/armen/internal/middleware"
)
type server struct {
logger components.Logger
server *http.Server
}
func newServer(logger components.Logger) *server {
return &server{
logger: logger,
}
}
func Build(ccs *components.Components) (components.Server, error) {
cs := newServer(ccs.Logger.NewLogger(uuid.New(), "server"))
cfg := ccs.Config.Server()
handler := middleware.Trace(ccs.Router.Handler(), ccs.Logger)
cs.server = &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: handler,
ErrorLog: cs.logger.NewStdLogger("error", "", log.Llongfile),
IdleTimeout: 1 * time.Minute,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
return cs, nil
}
// Start AFAIRE.
func (cs *server) Start() error {
err := cs.server.ListenAndServe()
if err == http.ErrServerClosed {
return nil
}
return err
}
// Stop AFAIRE.
func (cs *server) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cs.server.SetKeepAlivesEnabled(false)
if err := cs.server.Shutdown(ctx); err != nil {
cs.logger.Error( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Server shutdown error",
"reason", err,
)
}
}
/*
######################################################################################################## @(°_°)@ #######
*/