package main

import (
	"git.mowie.cc/konrad/Library/models"
	"git.mowie.cc/konrad/Library/routes"

	"fmt"
	"os"
	"time"
	"os/signal"
	"context"
)

// UserLogin Object to recive user credentials in JSON format
type UserLogin struct {
	Username string `json:"username" form:"username"`
	Password string `json:"password" form:"password"`
}

func main() {

	// Init Config
	err := models.SetConfig()
	if err != nil {
		fmt.Println(err)
	}

	// Set Engine
	err = models.SetEngine()
	if err != nil {
		fmt.Println(err)
	}

	// Start the webserver
	e := routes.NewEcho()
	routes.RegisterRoutes(e)
	// Start server
	go func() {
		if err := e.Start(models.Config.Interface); err != nil {
			e.Logger.Info("shutting down the server")
		}
	}()

	// Wait for interrupt signal to gracefully shutdown the server with
	// a timeout of 10 seconds.
	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	fmt.Println("Shutting down...")
	if err := e.Shutdown(ctx); err != nil {
		e.Logger.Fatal(err)
	}
}
