From 8d8b89b000ed1afef8ff11785327f9435dab63c7 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:28:17 -0500 Subject: [PATCH 1/2] minimal server struct --- backend/internal/server/index.go | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/backend/internal/server/index.go b/backend/internal/server/index.go index a86bc127..9f94d701 100644 --- a/backend/internal/server/index.go +++ b/backend/internal/server/index.go @@ -1,2 +1,46 @@ package server +import ( + "KonferCA/SPUR/db" + "fmt" + "os" + + "github.com/jackc/pgx/v5/pgxpool" + "github.com/labstack/echo/v4" +) + +type Server struct { + DBPool *pgxpool.Pool + Echo *echo.Echo +} + +/* +Initializes a new Server instance and creates a connection pool to the database. +The database connection string can be controlled by setting the following env variables: + +DB_HOST DB_PORT DB_USER DB_PASSWORD DB_NAME DB_SSLMODE +*/ +func New() (*Server, error) { + connStr := fmt.Sprintf( + "host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", + os.Getenv("DB_HOST"), + os.Getenv("DB_PORT"), + os.Getenv("DB_USER"), + os.Getenv("DB_PASSWORD"), + os.Getenv("DB_NAME"), + os.Getenv("DB_SSLMODE"), + ) + pool, err := db.NewPool(connStr) + if err != nil { + return nil, err + } + + e := echo.New() + + s := Server{ + DBPool: pool, + Echo: e, + } + + return &s, nil +} From a89719a6e6ac33f33e18969a2a212f9c0b92cb91 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Mon, 16 Dec 2024 11:04:16 -0500 Subject: [PATCH 2/2] setup helpers for server pkg to setup --- backend/internal/server/index.go | 10 ++++++++++ backend/internal/server/middleware.go | 7 +++++++ backend/internal/server/routes.go | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 backend/internal/server/routes.go diff --git a/backend/internal/server/index.go b/backend/internal/server/index.go index 9f94d701..0d39145d 100644 --- a/backend/internal/server/index.go +++ b/backend/internal/server/index.go @@ -42,5 +42,15 @@ func New() (*Server, error) { Echo: e, } + s.setupMiddlewares() + s.setupRoutes() + return &s, nil } + +/* +Start the server and binds it to the given port. +*/ +func (s *Server) Start(port int) error { + return s.Echo.Start(fmt.Sprintf(":%d", port)) +} diff --git a/backend/internal/server/middleware.go b/backend/internal/server/middleware.go index abb4e431..f9c12b67 100644 --- a/backend/internal/server/middleware.go +++ b/backend/internal/server/middleware.go @@ -1 +1,8 @@ package server + +/* +Setup all the global middlewares used in the server. +*/ +func (s *Server) setupMiddlewares() { + +} diff --git a/backend/internal/server/routes.go b/backend/internal/server/routes.go new file mode 100644 index 00000000..dd51d0ed --- /dev/null +++ b/backend/internal/server/routes.go @@ -0,0 +1,8 @@ +package server + +/* +Setup all the API routes of any version that will be available in this server. +*/ +func (s *Server) setupRoutes() { + +}