/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  stage0_ext2.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver.
 *
 * kvmstoreserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kvmstoreserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kvmstoreserver.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package main

import (
	fh "kvmstoreserver_utils/HttpHandlers/finalHandlers"
	mf "kvmstoreserver_utils/HttpHandlers/middlewareFunctions"
	"net/http"
	fcb "utils/Middleware/functionChainBuilder"
	conf "utils/configMap"
	"versionInfo"

	log "github.com/judwhite/logrjack"
)

// Here, middleware chian containers can be formed and the corresponding routes
// defined. A convention valid in this file requires setting log entries for
// loading containers and routes
func initializeMiddleware() {

	//*****************************************************************************
	//*
	//*      Create and set Middleware components and final handlers
	//*
	//*****************************************************************************

	// Setup controller /add/
	ctrlAddChain := fcb.NewChainConatainer().
		RegisterFunctions(mf.MWareInOutLogger, mf.MWareIfMethodPost, mf.MWareCheckKeyInURL).
		SetFinalHandler(fh.AddFinalHandler)

	log.Infof("Initialize chain container for route -> %v",
		conf.GetStringOf("ctrl_add_path"))

	// Setup controller /get/
	ctrlGetChain := fcb.NewChainConatainer().
		RegisterFunctions(mf.MWareInOutLogger, mf.MWareIfMethodGet, mf.MWareCheckKeyInURL, mf.MwareCheckParamIntegrity).
		SetFinalHandler(fh.GetFinalHandler)

	log.Infof("Initialize chain container for route -> %v",
		conf.GetStringOf("ctrl_get_path"))

	// Setup controller /del/
	ctrlDelChain := fcb.NewChainConatainer().
		RegisterFunctions(mf.MWareInOutLogger, mf.MWareIfMethodDelete, mf.MWareCheckKeyInURL).
		SetFinalHandler(fh.DelFinalHandler)

	log.Infof("Initialize chain container for route -> %v",
		conf.GetStringOf("ctrl_del_path"))

	// Setup controller /tree
	ctrlTreeChain := fcb.NewChainConatainer().
		RegisterFunctions(mf.MWareInOutLogger, mf.MWareIfMethodGet).
		SetFinalHandler(fh.TreeFinalHandler)

	log.Infof("Initialize chain container for route -> %v",
		conf.GetStringOf("ctrl_tree_path"))

	// Setup controller /tree
	ctrlVersionChain := fcb.NewChainConatainer().
		RegisterFunctions(mf.MWareInOutLogger, mf.MWareIfMethodGet).
		SetFinalHandler(versionInfo.VerInfoFinalHandler)

	log.Infof("Initialize chain container for route -> %v",
		conf.GetStringOf("ctrl_version_path"))

	//*****************************************************************************
	//*
	//*      Configure Routes
	//*
	//*****************************************************************************

	// Configure ROUTE for controller /add/
	http.Handle(conf.GetStringOf("ctrl_add_path"), ctrlAddChain.RunChain())

	log.Infof("Set controller into route -> %v",
		conf.GetStringOf("ctrl_add_path"))

	// Configure ROUTE for controller /get/
	http.Handle(conf.GetStringOf("ctrl_get_path"), ctrlGetChain.RunChain())

	log.Infof("Set controller into route -> %v",
		conf.GetStringOf("ctrl_get_path"))

	// Configure ROUTE for controller /del/
	http.Handle(conf.GetStringOf("ctrl_del_path"), ctrlDelChain.RunChain())

	log.Infof("Set controller into route -> %v",
		conf.GetStringOf("ctrl_del_path"))

	// Configure ROUTE for controller /tree
	http.Handle(conf.GetStringOf("ctrl_tree_path"), ctrlTreeChain.RunChain())

	log.Infof("Set controller into route -> %v",
		conf.GetStringOf("ctrl_tree_path"))

	// Configure ROUTE for controller /tree
	http.Handle(conf.GetStringOf("ctrl_version_path"), ctrlVersionChain.RunChain())

	log.Infof("Set controller into route -> %v",
		conf.GetStringOf("ctrl_version_path"))
}
