<commit_before>/*
Copyright © 2014–5 <NAME>.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/backerman/evego"
	"github.com/backerman/evego/pkg/cache"
	"github.com/backerman/evego/pkg/dbaccess"
	"github.com/backerman/evego/pkg/eveapi"
	"github.com/backerman/evego/pkg/evesso"
	"github.com/backerman/evego/pkg/market"
	"github.com/backerman/evego/pkg/routing"
	"github.com/backerman/eveindy/pkg/api"
	"github.com/backerman/eveindy/pkg/db"
	"github.com/backerman/eveindy/pkg/server"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/zenazn/goji"
	// Register SQLite3 and PgSQL drivers
	_ "github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
)

var c config
var rootCmd *cobra.Command

type config struct {
	Dev                      bool
	DbDriver, DbPath         string
	Bind                     string
	XMLAPIEndpoint           string
	Router                   string
	Cache                    string
	RedisHost, RedisPassword string
	CookieDomain, CookiePath string
	ClientID, ClientSecret   string
	RedirectURL              string
}

func setRoutes(sde evego.Database, localdb db.LocalDB, xmlAPI evego.XMLAPI,
	eveCentral evego.Market, sessionizer server.Sessionizer, cache evego.Cache) {
	assets := http.FileServer(http.Dir("dist"))
	bower := http.FileServer(http.Dir("bower_components"))
	if c.Dev {
		goji.Get("/bower_components/*", http.StripPrefix("/bower_components/", bower))
	}
	goji.Get("/autocomplete/system/:name", api.AutocompleteSystems(sde))
	goji.Get("/autocomplete/station/:name", api.AutocompleteStations(sde, xmlAPI))
	goji.Post("/pastebin", api.ParseItems(sde))
	marketHandler := api.ItemsMarketValue(sde, eveCentral, xmlAPI)
	// For now these do the same thing. That may change.
	goji.Post("/market/region/:location", marketHandler)
	goji.Post("/market/system/:location", marketHandler)
	goji.Post("/market/station/:id", marketHandler)
	goji.Get("/market/jita", api.ReprocessOutputValues(sde, eveCentral, xmlAPI, cache))

	goji.Post("/reprocess", api.ReprocessItems(sde))
	// SSO!
	auth := evesso.MakeAuthenticator(evesso.Endpoint, c.ClientID, c.ClientSecret,
		c.RedirectURL, evesso.PublicData)
	goji.Get("/crestcallback", api.CRESTCallbackListener(localdb, auth, sessionizer))
	goji.Get("/authenticate", api.AuthenticateHandler(auth, sessionizer))
	goji.Get("/session", api.SessionInfo(auth, sessionizer, localdb))
	goji.Post("/logout", api.LogoutHandler(localdb, auth, sessionizer))

	// API keys
	listHandler, deleteHander, addHandler, refreshHandler := api.XMLAPIKeysHandlers(localdb, sessionizer)
	goji.Get("/apikeys/list", listHandler)
	goji.Post("/apikeys/delete/:keyid", deleteHander)
	goji.Post("/apikeys/add", addHandler)
	goji.Post("/apikeys/refresh", refreshHandler)

	// Standings and skills
	goji.Get("/standings/:charID/:npcCorpID", api.StandingsHandler(localdb, sessionizer))
	goji.Get("/skills/:charID/group/:skillGroupID", api.SkillsHandler(localdb, sessionizer))

	// Static assets
	goji.Get("/*", assets)
}

func mainCommand(cmd *cobra.Command, args []string) {
	err := viper.Unmarshal(&c)
	if err != nil {
		log.Fatalf("Unable to marshal configuration: %v", err)
	}
	if !viper.IsSet("dbpath") {
		log.Fatalf("Please set the dbpath configuration option or EVEINDY_DBPATH " +
			"environment variable to the database's path.")
	}

	if !(viper.IsSet("CookieDomain") && viper.IsSet("CookiePath")) {
		log.Fatalf("Please set the CookieDomain and CookiePath configuration options.")
	}

	if !(viper.IsSet("ClientID") && viper.IsSet("ClientSecret") &&
		viper.IsSet("RedirectURL")) {
		log.Fatalf("Please set the ClientID, ClientSecret, and RedirectURL configuration " +
			"options as registered with CCP.")
	}

	if c.Dev {
		log.Printf("Configuration is: %+v", c)
	}

	sde := dbaccess.SQLDatabase(c.DbDriver, c.DbPath)
	// if c.Dev {
	// 	ts := httptest.NewServer(
	// 		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// 			respFile, _ := os.Open("./conquerable-stations.xml")
	// 			defer respFile.Close()
	// 			responseBytes, _ := ioutil.ReadAll(respFile)
	// 			responseBuf := bytes.NewBuffer(responseBytes)
	// 			responseBuf.WriteTo(w)
	// 		}))
	// 	c.XMLAPIEndpoint = ts.URL
	// }
	var myCache evego.Cache
	switch c.Cache {
	case "inproc":
		myCache = server.InMemCache()
	case "redis":
		if c.RedisPassword != "" {
			myCache = cache.RedisCache(c.RedisHost, c.RedisPassword)
		} else {<commit_msg>Remove some commented-out code; remove SQLite driver<commit_after>/*
Copyright © 2014–5 <NAME>.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/backerman/evego"
	"github.com/backerman/evego/pkg/cache"
	"github.com/backerman/evego/pkg/dbaccess"
	"github.com/backerman/evego/pkg/eveapi"
	"github.com/backerman/evego/pkg/evesso"
	"github.com/backerman/evego/pkg/market"
	"github.com/backerman/evego/pkg/routing"
	"github.com/backerman/eveindy/pkg/api"
	"github.com/backerman/eveindy/pkg/db"
	"github.com/backerman/eveindy/pkg/server"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/zenazn/goji"
	// Register PgSQL driver
	_ "github.com/lib/pq"
)

var c config
var rootCmd *cobra.Command

type config struct {
	Dev                      bool
	DbDriver, DbPath         string
	Bind                     string
	XMLAPIEndpoint           string
	Router                   string
	Cache                    string
	RedisHost, RedisPassword string
	CookieDomain, CookiePath string
	ClientID, ClientSecret   string
	RedirectURL              string
}

func setRoutes(sde evego.Database, localdb db.LocalDB, xmlAPI evego.XMLAPI,
	eveCentral evego.Market, sessionizer server.Sessionizer, cache evego.Cache) {
	assets := http.FileServer(http.Dir("dist"))
	bower := http.FileServer(http.Dir("bower_components"))
	if c.Dev {
		goji.Get("/bower_components/*", http.StripPrefix("/bower_components/", bower))
	}
	goji.Get("/autocomplete/system/:name", api.AutocompleteSystems(sde))
	goji.Get("/autocomplete/station/:name", api.AutocompleteStations(sde, xmlAPI))
	goji.Post("/pastebin", api.ParseItems(sde))
	marketHandler := api.ItemsMarketValue(sde, eveCentral, xmlAPI)
	// For now these do the same thing. That may change.
	goji.Post("/market/region/:location", marketHandler)
	goji.Post("/market/system/:location", marketHandler)
	goji.Post("/market/station/:id", marketHandler)
	goji.Get("/market/jita", api.ReprocessOutputValues(sde, eveCentral, xmlAPI, cache))

	goji.Post("/reprocess", api.ReprocessItems(sde))
	// SSO!
	auth := evesso.MakeAuthenticator(evesso.Endpoint, c.ClientID, c.ClientSecret,
		c.RedirectURL, evesso.PublicData)
	goji.Get("/crestcallback", api.CRESTCallbackListener(localdb, auth, sessionizer))
	goji.Get("/authenticate", api.AuthenticateHandler(auth, sessionizer))
	goji.Get("/session", api.SessionInfo(auth, sessionizer, localdb))
	goji.Post("/logout", api.LogoutHandler(localdb, auth, sessionizer))

	// API keys
	listHandler, deleteHander, addHandler, refreshHandler := api.XMLAPIKeysHandlers(localdb, sessionizer)
	goji.Get("/apikeys/list", listHandler)
	goji.Post("/apikeys/delete/:keyid", deleteHander)
	goji.Post("/apikeys/add", addHandler)
	goji.Post("/apikeys/refresh", refreshHandler)

	// Standings and skills
	goji.Get("/standings/:charID/:npcCorpID", api.StandingsHandler(localdb, sessionizer))
	goji.Get("/skills/:charID/group/:skillGroupID", api.SkillsHandler(localdb, sessionizer))

	// Static assets
	goji.Get("/*", assets)
}

func mainCommand(cmd *cobra.Command, args []string) {
	err := viper.Unmarshal(&c)
	if err != nil {
		log.Fatalf("Unable to marshal configuration: %v", err)
	}
	if !viper.IsSet("dbpath") {
		log.Fatalf("Please set the dbpath configuration option or EVEINDY_DBPATH " +
			"environment variable to the database's path.")
	}

	if !(viper.IsSet("CookieDomain") && viper.IsSet("CookiePath")) {
		log.Fatalf("Please set the CookieDomain and CookiePath configuration options.")
	}

	if !(viper.IsSet("ClientID") && viper.IsSet("ClientSecret") &&
		viper.IsSet("RedirectURL")) {
		log.Fatalf("Please set the ClientID, ClientSecret, and RedirectURL configuration " +
			"options as registered with CCP.")
	}

	if c.Dev {
		log.Printf("Configuration is: %+v", c)
	}

	sde := dbaccess.SQLDatabase(c.DbDriver, c.DbPath)
	var myCache evego.Cache
	switch c.Cache {
	case "inproc":
		myCache = server.InMemCache()
	case "redis":
		if c.RedisPassword != "" {
			myCache = cache.RedisCache(c.RedisHost, c.RedisPassword)
		} else {
{'fraction_non_alphanumeric': 0.1363588278853274, 'fraction_numerical': 0.0029620226383158784, 'mean_word_length': 4.26391982182628, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 9}, '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': '7207492', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3236, 'n_words': 694}