feat: Create collection file for routes
This commit is contained in:
39
server/api.go
Normal file
39
server/api.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user