package kubelwagen

import (
	"fmt"
	"os"
	"os/signal"
	"time"

	"github.com/sirupsen/logrus"
)

func Serve(hostport string, dir string, opts *WsFsOpts) {
	closer := make(chan bool)

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)

	go func() {
		for range signalChan {
			fmt.Println("\nReceived an interrupt, disconnecting...")
			close(closer)
		}
	}()

	req := make(chan RequestCallback, 5)

	go func(dir string, closer chan bool) {
		err := serveFuse(dir, req, *opts, closer)
		if err != nil {
			logrus.Fatalf("Error serving FUSE: %s", err)
		}
	}(dir, closer)

	go func(hostport string, closer chan bool) {
		err := serveHTTP(hostport, req, closer)
		if err != nil {
			logrus.Fatalf("Error serving HTTP: %s", err)
		}
	}(hostport, closer)

	<-closer
	time.Sleep(2 * time.Second)
}
