Split Cache and HTTP build code into other files
This commit is contained in:
parent
8e5320481a
commit
b1113dff25
3 changed files with 271 additions and 192 deletions
90
internals/cache.go
Normal file
90
internals/cache.go
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
package internals
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CacheConn -
|
||||||
|
type CacheConn struct {
|
||||||
|
CacheDB *leveldb.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCache -
|
||||||
|
func (c *CacheConn) SetCache(cacheDB *leveldb.DB) {
|
||||||
|
c.CacheDB = cacheDB
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCachedDataIntSub -
|
||||||
|
func (c *CacheConn) GetCachedDataIntSub(key int32, subkey string) int32 {
|
||||||
|
return c.GetCachedDataInt(fmt.Sprintf("%d.%s", key, subkey))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCachedDataSub -
|
||||||
|
func (c *CacheConn) GetCachedDataSub(key int32, subkey string) string {
|
||||||
|
return c.GetCachedData(fmt.Sprintf("%d.%s", key, subkey))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCachedDataIntSub64 -
|
||||||
|
func (c *CacheConn) GetCachedDataIntSub64(key int64, subkey string) int32 {
|
||||||
|
return c.GetCachedDataInt(fmt.Sprintf("%d.%s", key, subkey))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCachedDataSub64 -
|
||||||
|
func (c *CacheConn) GetCachedDataSub64(key int64, subkey string) string {
|
||||||
|
return c.GetCachedData(fmt.Sprintf("%d.%s", key, subkey))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCachedDataInt -
|
||||||
|
func (c *CacheConn) GetCachedDataInt(key string) int32 {
|
||||||
|
strVal := c.GetCachedData(key)
|
||||||
|
|
||||||
|
intVal, convErr := strconv.ParseInt(strVal, 10, 32)
|
||||||
|
if convErr != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return int32(intVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCachedData -
|
||||||
|
func (c *CacheConn) GetCachedData(key string) string {
|
||||||
|
|
||||||
|
response, err := c.CacheDB.Get([]byte(key), nil)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(response[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutCacheDataIntSub -
|
||||||
|
func (c *CacheConn) PutCacheDataIntSub(key int32, subkey string, value int32) {
|
||||||
|
c.PutCacheDataSub(key, subkey, fmt.Sprintf("%d", value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutCacheDataSub -
|
||||||
|
func (c *CacheConn) PutCacheDataSub(key int32, subkey string, value string) {
|
||||||
|
c.PutCacheData(fmt.Sprintf("%d.%s", key, subkey), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutCacheDataIntSub64 -
|
||||||
|
func (c *CacheConn) PutCacheDataIntSub64(key int64, subkey string, value int32) {
|
||||||
|
c.PutCacheDataSub64(key, subkey, fmt.Sprintf("%d", value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutCacheDataSub64 -
|
||||||
|
func (c *CacheConn) PutCacheDataSub64(key int64, subkey string, value string) {
|
||||||
|
c.PutCacheData(fmt.Sprintf("%d.%s", key, subkey), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutCacheData -
|
||||||
|
func (c *CacheConn) PutCacheData(key string, value string) {
|
||||||
|
err := c.CacheDB.Put([]byte(key), []byte(value), nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
121
internals/httpclient.go
Normal file
121
internals/httpclient.go
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
package internals
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gregjones/httpcache"
|
||||||
|
"github.com/gregjones/httpcache/leveldbcache"
|
||||||
|
loghttp "github.com/motemen/go-loghttp"
|
||||||
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
googleOauthConfig = &oauth2.Config{
|
||||||
|
RedirectURL: "http://localhost:3000/callback",
|
||||||
|
ClientID: "CLIENTKEY",
|
||||||
|
ClientSecret: "SECRETKEY",
|
||||||
|
Scopes: []string{
|
||||||
|
"esi-skills.read_skillqueue.v1",
|
||||||
|
"esi-skills.read_skills.v1",
|
||||||
|
"esi-planets.manage_planets.v1",
|
||||||
|
"esi-wallet.read_character_wallet.v1",
|
||||||
|
"esi-location.read_location.v1",
|
||||||
|
"esi-clones.read_clones.v1",
|
||||||
|
"esi-universe.read_structures.v1",
|
||||||
|
},
|
||||||
|
Endpoint: oauth2.Endpoint{
|
||||||
|
AuthURL: "https://login.eveonline.com/oauth/authorize/",
|
||||||
|
TokenURL: "https://login.eveonline.com/oauth/token/",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// Some random string, random for each request
|
||||||
|
oauthStateString = "random"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetTokenURL -
|
||||||
|
func GetTokenURL() string {
|
||||||
|
url := googleOauthConfig.AuthCodeURL(oauthStateString, oauth2.AccessTypeOffline)
|
||||||
|
|
||||||
|
// https://eveonline-third-party-documentation.readthedocs.io/en/latest/sso/authentication.html
|
||||||
|
// response_type: Must be set to “code”.
|
||||||
|
url = url + "&response_type=code"
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTemporaryClient -
|
||||||
|
func GetTemporaryClient(r *http.Request) (*http.Client, *oauth2.Token, error) {
|
||||||
|
state := r.FormValue("state")
|
||||||
|
if state != oauthStateString {
|
||||||
|
errorTxt := fmt.Sprintf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
|
||||||
|
return nil, nil, errors.New(errorTxt)
|
||||||
|
}
|
||||||
|
|
||||||
|
code := r.FormValue("code")
|
||||||
|
token, err := googleOauthConfig.Exchange(oauth2.NoContext, code)
|
||||||
|
if err != nil {
|
||||||
|
errorTxt := fmt.Sprintf("Code exchange failed with '%s'\n", err)
|
||||||
|
return nil, nil, errors.New(errorTxt)
|
||||||
|
}
|
||||||
|
client := googleOauthConfig.Client(oauth2.NoContext, token)
|
||||||
|
|
||||||
|
return client, token, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDefaultClient - Returns the default client based on the given configuration
|
||||||
|
func GetDefaultClient(cacheDB *leveldb.DB, config *HTTPConfiguration) *http.Client {
|
||||||
|
|
||||||
|
ldb := leveldbcache.NewWithDB(cacheDB)
|
||||||
|
cachingTransport := httpcache.NewTransport(ldb)
|
||||||
|
|
||||||
|
if config.InsecureCalls {
|
||||||
|
insecureTransport := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
}
|
||||||
|
cachingTransport.Transport = insecureTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.LogCalls {
|
||||||
|
var CustomLogResponse = func(resp *http.Response) {
|
||||||
|
log.Printf("<--- HTTP %d.%d %d %s (expires on %s) %s",
|
||||||
|
resp.ProtoMajor, resp.ProtoMinor,
|
||||||
|
resp.StatusCode,
|
||||||
|
resp.Request.URL,
|
||||||
|
resp.Header.Get("expires"),
|
||||||
|
resp.Header.Get("content-type"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
loggingTransport := &loghttp.Transport{
|
||||||
|
LogResponse: CustomLogResponse,
|
||||||
|
}
|
||||||
|
loggingTransport.Transport = cachingTransport.Transport
|
||||||
|
cachingTransport.Transport = loggingTransport
|
||||||
|
}
|
||||||
|
|
||||||
|
cachingClient := &http.Client{Transport: cachingTransport}
|
||||||
|
|
||||||
|
ctx := context.WithValue(context.TODO(), oauth2.HTTPClient, cachingClient)
|
||||||
|
|
||||||
|
googleOauthConfig.ClientID = config.ClientID
|
||||||
|
googleOauthConfig.ClientSecret = config.ClientSecret
|
||||||
|
|
||||||
|
client := googleOauthConfig.Client(ctx, config.ConnectionToken)
|
||||||
|
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPConfiguration - Configuration for the HTTP swagger client.
|
||||||
|
type HTTPConfiguration struct {
|
||||||
|
ClientID string
|
||||||
|
ClientSecret string
|
||||||
|
LogCalls bool
|
||||||
|
InsecureCalls bool
|
||||||
|
ConnectionToken *oauth2.Token
|
||||||
|
}
|
252
main.go
252
main.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
@ -17,16 +16,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
"github.com/gregjones/httpcache"
|
|
||||||
"github.com/gregjones/httpcache/leveldbcache"
|
|
||||||
"github.com/leekchan/accounting"
|
"github.com/leekchan/accounting"
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
|
|
||||||
|
InternalUtils "./internals/"
|
||||||
|
|
||||||
ESI "./client"
|
ESI "./client"
|
||||||
|
|
||||||
ESIClones "./client/clones"
|
ESIClones "./client/clones"
|
||||||
|
@ -37,8 +35,6 @@ import (
|
||||||
ESIWallet "./client/wallet"
|
ESIWallet "./client/wallet"
|
||||||
|
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
httptransport "github.com/go-openapi/runtime/client"
|
||||||
|
|
||||||
loghttp "github.com/motemen/go-loghttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Character - Structure to save the verification data.
|
// Character - Structure to save the verification data.
|
||||||
|
@ -54,27 +50,6 @@ type configurationFile struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
googleOauthConfig = &oauth2.Config{
|
|
||||||
RedirectURL: "http://localhost:3000/callback",
|
|
||||||
ClientID: "CLIENTKEY",
|
|
||||||
ClientSecret: "SECRETKEY",
|
|
||||||
Scopes: []string{
|
|
||||||
"esi-skills.read_skillqueue.v1",
|
|
||||||
"esi-skills.read_skills.v1",
|
|
||||||
"esi-planets.manage_planets.v1",
|
|
||||||
"esi-wallet.read_character_wallet.v1",
|
|
||||||
"esi-location.read_location.v1",
|
|
||||||
"esi-clones.read_clones.v1",
|
|
||||||
"esi-universe.read_structures.v1",
|
|
||||||
},
|
|
||||||
Endpoint: oauth2.Endpoint{
|
|
||||||
AuthURL: "https://login.eveonline.com/oauth/authorize/",
|
|
||||||
TokenURL: "https://login.eveonline.com/oauth/token/",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
// Some random string, random for each request
|
|
||||||
oauthStateString = "random"
|
|
||||||
|
|
||||||
defaultDateFormat = "_2 Jan 2006, 15:04"
|
defaultDateFormat = "_2 Jan 2006, 15:04"
|
||||||
|
|
||||||
cfgFilePath = flag.String("config", "configuration.toml", "Path to the configuration file.")
|
cfgFilePath = flag.String("config", "configuration.toml", "Path to the configuration file.")
|
||||||
|
@ -85,7 +60,7 @@ var (
|
||||||
|
|
||||||
var ctx = context.Background()
|
var ctx = context.Background()
|
||||||
var messages = make(chan *oauth2.Token)
|
var messages = make(chan *oauth2.Token)
|
||||||
var cacheDB *leveldb.DB
|
var cache InternalUtils.CacheConn
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -100,13 +75,15 @@ func main() {
|
||||||
log.SetOutput(f)
|
log.SetOutput(f)
|
||||||
|
|
||||||
var cacheDBerr error
|
var cacheDBerr error
|
||||||
cacheDB, cacheDBerr = leveldb.OpenFile(*cacheDBPath, nil)
|
cacheDB, cacheDBerr := leveldb.OpenFile(*cacheDBPath, nil)
|
||||||
if cacheDBerr != nil {
|
if cacheDBerr != nil {
|
||||||
fmt.Println("Unable to initialize the LevelDB cache.")
|
fmt.Println("Unable to initialize the LevelDB cache.")
|
||||||
log.Fatal(cacheDBerr)
|
log.Fatal(cacheDBerr)
|
||||||
}
|
}
|
||||||
|
|
||||||
cErr := readConfigurationFile()
|
cache.SetCache(cacheDB)
|
||||||
|
|
||||||
|
httpConfiguration, cErr := readConfigurationFile()
|
||||||
if cErr != nil {
|
if cErr != nil {
|
||||||
fmt.Println("Missing configuration file.")
|
fmt.Println("Missing configuration file.")
|
||||||
log.Fatal(cErr)
|
log.Fatal(cErr)
|
||||||
|
@ -118,45 +95,17 @@ func main() {
|
||||||
cToken = getNewAuthorizationToken()
|
cToken = getNewAuthorizationToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
ldb := leveldbcache.NewWithDB(cacheDB)
|
httpConfiguration.ConnectionToken = cToken
|
||||||
cachingTransport := httpcache.NewTransport(ldb)
|
|
||||||
|
|
||||||
if *insecureFlag {
|
client := InternalUtils.GetDefaultClient(cacheDB, httpConfiguration)
|
||||||
insecureTransport := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
Proxy: http.ProxyFromEnvironment,
|
|
||||||
}
|
|
||||||
cachingTransport.Transport = insecureTransport
|
|
||||||
}
|
|
||||||
|
|
||||||
if *logCalls {
|
|
||||||
var CustomLogResponse = func(resp *http.Response) {
|
|
||||||
log.Printf("<--- HTTP %d.%d %d %s (expires on %s) %s",
|
|
||||||
resp.ProtoMajor, resp.ProtoMinor,
|
|
||||||
resp.StatusCode,
|
|
||||||
resp.Request.URL,
|
|
||||||
resp.Header.Get("expires"),
|
|
||||||
resp.Header.Get("content-type"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
loggingTransport := &loghttp.Transport{
|
|
||||||
LogResponse: CustomLogResponse,
|
|
||||||
}
|
|
||||||
loggingTransport.Transport = cachingTransport.Transport
|
|
||||||
cachingTransport.Transport = loggingTransport
|
|
||||||
}
|
|
||||||
|
|
||||||
cachingClient := &http.Client{Transport: cachingTransport}
|
|
||||||
|
|
||||||
ctx := context.WithValue(context.TODO(), oauth2.HTTPClient, cachingClient)
|
|
||||||
client := googleOauthConfig.Client(ctx, cToken)
|
|
||||||
|
|
||||||
m, err := getCharacterInfo(client)
|
m, err := getCharacterInfo(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
cToken = getNewAuthorizationToken()
|
cToken = getNewAuthorizationToken()
|
||||||
client = googleOauthConfig.Client(ctx, cToken)
|
httpConfiguration.ConnectionToken = cToken
|
||||||
|
|
||||||
|
client = InternalUtils.GetDefaultClient(cacheDB, httpConfiguration)
|
||||||
m, err = getCharacterInfo(client)
|
m, err = getCharacterInfo(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -198,22 +147,26 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfigurationFile() error {
|
func readConfigurationFile() (*InternalUtils.HTTPConfiguration, error) {
|
||||||
var config configurationFile
|
var config configurationFile
|
||||||
if _, err := toml.DecodeFile(*cfgFilePath, &config); err != nil {
|
if _, err := toml.DecodeFile(*cfgFilePath, &config); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return err
|
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.ClientID == "" || config.ClientSecret == "" {
|
if config.ClientID == "" || config.ClientSecret == "" {
|
||||||
log.Println("Missing ClientID or ClientSecret configuration option in configuration file" + *cfgFilePath + ".")
|
log.Println("Missing ClientID or ClientSecret configuration option in configuration file" + *cfgFilePath + ".")
|
||||||
return errors.New("Missing ClientID or ClientSecret in configuration file " + *cfgFilePath + ".")
|
err := errors.New("Missing ClientID or ClientSecret in configuration file " + *cfgFilePath + ".")
|
||||||
|
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
googleOauthConfig.ClientID = config.ClientID
|
var httpConfig InternalUtils.HTTPConfiguration
|
||||||
googleOauthConfig.ClientSecret = config.ClientSecret
|
httpConfig.ClientID = config.ClientID
|
||||||
|
httpConfig.ClientSecret = config.ClientSecret
|
||||||
|
|
||||||
return nil
|
return &httpConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCharacterInfo(client *http.Client) (*Character, error) {
|
func getCharacterInfo(client *http.Client) (*Character, error) {
|
||||||
|
@ -426,8 +379,8 @@ type structureInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStructureStationInfo(swaggerclient *ESI.App, typeID string, ID int64) (*structureInfo, error) {
|
func getStructureStationInfo(swaggerclient *ESI.App, typeID string, ID int64) (*structureInfo, error) {
|
||||||
locationName := getCachedDataSub64(ID, "name")
|
locationName := cache.GetCachedDataSub64(ID, "name")
|
||||||
locationSystem := getCachedDataIntSub64(ID, "system")
|
locationSystem := cache.GetCachedDataIntSub64(ID, "system")
|
||||||
|
|
||||||
if locationName != "" && locationSystem > 0 {
|
if locationName != "" && locationSystem > 0 {
|
||||||
var sInfo structureInfo
|
var sInfo structureInfo
|
||||||
|
@ -458,8 +411,8 @@ func getStructureStationInfo(swaggerclient *ESI.App, typeID string, ID int64) (*
|
||||||
sInfo.SystemID = *staInfo.SystemID
|
sInfo.SystemID = *staInfo.SystemID
|
||||||
sInfo.System = getSolarSystemInformation(swaggerclient, sInfo.SystemID)
|
sInfo.System = getSolarSystemInformation(swaggerclient, sInfo.SystemID)
|
||||||
|
|
||||||
putCacheDataSub64(sInfo.ID, "name", sInfo.Name)
|
cache.PutCacheDataSub64(sInfo.ID, "name", sInfo.Name)
|
||||||
putCacheDataIntSub64(sInfo.ID, "system", sInfo.SystemID)
|
cache.PutCacheDataIntSub64(sInfo.ID, "system", sInfo.SystemID)
|
||||||
|
|
||||||
return &sInfo, nil
|
return &sInfo, nil
|
||||||
}
|
}
|
||||||
|
@ -483,8 +436,8 @@ func getStructureStationInfo(swaggerclient *ESI.App, typeID string, ID int64) (*
|
||||||
sInfo.SystemID = *strInfo.SolarSystemID
|
sInfo.SystemID = *strInfo.SolarSystemID
|
||||||
sInfo.System = getSolarSystemInformation(swaggerclient, sInfo.SystemID)
|
sInfo.System = getSolarSystemInformation(swaggerclient, sInfo.SystemID)
|
||||||
|
|
||||||
putCacheDataSub64(sInfo.ID, "name", sInfo.Name)
|
cache.PutCacheDataSub64(sInfo.ID, "name", sInfo.Name)
|
||||||
putCacheDataIntSub64(sInfo.ID, "system", sInfo.SystemID)
|
cache.PutCacheDataIntSub64(sInfo.ID, "system", sInfo.SystemID)
|
||||||
|
|
||||||
return &sInfo, nil
|
return &sInfo, nil
|
||||||
}
|
}
|
||||||
|
@ -614,7 +567,7 @@ func getUniverseNames(swaggerclient *ESI.App, itemIds *[]int32) map[int32]string
|
||||||
itemMissingNames := make(map[int32]string)
|
itemMissingNames := make(map[int32]string)
|
||||||
|
|
||||||
for _, itemID := range *itemIds {
|
for _, itemID := range *itemIds {
|
||||||
itemName := getCachedDataSub(itemID, "name")
|
itemName := cache.GetCachedDataSub(itemID, "name")
|
||||||
if itemName != "" {
|
if itemName != "" {
|
||||||
itemNames[itemID] = itemName
|
itemNames[itemID] = itemName
|
||||||
} else {
|
} else {
|
||||||
|
@ -640,7 +593,7 @@ func getUniverseNames(swaggerclient *ESI.App, itemIds *[]int32) map[int32]string
|
||||||
itemName := searchResult.Name
|
itemName := searchResult.Name
|
||||||
itemID := searchResult.ID
|
itemID := searchResult.ID
|
||||||
|
|
||||||
putCacheDataSub(*itemID, "name", *itemName)
|
cache.PutCacheDataSub(*itemID, "name", *itemName)
|
||||||
|
|
||||||
itemNames[*itemID] = *itemName
|
itemNames[*itemID] = *itemName
|
||||||
}
|
}
|
||||||
|
@ -657,8 +610,8 @@ type planetInformation struct {
|
||||||
|
|
||||||
func getPlanetInformation(swaggerclient *ESI.App, planetID int32) planetInformation {
|
func getPlanetInformation(swaggerclient *ESI.App, planetID int32) planetInformation {
|
||||||
|
|
||||||
planetName := getCachedDataSub(planetID, "name")
|
planetName := cache.GetCachedDataSub(planetID, "name")
|
||||||
planetType := getCachedDataIntSub(planetID, "type")
|
planetType := cache.GetCachedDataIntSub(planetID, "type")
|
||||||
|
|
||||||
if planetName == "" {
|
if planetName == "" {
|
||||||
pcallParams := ESIUniverse.NewGetUniversePlanetsPlanetIDParams()
|
pcallParams := ESIUniverse.NewGetUniversePlanetsPlanetIDParams()
|
||||||
|
@ -672,8 +625,8 @@ func getPlanetInformation(swaggerclient *ESI.App, planetID int32) planetInformat
|
||||||
planetInfo.PlanetName = *planetESIInfo.Name
|
planetInfo.PlanetName = *planetESIInfo.Name
|
||||||
planetInfo.PlanetTypeID = *planetESIInfo.TypeID
|
planetInfo.PlanetTypeID = *planetESIInfo.TypeID
|
||||||
|
|
||||||
putCacheDataSub(planetID, "name", planetInfo.PlanetName)
|
cache.PutCacheDataSub(planetID, "name", planetInfo.PlanetName)
|
||||||
putCacheDataIntSub(planetID, "type", planetInfo.PlanetTypeID)
|
cache.PutCacheDataIntSub(planetID, "type", planetInfo.PlanetTypeID)
|
||||||
|
|
||||||
return planetInfo
|
return planetInfo
|
||||||
}
|
}
|
||||||
|
@ -695,11 +648,11 @@ type solarSystemInfo struct {
|
||||||
|
|
||||||
func getSolarSystemInformation(swaggerclient *ESI.App, solarSystemID int32) solarSystemInfo {
|
func getSolarSystemInformation(swaggerclient *ESI.App, solarSystemID int32) solarSystemInfo {
|
||||||
|
|
||||||
systemName := getCachedDataSub(solarSystemID, "name")
|
systemName := cache.GetCachedDataSub(solarSystemID, "name")
|
||||||
systemConstellationID := getCachedDataIntSub(solarSystemID, "constellation")
|
systemConstellationID := cache.GetCachedDataIntSub(solarSystemID, "constellation")
|
||||||
constellationName := getCachedDataSub(systemConstellationID, "name")
|
constellationName := cache.GetCachedDataSub(systemConstellationID, "name")
|
||||||
constellationRegionID := getCachedDataIntSub(systemConstellationID, "region")
|
constellationRegionID := cache.GetCachedDataIntSub(systemConstellationID, "region")
|
||||||
regionName := getCachedDataSub(constellationRegionID, "name")
|
regionName := cache.GetCachedDataSub(constellationRegionID, "name")
|
||||||
|
|
||||||
if systemName == "" || constellationName == "" || regionName == "" {
|
if systemName == "" || constellationName == "" || regionName == "" {
|
||||||
scallParams := ESIUniverse.NewGetUniverseSystemsSystemIDParams()
|
scallParams := ESIUniverse.NewGetUniverseSystemsSystemIDParams()
|
||||||
|
@ -729,13 +682,13 @@ func getSolarSystemInformation(swaggerclient *ESI.App, solarSystemID int32) sola
|
||||||
|
|
||||||
ssInfo.RegionName = *regionESIInfo.Name
|
ssInfo.RegionName = *regionESIInfo.Name
|
||||||
|
|
||||||
putCacheDataSub(solarSystemID, "name", ssInfo.SolarSystemName)
|
cache.PutCacheDataSub(solarSystemID, "name", ssInfo.SolarSystemName)
|
||||||
putCacheDataIntSub(solarSystemID, "constellation", ssInfo.ConstellationID)
|
cache.PutCacheDataIntSub(solarSystemID, "constellation", ssInfo.ConstellationID)
|
||||||
|
|
||||||
putCacheDataSub(ssInfo.ConstellationID, "name", ssInfo.ConstellationName)
|
cache.PutCacheDataSub(ssInfo.ConstellationID, "name", ssInfo.ConstellationName)
|
||||||
putCacheDataIntSub(ssInfo.ConstellationID, "region", ssInfo.RegionID)
|
cache.PutCacheDataIntSub(ssInfo.ConstellationID, "region", ssInfo.RegionID)
|
||||||
|
|
||||||
putCacheDataSub(ssInfo.RegionID, "name", ssInfo.RegionName)
|
cache.PutCacheDataSub(ssInfo.RegionID, "name", ssInfo.RegionName)
|
||||||
|
|
||||||
return ssInfo
|
return ssInfo
|
||||||
}
|
}
|
||||||
|
@ -758,8 +711,8 @@ type SchematicInfo struct {
|
||||||
|
|
||||||
func getSchematicsInformation(swaggerclient *ESI.App, schematicID int32) (*SchematicInfo, error) {
|
func getSchematicsInformation(swaggerclient *ESI.App, schematicID int32) (*SchematicInfo, error) {
|
||||||
|
|
||||||
schematicName := getCachedDataSub(schematicID, "name")
|
schematicName := cache.GetCachedDataSub(schematicID, "name")
|
||||||
schematicCycle := getCachedDataIntSub(schematicID, "cycle")
|
schematicCycle := cache.GetCachedDataIntSub(schematicID, "cycle")
|
||||||
|
|
||||||
if schematicName == "" {
|
if schematicName == "" {
|
||||||
scallParams := ESIPlanetaryInteraction.NewGetUniverseSchematicsSchematicIDParams()
|
scallParams := ESIPlanetaryInteraction.NewGetUniverseSchematicsSchematicIDParams()
|
||||||
|
@ -776,8 +729,8 @@ func getSchematicsInformation(swaggerclient *ESI.App, schematicID int32) (*Schem
|
||||||
schematicInfo.CycleTime = *schematicsESIInfo.CycleTime
|
schematicInfo.CycleTime = *schematicsESIInfo.CycleTime
|
||||||
schematicInfo.SchematicName = *schematicsESIInfo.SchematicName
|
schematicInfo.SchematicName = *schematicsESIInfo.SchematicName
|
||||||
|
|
||||||
putCacheDataSub(schematicID, "name", schematicInfo.SchematicName)
|
cache.PutCacheDataSub(schematicID, "name", schematicInfo.SchematicName)
|
||||||
putCacheDataIntSub(schematicID, "cycle", schematicInfo.CycleTime)
|
cache.PutCacheDataIntSub(schematicID, "cycle", schematicInfo.CycleTime)
|
||||||
|
|
||||||
return &schematicInfo, nil
|
return &schematicInfo, nil
|
||||||
}
|
}
|
||||||
|
@ -801,123 +754,38 @@ func getNewAuthorizationToken() *oauth2.Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLogin(w http.ResponseWriter, r *http.Request) {
|
func handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
url := googleOauthConfig.AuthCodeURL(oauthStateString, oauth2.AccessTypeOffline)
|
url := InternalUtils.GetTokenURL()
|
||||||
|
|
||||||
// https://eveonline-third-party-documentation.readthedocs.io/en/latest/sso/authentication.html
|
|
||||||
// response_type: Must be set to “code”.
|
|
||||||
url = url + "&response_type=code"
|
|
||||||
|
|
||||||
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAuthenticationCallback(w http.ResponseWriter, r *http.Request) {
|
func handleAuthenticationCallback(w http.ResponseWriter, r *http.Request) {
|
||||||
state := r.FormValue("state")
|
client, token, clientErr := InternalUtils.GetTemporaryClient(r)
|
||||||
if state != oauthStateString {
|
if clientErr != nil {
|
||||||
log.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
|
log.Printf("Code exchange failed with '%s'\n", clientErr)
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
// No token to pass, we will get one on the second pass
|
// No token to pass, we will get one on the second pass
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
code := r.FormValue("code")
|
charInfo, charErr := getCharacterInfo(client)
|
||||||
token, err := googleOauthConfig.Exchange(oauth2.NoContext, code)
|
if charErr != nil {
|
||||||
if err != nil {
|
fmt.Printf("Character read error with '%s'\n", charErr)
|
||||||
log.Printf("Code exchange failed with '%s'\n", err)
|
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
||||||
// No token to pass, we will get one on the second pass
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
client := googleOauthConfig.Client(oauth2.NoContext, token)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "https://login.eveonline.com/oauth/verify", nil)
|
|
||||||
response, _ := client.Do(req)
|
|
||||||
|
|
||||||
defer response.Body.Close()
|
|
||||||
contents, _ := ioutil.ReadAll(response.Body)
|
|
||||||
|
|
||||||
var m Character
|
|
||||||
errJSON := json.Unmarshal(contents, &m)
|
|
||||||
if errJSON != nil {
|
|
||||||
fmt.Printf("JSON read error with '%s'\n", errJSON)
|
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "Got token for character %s.\n", m.CharacterName)
|
fmt.Fprintf(w, "Got token for character %s.\n", charInfo.CharacterName)
|
||||||
fmt.Fprintf(w, "You can now close this navigator tab.\n")
|
fmt.Fprintf(w, "You can now close this navigator tab.\n")
|
||||||
|
|
||||||
log.Printf("Refresh token is %s\n", token.RefreshToken)
|
cache.PutCacheData("accessToken", token.AccessToken)
|
||||||
|
cache.PutCacheData("refreshToken", token.RefreshToken)
|
||||||
putCacheData("accessToken", token.AccessToken)
|
|
||||||
putCacheData("refreshToken", token.RefreshToken)
|
|
||||||
|
|
||||||
messages <- token
|
messages <- token
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCachedDataIntSub(key int32, subkey string) int32 {
|
|
||||||
return getCachedDataInt(fmt.Sprintf("%d.%s", key, subkey))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCachedDataSub(key int32, subkey string) string {
|
|
||||||
return getCachedData(fmt.Sprintf("%d.%s", key, subkey))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCachedDataIntSub64(key int64, subkey string) int32 {
|
|
||||||
return getCachedDataInt(fmt.Sprintf("%d.%s", key, subkey))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCachedDataSub64(key int64, subkey string) string {
|
|
||||||
return getCachedData(fmt.Sprintf("%d.%s", key, subkey))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCachedDataInt(key string) int32 {
|
|
||||||
strVal := getCachedData(key)
|
|
||||||
|
|
||||||
intVal, convErr := strconv.ParseInt(strVal, 10, 32)
|
|
||||||
if convErr != nil {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
return int32(intVal)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCachedData(key string) string {
|
|
||||||
|
|
||||||
response, err := cacheDB.Get([]byte(key), nil)
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(response[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func putCacheDataIntSub(key int32, subkey string, value int32) {
|
|
||||||
putCacheDataSub(key, subkey, fmt.Sprintf("%d", value))
|
|
||||||
}
|
|
||||||
|
|
||||||
func putCacheDataSub(key int32, subkey string, value string) {
|
|
||||||
putCacheData(fmt.Sprintf("%d.%s", key, subkey), value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func putCacheDataIntSub64(key int64, subkey string, value int32) {
|
|
||||||
putCacheDataSub64(key, subkey, fmt.Sprintf("%d", value))
|
|
||||||
}
|
|
||||||
|
|
||||||
func putCacheDataSub64(key int64, subkey string, value string) {
|
|
||||||
putCacheData(fmt.Sprintf("%d.%s", key, subkey), value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func putCacheData(key string, value string) {
|
|
||||||
err := cacheDB.Put([]byte(key), []byte(value), nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDatabaseToken() *oauth2.Token {
|
func getDatabaseToken() *oauth2.Token {
|
||||||
refreshToken := getCachedData("refreshToken")
|
refreshToken := cache.GetCachedData("refreshToken")
|
||||||
//accessToken := getCachedData("accessToken")
|
//accessToken := cache.GetCachedData("accessToken")
|
||||||
|
|
||||||
token := new(oauth2.Token)
|
token := new(oauth2.Token)
|
||||||
token.RefreshToken = refreshToken
|
token.RefreshToken = refreshToken
|
||||||
|
|
Reference in a new issue