{{>partial_header}}
package swagger

import (
    "net/http"
    "strconv"

    "github.com/lbryio/chainquery/config"
    "github.com/lbryio/chainquery/db"
    sw "github.com/lbryio/chainquery/swagger/apiserver/go"
    "github.com/lbryio/lbry.go/extras/api"

    "github.com/fatih/color"
    "github.com/sirupsen/logrus"
)

func InitApiServer(hostAndPort string) {
    logrus.Info("API Server started")
    hs := make(map[string]string)
    hs["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
    hs["Content-Type"] = "application/json; charset=utf-8; application/x-www-form-urlencoded"
    hs["X-Content-Type-Options"] = "nosniff"
    hs["Content-Security-Policy"] = "default-src 'none'"
    hs["Server"] = "lbry.com"
    hs["Access-Control-Allow-Origin"] = "*"
    api.ResponseHeaders = hs
    api.Log = func(request *http.Request, response *api.Response, err error) {
        if err != nil {
            logrus.Error(err)
        }
        consoleText := request.RemoteAddr + " [" + strconv.Itoa(response.Status) + "]: " + request.Method + " " + request.URL.Path
        logrus.Debug(color.GreenString(consoleText))

    }
    //API Chainquery DB connection
    chainqueryInstance, err := db.InitAPIQuery(config.GetAPIMySQLDSN(), false)
    if err != nil {
        logrus.Panic("unable to connect to chainquery database instance for API Server: ", err)
    }
    defer db.CloseDB(chainqueryInstance)
    router := sw.NewRouter()

    logrus.Fatal(http.ListenAndServe(hostAndPort, router))
}
