start implementation of database package

This commit is contained in:
Herman Schaaf
2019-07-28 14:35:28 +01:00
parent e0bb7b2ace
commit 05556daf26
3 changed files with 104 additions and 26 deletions

2
.gitignore vendored
View File

@@ -28,3 +28,5 @@ _testmain.go
_repos
goreportcard.db
var

91
database/db.go Normal file
View File

@@ -0,0 +1,91 @@
package database
import (
"github.com/go-redis/redis"
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/ledis"
)
type Database interface {
Get(string) (string, error)
Set(string, string) error
Close() error
}
// GetConnection connects to the Redis/Ledis database (or creates it if it does not exist, in the case of Ledis),
// and creates a space for saving the repos, also only if it does not exist.
func GetConnection(redisHost string) (Database, error) {
if redisHost == "" {
return newLedisDatabase()
}
return newRedisDatabase(redisHost)
}
type ledisDatabase struct {
connection *ledis.DB
}
func (l *ledisDatabase) Get(k string) (string, error) {
b, _ := l.connection.Get([]byte(k))
return string(b), nil
}
func (l *ledisDatabase) Set(k, v string) error {
return l.connection.Set([]byte(k), []byte(v))
}
func (l *ledisDatabase) Close() error {
return nil
}
func newLedisDatabase() (*ledisDatabase, error) {
db, err := connectLedis()
if err != nil {
return nil, err
}
return &ledisDatabase{connection: db}, nil
}
func connectLedis() (*ledis.DB, error) {
// connect to ledis
l, err := ledis.Open(config.NewConfigDefault())
if err != nil {
return nil, err
}
db, err := l.Select(0)
if err != nil {
return nil, err
}
return db, nil
}
type redisDatabase struct {
connection *redis.Client
}
func newRedisDatabase(redisHost string) (*redisDatabase, error) {
db := connectRedis(redisHost)
err := db.Echo("test").Err()
return &redisDatabase{connection: db}, err
}
func (r *redisDatabase) Get(k string) (string, error) {
return r.connection.Get(k).Result()
}
func (r *redisDatabase) Set(k, v string) error {
return r.connection.Set(k, v, 0).Err()
}
func (r *redisDatabase) Close() error {
return r.connection.Close()
}
func connectRedis(redisHost string) *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: redisHost,
Password: "", // no password set
DB: 0, // use default DB
})
return client
}

37
main.go
View File

@@ -9,15 +9,17 @@ import (
"regexp"
"time"
"github.com/gojp/goreportcard/database"
"github.com/gojp/goreportcard/handlers"
"github.com/boltdb/bolt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
addr = flag.String("http", ":8000", "HTTP listen address")
addr string
redisHost string
)
func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
@@ -57,26 +59,6 @@ func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string
}
}
// initDB opens the bolt database file (or creates it if it does not exist), and creates
// a bucket for saving the repos, also only if it does not exist.
func initDB() error {
db, err := bolt.Open(handlers.DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return err
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(handlers.RepoBucket))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(handlers.MetaBucket))
return err
})
return err
}
// metrics provides functionality for monitoring the application status
type metrics struct {
responseTimes *prometheus.SummaryVec
@@ -114,14 +96,17 @@ func (m metrics) instrument(path string, h http.HandlerFunc) (string, http.Handl
}
func main() {
flag.StringVar(&addr, "http", ":8000", "HTTP listen address")
flag.StringVar(&redisHost, "redis", "", "Address of Redis server")
flag.Parse()
if err := os.MkdirAll("_repos/src/github.com", 0755); err != nil && !os.IsExist(err) {
log.Fatal("ERROR: could not create repos dir: ", err)
}
// initialize database
if err := initDB(); err != nil {
log.Fatal("ERROR: could not open bolt db: ", err)
_, err := database.GetConnection(redisHost)
if err != nil {
log.Fatal("ERROR: could not connect to db: ", err)
}
m := setupMetrics()
@@ -137,6 +122,6 @@ func main() {
http.Handle("/metrics", promhttp.Handler())
log.Printf("Running on %s ...", *addr)
log.Fatal(http.ListenAndServe(*addr, nil))
log.Printf("Running on %s ...", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}