Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init server #168

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions backend/internal/server/index.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
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,
}

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))
}
7 changes: 7 additions & 0 deletions backend/internal/server/middleware.go
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
package server

/*
Setup all the global middlewares used in the server.
*/
func (s *Server) setupMiddlewares() {

}
8 changes: 8 additions & 0 deletions backend/internal/server/routes.go
Original file line number Diff line number Diff line change
@@ -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() {

}
Loading