package server

import (
	"fmt"
	"net/http"

	"git.linuxit.ro/turos.robert/mynotes/lib/inout"
)

func NewServer(filename string) *Server {
	srv := Server{}
	err := inout.FileToObj(filename, &srv)
	if err != nil {
		srv.Err = err
	}

	return &srv

}

func (s *Server) Run() {
	fmt.Println(s.Port, s.Host)
	// adding endpoints to the server
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		Wildcard(w, r, s)
	})

	//running server
	fmt.Printf("Server running at http://%s:%s/\n", s.Host, s.Port)
	err := http.ListenAndServe(fmt.Sprintf("%s:%s", s.Host, s.Port), nil)
	if err != nil {
		fmt.Printf("Error starting server: %+v\n", err)
	}
}
