40 lines
816 B
Go
40 lines
816 B
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/tenante/api/database"
|
|
)
|
|
|
|
type ApiServer struct {
|
|
Port int
|
|
DatabaseConnection *database.Connection
|
|
}
|
|
|
|
func NewApiServer(port int, database *database.Connection) *ApiServer {
|
|
return &ApiServer{Port: port, DatabaseConnection: database}
|
|
}
|
|
|
|
func (api *ApiServer) Start() error {
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/_/health", api.HealthCheckRoute)
|
|
|
|
srv := &http.Server{
|
|
Addr: fmt.Sprintf(":%d", api.Port),
|
|
Handler: mux,
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 15 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
}
|
|
|
|
fmt.Printf("🚀 Server starting on %s...\n", srv.Addr)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|