<commit_before>//tegola server
package main

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/BurntSushi/toml"

	"github.com/terranodo/tegola/mvt"
	"github.com/terranodo/tegola/mvt/provider"
	_ "github.com/terranodo/tegola/provider/postgis"
	"github.com/terranodo/tegola/server"
)

type Config struct {
	Webserver struct {
		Port      string
		LogFile   string `toml:"log_file"`
		LogFormat string `toml:"log_format"`
	}
	Providers []map[string]interface{}
	Maps      []Map
}

type Map struct {
	Name   string `toml:"name"`
	Layers []struct {
		ProviderLayer string      `toml:"provider_layer"`
		MinZoom       int         `toml:"min_zoom"`
		MaxZoom       int         `toml:"max_zoom"`
		DefaultTags   interface{} `toml:"default_tags"`
	} `toml:"layers"`
}

func main() {
	var err error

	//	parse our command line flags
	flag.Parse()

	conf, err := loadConfig(configFile)
	if err != nil {
		log.Fatal(err)
	}

	//	init our providers
	providers, err := initProviders(conf.Providers)
	if err != nil {
		log.Fatal(err)
	}

	//	init our maps
	if err = initMaps(conf.Maps, providers); err != nil {
		log.Fatal(err)
	}

	initLogger(logFile, logFormat, conf.Webserver.LogFile, conf.Webserver.LogFormat)

	//	if port was not set via the command line
	if port == "" {
		//	do we have a port in our config file
		if conf.Webserver.Port != "" {
			port = conf.Webserver.Port
		}
	}

	//	start our webserver
	server.Start(port)
}

//	parseConfig handles loading a config file locally or remote over http(s)
func loadConfig(confLocation string) (Config, error) {
	var err error
	var conf Config
	var reader io.Reader

	//	check for http prefix
	if strings.HasPrefix(confLocation, "http") {
		log.Printf("Loading remote config (%v)", confLocation)

		//	setup http client with a timeout
		var httpClient = &http.Client{
			Timeout: time.Second * 10,
		}

		//	make the http request
		res, err := httpClient.Get(confLocation)
		if err != nil {
			return conf, fmt.Errorf("error fetching remote config file (%v): %v ", confLocation, err)
		}

		//	set the reader to the response body
		reader = res.Body
	} else {
		log.Printf("Loading local config (%v)", confLocation)

		//	check the conf file exists
		if _, err := os.Stat(confLocation); os.IsNotExist(err) {
			return conf, fmt.Errorf("config file at location (%v) not found!", confLocation)
		}

		//	open the confi file
		reader, err = os.Open(confLocation)
		if err != nil {
			return conf, fmt.Errorf("error opening local config file (%v): %v ", confLocation, err)
		}
	}

	//	decode conf file
	if _, err := toml.DecodeReader(reader, &conf); err != nil {
		return conf, nil
	}

	return conf, nil
}

//	initMaps registers maps with our server
func initMaps(maps []Map, providers map[string]mvt.Provider) error {

	//	iterate our maps
	for _, m := range maps {
		var layers []server.Layer
		//	iterate our layers
		for _, l := range m.Layers {
			//	split our provider name (provider.layer) into [provider,layer]
			providerLayer := strings.Split(l.ProviderLayer, ".")

			//	we're expecting two params in the provider layer definition
			if len(providerLayer) != 2 {
				return fmt.Errorf("invalid provider layer (%v) for map (%v)", l.ProviderLayer, m)
			}

			//	lookup our proivder
			provider, ok := providers[providerLayer[0]]
			if !ok {
				return fmt.Errorf("provider (%v) not defined", providerLayer[0])
			}<commit_msg>if toml config parsing fails, return error. was returning nil. closes #80<commit_after>//tegola server
package main

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/BurntSushi/toml"

	"github.com/terranodo/tegola/mvt"
	"github.com/terranodo/tegola/mvt/provider"
	_ "github.com/terranodo/tegola/provider/postgis"
	"github.com/terranodo/tegola/server"
)

type Config struct {
	Webserver struct {
		Port      string
		LogFile   string `toml:"log_file"`
		LogFormat string `toml:"log_format"`
	}
	Providers []map[string]interface{}
	Maps      []Map
}

type Map struct {
	Name   string `toml:"name"`
	Layers []struct {
		ProviderLayer string      `toml:"provider_layer"`
		MinZoom       int         `toml:"min_zoom"`
		MaxZoom       int         `toml:"max_zoom"`
		DefaultTags   interface{} `toml:"default_tags"`
	} `toml:"layers"`
}

func main() {
	var err error

	//	parse our command line flags
	flag.Parse()

	conf, err := loadConfig(configFile)
	if err != nil {
		log.Fatal(err)
	}

	//	init our providers
	providers, err := initProviders(conf.Providers)
	if err != nil {
		log.Fatal(err)
	}

	//	init our maps
	if err = initMaps(conf.Maps, providers); err != nil {
		log.Fatal(err)
	}

	initLogger(logFile, logFormat, conf.Webserver.LogFile, conf.Webserver.LogFormat)

	//	if port was not set via the command line
	if port == "" {
		//	do we have a port in our config file
		if conf.Webserver.Port != "" {
			port = conf.Webserver.Port
		}
	}

	//	start our webserver
	server.Start(port)
}

//	parseConfig handles loading a config file locally or remote over http(s)
func loadConfig(confLocation string) (Config, error) {
	var err error
	var conf Config
	var reader io.Reader

	//	check for http prefix
	if strings.HasPrefix(confLocation, "http") {
		log.Printf("Loading remote config (%v)", confLocation)

		//	setup http client with a timeout
		var httpClient = &http.Client{
			Timeout: time.Second * 10,
		}

		//	make the http request
		res, err := httpClient.Get(confLocation)
		if err != nil {
			return conf, fmt.Errorf("error fetching remote config file (%v): %v ", confLocation, err)
		}

		//	set the reader to the response body
		reader = res.Body
	} else {
		log.Printf("Loading local config (%v)", confLocation)

		//	check the conf file exists
		if _, err := os.Stat(confLocation); os.IsNotExist(err) {
			return conf, fmt.Errorf("config file at location (%v) not found!", confLocation)
		}

		//	open the confi file
		reader, err = os.Open(confLocation)
		if err != nil {
			return conf, fmt.Errorf("error opening local config file (%v): %v ", confLocation, err)
		}
	}

	//	decode conf file
	if _, err := toml.DecodeReader(reader, &conf); err != nil {
		return conf, err
	}

	return conf, nil
}

//	initMaps registers maps with our server
func initMaps(maps []Map, providers map[string]mvt.Provider) error {

	//	iterate our maps
	for _, m := range maps {
		var layers []server.Layer
		//	iterate our layers
		for _, l := range m.Layers {
			//	split our provider name (provider.layer) into [provider,layer]
			providerLayer := strings.Split(l.ProviderLayer, ".")

			//	we're expecting two params in the provider layer definition
			if len(providerLayer) != 2 {
				return fmt.Errorf("invalid provider layer (%v) for map (%v)", l.ProviderLayer, m)
			}

			//	lookup our proivder
			provider, ok := providers[providerLayer[0]]
			if !ok {
				return fmt.Errorf("provider (%v) not defined", providerLayer[0])
			}
{'fraction_non_alphanumeric': 0.12865835989568242, 'fraction_numerical': 0.0017386264850767893, 'mean_word_length': 3.3306148055207028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4581004', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2339, 'n_words': 693}