package server

import (
	"context"
	"net/http"

	"github.com/fiskerinc/cloud-services/services/gateway/handlers"

	"github.com/fiskerinc/cloud-services/pkg/httphandlers"
	"github.com/fiskerinc/cloud-services/pkg/logger"
	httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)

// StartHTTP runs server for websockets and docs
func StartHTTP(ctx context.Context, port string) {
	mux := httptrace.NewServeMux()

	if docsHandler := handlers.DocsHandler(); docsHandler != nil {
		mux.Handle("/docs/", http.StripPrefix("/docs", docsHandler))
	}

	mux.HandleFunc("/session", httphandlers.PanicHandler(handlers.SecureSessionWebsocketHandler))
	mux.HandleFunc("/secret_mobile", httphandlers.PanicHandler(handlers.InsecureSessionWebsocketHandler))

	logger.Info().Msgf("gateway websockets on http://0.0.0.0%s", port)

	logger.Fatal().AnErr("http.ListenAndServe", http.ListenAndServe(port, mux)).Send()
}
