Brijesh's Git Server — navigator-v2 @ 19f964e87980aeb2d2f6a016091d7bfe1bdb3732

api/internal/server/server.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
package server

import (
	"fmt"
	"net/http"
	"os"
	"strconv"
	"time"

	_ "github.com/joho/godotenv/autoload"

	"api/internal/browser"
	"api/internal/database"
)

type Server struct {
	port           int
	db             database.Service
	browserService browser.Service
}

func NewServer() *http.Server {
	port, _ := strconv.Atoi(os.Getenv("PORT"))

	// Initialize database
	db := database.New()

	// Initialize browser service with default config
	browserService := browser.NewService(db, database.DefaultConfig())

	NewServer := &Server{
		port:           port,
		db:             db,
		browserService: browserService,
	}

	// Declare Server config
	server := &http.Server{
		Addr:         fmt.Sprintf(":%d", NewServer.port),
		Handler:      NewServer.RegisterRoutes(),
		IdleTimeout:  time.Minute,
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 30 * time.Second,
	}

	return server
}