package web

import (
	"fmt"
	"net/http"
)

type WebServer struct {
	*http.Server
}

func NewWebServer(port int) (*WebServer, error) {
	mux := http.NewServeMux()

	// ToDo: configure routes
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello craban!!")
	})

	s := &WebServer{
		&http.Server{
			Addr:    fmt.Sprintf(":%d", port),
			Handler: mux,
		},
	}

	return s, nil
}

func (w *WebServer) Start() error {
	if err := w.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
		return err
	}
	return nil
}

func (w *WebServer) Close() error {
	return w.Server.Close()
}
