func NewHTTPd(url string, site core.SiteAPI, hub *SocketHub, cache *util.Cache) *HTTPd {
	routes := map[string]route{
		"health":    {[]string{"GET"}, "/health", HealthHandler(site)},
		"state":     {[]string{"GET"}, "/state", StateHandler(cache)},
		"templates": {[]string{"GET"}, "/config/templates/{class:[a-z]+}", TemplatesHandler()},
	}

	router := mux.NewRouter().StrictSlash(true)

	// websocket
	router.HandleFunc("/ws", SocketHandler(hub))

	// static - individual handlers per root and folders
	static := router.PathPrefix("/").Subrouter()
	static.Use(handlers.CompressHandler)

	static.HandleFunc("/", indexHandler(site))
	for _, dir := range []string{"css", "js", "ico"} {
		static.PathPrefix("/" + dir).Handler(http.FileServer(http.FS(Assets)))
	}

	// api
	api := router.PathPrefix("/api").Subrouter()
	api.Use(jsonHandler)
	api.Use(handlers.CompressHandler)
	api.Use(handlers.CORS(
		handlers.AllowedHeaders([]string{
			"Accept", "Accept-Language", "Content-Language", "Content-Type", "Origin",
		}),
	))

	// site api
	for _, r := range routes {
		api.Methods(r.Methods...).Path(r.Pattern).Handler(r.HandlerFunc)
	}

	// loadpoint api
	for id, lp := range site.LoadPoints() {
		lpAPI := api.PathPrefix(fmt.Sprintf("/loadpoints/%d", id)).Subrouter()

		routes := map[string]route{
			"getmode":         {[]string{"GET"}, "/mode", CurrentChargeModeHandler(lp)},
			"setmode":         {[]string{"POST", "OPTIONS"}, "/mode/{mode:[a-z]+}", ChargeModeHandler(lp)},
			"gettargetsoc":    {[]string{"GET"}, "/targetsoc", CurrentTargetSoCHandler(lp)},
			"settargetsoc":    {[]string{"POST", "OPTIONS"}, "/targetsoc/{soc:[0-9]+}", TargetSoCHandler(lp)},
			"getminsoc":       {[]string{"GET"}, "/minsoc", CurrentMinSoCHandler(lp)},
			"setminsoc":       {[]string{"POST", "OPTIONS"}, "/minsoc/{soc:[0-9]+}", MinSoCHandler(lp)},
			"settargetcharge": {[]string{"POST", "OPTIONS"}, "/targetcharge/{soc:[0-9]+}/{time:[0-9TZ:-]+}", TargetChargeHandler(lp)},
			"remotedemand":    {[]string{"POST", "OPTIONS"}, "/remotedemand/{demand:[a-z]+}/{source}", RemoteDemandHandler(lp)},
		}

		for _, r := range routes {
			lpAPI.Methods(r.Methods...).Path(r.Pattern).Handler(r.HandlerFunc)
		}
	}

	srv := &HTTPd{
		Server: &http.Server{
			Addr:         url,
			Handler:      router,
			ReadTimeout:  5 * time.Second,
			WriteTimeout: 10 * time.Second,
			IdleTimeout:  120 * time.Second,
			ErrorLog:     log.ERROR,
		},
	}
	srv.SetKeepAlivesEnabled(true)

	return srv
}
// NewHTTPd creates HTTP server with configured routes for loadpoint
NewHTTPd creates HTTP server with configured routes for loadpoint
// NewHTTPd creates HTTP server with configured routes for loadpoint
func NewHTTPd(url string, site core.SiteAPI, hub *SocketHub, cache *util.Cache) *HTTPd {
	routes := map[string]route{
		"health":    {[]string{"GET"}, "/health", HealthHandler(site)},
		"state":     {[]string{"GET"}, "/state", StateHandler(cache)},
		"templates": {[]string{"GET"}, "/config/templates/{class:[a-z]+}", TemplatesHandler()},
	}
	router := mux.NewRouter().StrictSlash(true)
	router.HandleFunc("/ws", SocketHandler(hub))
	static := router.PathPrefix("/").Subrouter()
	static.Use(handlers.CompressHandler)
	static.HandleFunc("/", indexHandler(site))
	for _, dir := range []string{"css", "js", "ico"} {
		static.PathPrefix("/" + dir).Handler(http.FileServer(http.FS(Assets)))
	}
	api := router.PathPrefix("/api").Subrouter()
	api.Use(jsonHandler)
	api.Use(handlers.CompressHandler)
	api.Use(handlers.CORS(
		handlers.AllowedHeaders([]string{
			"Accept", "Accept-Language", "Content-Language", "Content-Type", "Origin",
		}),
	))
	for _, r := range routes {
		api.Methods(r.Methods...).Path(r.Pattern).Handler(r.HandlerFunc)
	}
	for id, lp := range site.LoadPoints() {
		lpAPI := api.PathPrefix(fmt.Sprintf("/loadpoints/%d", id)).Subrouter()
		routes := map[string]route{
			"getmode":         {[]string{"GET"}, "/mode", CurrentChargeModeHandler(lp)},
			"setmode":         {[]string{"POST", "OPTIONS"}, "/mode/{mode:[a-z]+}", ChargeModeHandler(lp)},
			"gettargetsoc":    {[]string{"GET"}, "/targetsoc", CurrentTargetSoCHandler(lp)},
			"settargetsoc":    {[]string{"POST", "OPTIONS"}, "/targetsoc/{soc:[0-9]+}", TargetSoCHandler(lp)},
			"getminsoc":       {[]string{"GET"}, "/minsoc", CurrentMinSoCHandler(lp)},
			"setminsoc":       {[]string{"POST", "OPTIONS"}, "/minsoc/{soc:[0-9]+}", MinSoCHandler(lp)},
			"settargetcharge": {[]string{"POST", "OPTIONS"}, "/targetcharge/{soc:[0-9]+}/{time:[0-9TZ:-]+}", TargetChargeHandler(lp)},
			"remotedemand":    {[]string{"POST", "OPTIONS"}, "/remotedemand/{demand:[a-z]+}/{source}", RemoteDemandHandler(lp)},
		}
		for _, r := range routes {
			lpAPI.Methods(r.Methods...).Path(r.Pattern).Handler(r.HandlerFunc)
		}
	}
	srv := &HTTPd{
		Server: &http.Server{
			Addr:         url,
			Handler:      router,
			ReadTimeout:  5 * time.Second,
			WriteTimeout: 10 * time.Second,
			IdleTimeout:  120 * time.Second,
			ErrorLog:     log.ERROR,
		},
	}
	srv.SetKeepAlivesEnabled(true)
	return srv
}
func NewHTTPd ( url string , site core . SiteAPI , hub * SocketHub , cache * util . Cache ) * HTTPd { routes := map [ string ] route { " " : { [ ] string { " " } , " " , HealthHandler ( site ) } , " " : { [ ] string { " " } , " " , StateHandler ( cache ) } , " " : { [ ] string { " " } , " " , TemplatesHandler ( ) } , } router := mux . NewRouter ( ) . StrictSlash ( true ) router . HandleFunc ( " " , SocketHandler ( hub ) ) static := router . PathPrefix ( " " ) . Subrouter ( ) static . Use ( handlers . CompressHandler ) static . HandleFunc ( " " , indexHandler ( site ) ) for _ , dir := range [ ] string { " " , " " , " " } { static . PathPrefix ( " " + dir ) . Handler ( http . FileServer ( http . FS ( Assets ) ) ) } api := router . PathPrefix ( " " ) . Subrouter ( ) api . Use ( jsonHandler ) api . Use ( handlers . CompressHandler ) api . Use ( handlers . CORS ( handlers . AllowedHeaders ( [ ] string { " " , " " , " " , " " , " " , } ) , ) ) for _ , r := range routes { api . Methods ( r . Methods ... ) . Path ( r . Pattern ) . Handler ( r . HandlerFunc ) } for id , lp := range site . LoadPoints ( ) { lpAPI := api . PathPrefix ( fmt . Sprintf ( " " , id ) ) . Subrouter ( ) routes := map [ string ] route { " " : { [ ] string { " " } , " " , CurrentChargeModeHandler ( lp ) } , " " : { [ ] string { " " , " " } , " " , ChargeModeHandler ( lp ) } , " " : { [ ] string { " " } , " " , CurrentTargetSoCHandler ( lp ) } , " " : { [ ] string { " " , " " } , " " , TargetSoCHandler ( lp ) } , " " : { [ ] string { " " } , " " , CurrentMinSoCHandler ( lp ) } , " " : { [ ] string { " " , " " } , " " , MinSoCHandler ( lp ) } , " " : { [ ] string { " " , " " } , " " , TargetChargeHandler ( lp ) } , " " : { [ ] string { " " , " " } , " " , RemoteDemandHandler ( lp ) } , } for _ , r := range routes { lpAPI . Methods ( r . Methods ... ) . Path ( r . Pattern ) . Handler ( r . HandlerFunc ) } } srv := & HTTPd { Server : & http . Server { Addr : url , Handler : router , ReadTimeout : 5 * time . Second , WriteTimeout : 10 * time . Second , IdleTimeout : 120 * time . Second , ErrorLog : log . ERROR , } , } srv . SetKeepAlivesEnabled ( true ) return srv }
NewHTTPd creates HTTP server with configured routes for loadpoint
NewHTTPd creates HTTP server with configured routes for loadpoint
// websocket // static - individual handlers per root and folders // api // site api // loadpoint api