Files
probo/pkg/server/server.go
Bryan Frimin 5370d59451 Add connector service and http handlers
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
2025-04-20 21:16:16 -07:00

103 lines
2.9 KiB
Go

// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// Package server provides functionality for serving the SPA frontend.
package server
import (
"net/http"
"strings"
"github.com/getprobo/probo/pkg/connector"
"github.com/getprobo/probo/pkg/probo"
"github.com/getprobo/probo/pkg/server/api"
console_v1 "github.com/getprobo/probo/pkg/server/api/console/v1"
"github.com/getprobo/probo/pkg/server/web"
"github.com/getprobo/probo/pkg/usrmgr"
"github.com/go-chi/chi/v5"
)
// Config holds the configuration for the server
type Config struct {
AllowedOrigins []string
Probo *probo.Service
Usrmgr *usrmgr.Service
Auth console_v1.AuthConfig
ConnectorRegistry *connector.ConnectorRegistry
}
// Server represents the main server that handles both API and frontend requests
type Server struct {
apiServer *api.Server
webServer *web.Server
router *chi.Mux
}
// NewServer creates a new server instance
func NewServer(cfg Config) (*Server, error) {
// Create API server
apiCfg := api.Config{
AllowedOrigins: cfg.AllowedOrigins,
Probo: cfg.Probo,
Usrmgr: cfg.Usrmgr,
Auth: cfg.Auth,
ConnectorRegistry: cfg.ConnectorRegistry,
}
apiServer, err := api.NewServer(apiCfg)
if err != nil {
return nil, err
}
// Create web server for SPA
webServer, err := web.NewServer()
if err != nil {
return nil, err
}
// Create main router
router := chi.NewRouter()
server := &Server{
apiServer: apiServer,
webServer: webServer,
router: router,
}
// Set up routes
server.setupRoutes()
return server, nil
}
// setupRoutes configures the routing for the server
func (s *Server) setupRoutes() {
// API routes under /api
s.router.Mount("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Strip the /api prefix from the path
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/api")
if r.URL.Path == "" {
r.URL.Path = "/"
}
s.apiServer.ServeHTTP(w, r)
}))
// All other routes go to the SPA frontend
s.router.Mount("/", s.webServer)
}
// ServeHTTP implements the http.Handler interface
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}