Skip to content

Commit

Permalink
Initial commit with the current production version
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmarton committed Jan 2, 2021
1 parent d29da6d commit 683b8da
Show file tree
Hide file tree
Showing 34 changed files with 2,034 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.idea/

.DS_Store

config.json

hextechdocs-be
58 changes: 58 additions & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Package configuration implements the configuration logic for the application.
It has 2 provider options: File and Consul
File simply loads the config.json from next to the binary. If it doesn't exist, it'll create one based on the example struct.
Consul loads everything from the dynamic configuration service. If it doesn't exist, it'll attempt to save the default config.
The contents of the configuration is not validated so you're expected to fill out everything you require.
*/
package configuration

type Configuration struct {
DatabaseHost string `json:"DatabaseHost"`
DatabasePort int `json:"DatabasePort"`
DatabaseUser string `json:"DatabaseUser"`
DatabasePassword string `json:"DatabasePassword"`
DatabaseName string `json:"DatabaseName"`

ShouldUseRedis bool `json:"ShouldUseRedis"`
RedisUrl string `json:"RedisUrl"`
ShouldUseRedisAuth bool `json:"ShouldUseRedisAuth"`
RedisPassword string `json:"RedisPassword"`
ShouldUseRedisDatabase bool `json:"ShouldUseRedisDatabase"`
RedisDatabase string `json:"RedisDatabase"`

ShouldLogToElastic bool `json:"ShouldLogToElastic"`
ElasticHosts []string `json:"ElasticHosts"`
ShouldUseElasticAuth bool `json:"ShouldUseElasticAuth"`
ElasticUsername string `json:"ElasticUsername"`
ElasticPassword string `json:"ElasticPassword"`
ElasticIndex string `json:"ElasticIndex"`
}

// exampleConfig returns a configuration with everything filled in with placeholder values for later customization.
func exampleConfig() *Configuration {
return &Configuration{
DatabaseHost: "dbhost.local",
DatabasePort: 5432,
DatabaseUser: "root",
DatabasePassword: "changeme",
DatabaseName: "hextechdocs",

ShouldUseRedis: true,
RedisUrl: "redishost.local:6379",
ShouldUseRedisAuth: true,
RedisPassword: "changeme",
ShouldUseRedisDatabase: true,
RedisDatabase: "15",

ShouldLogToElastic: false,
ElasticHosts: []string{"elastic.local:9300"},
ShouldUseElasticAuth: false,
ElasticUsername: "admin",
ElasticPassword: "supersecret",
ElasticIndex: "hexgo",
}
}
43 changes: 43 additions & 0 deletions configuration/consul.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package configuration

import (
"encoding/json"
"errors"
consul "github.com/hashicorp/consul/api"
"hextechdocs-be/logger"
"os"
)

// initConfiguration loads in the configuration from Consul (or saves the example config if there isn't one)
func initConfiguration() (*Configuration, error) {
consulClient, err := consul.NewClient(consul.DefaultConfig())
if err != nil {
return nil, err
}

pair, _, err := consulClient.KV().Get(os.Getenv("CONSUL_KV_PATH"), nil)
if err != nil {
return nil, err
}

if pair == nil {
logger.HexLogger.Warn("Configuration doesn't exist on the consul instance!")
logger.HexLogger.Warn("Saving a template and terminating...")

data, _ := json.Marshal(exampleConfig())
_, _ = consulClient.KV().Put(&consul.KVPair{
Key: os.Getenv("CONSUL_KV_PATH"),
Value: data,
}, nil)

os.Exit(1)
return nil, errors.New("unable to find configuration")
}

var config Configuration
if err := json.Unmarshal(pair.Value, &config); err != nil {
return nil, err
}

return &config, nil
}
35 changes: 35 additions & 0 deletions configuration/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package configuration

import (
"encoding/json"
"errors"
"hextechdocs-be/logger"
"io/ioutil"
"os"
)

// initFileConfiguration loads in the configuration from the filesystem (or saves the example config if there isn't one)
func initFileConfiguration() (*Configuration, error) {
if _, err := os.Stat("config.json"); err == nil {
contents, err := ioutil.ReadFile("config.json")
if err != nil {
return nil, err
}

var configuration Configuration
if err := json.Unmarshal(contents, &configuration); err != nil {
return nil, err
}

return &configuration, nil
}

logger.HexLogger.Warn("Configuration file doesn't exist but the use of consul is disabled!")
logger.HexLogger.Warn("Saving a template file and terminating...")

configContents, _ := json.MarshalIndent(exampleConfig(), "", " ")
_ = ioutil.WriteFile("config.json", configContents, 0644)

os.Exit(1)
return nil, errors.New("unable to find configuration")
}
19 changes: 19 additions & 0 deletions configuration/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package configuration

import (
"github.com/sirupsen/logrus"
"hextechdocs-be/logger"
"os"
)

// InitializeConfiguration calls the appropriate internal function based on the presence of an environment variable
func InitializeConfiguration() (*Configuration, error) {
shouldUseAnuEco := os.Getenv("ANU_DISABLE") == ""
logger.HexLogger.WithFields(logrus.Fields{"shouldUseAnuEco": shouldUseAnuEco}).Trace("shouldUseAnuEco: ")

if !shouldUseAnuEco {
return initFileConfiguration()
} else {
return initConfiguration()
}
}
25 changes: 25 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Package flags is a simple utility package for every command line argument the software supports
package flags

import (
"flag"
"github.com/sirupsen/logrus"
"hextechdocs-be/logger"
)

// Importer controls whether or not the software should launch in web or importer mode
var Importer bool
// ServerPort overrides the default port of 8080 (can be overwritten by the NOMAD_HOST_ADDR_http environment variable)
var ServerPort int

// InitFlags parses the command line arguments and fills in the data we might need
func InitFlags() {
flag.BoolVar(&Importer, "importer", false, "Should the software launch in importer mode")
flag.IntVar(&ServerPort, "port", 8080, "The port the webserver should bind to")
flag.Parse()

logger.HexLogger.WithFields(logrus.Fields{
"importer": Importer,
"serverPort": ServerPort,
}).Trace("Flags have been parsed")
}
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module hextechdocs-be

go 1.14

require (
gitea.com/xorm/xorm-redis-cache v0.2.0
github.com/garyburd/redigo v1.6.0
github.com/google/uuid v1.1.2
github.com/graphql-go/graphql v0.7.9
github.com/hashicorp/consul/api v1.8.1
github.com/lib/pq v1.7.0
github.com/olivere/elastic/v7 v7.0.22
github.com/sirupsen/logrus v1.4.2
gopkg.in/sohlich/elogrus.v7 v7.0.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
xorm.io/xorm v1.0.3
)
Loading

0 comments on commit 683b8da

Please sign in to comment.