mysql dep removed just use file system

This commit is contained in:
gempir 2018-03-04 20:27:54 +01:00
parent 90011d16ba
commit e626ae83d4
7 changed files with 30 additions and 83 deletions

View file

@ -1,4 +1,3 @@
ADMIN=gempir
IRCUSER=gempbot
IRCTOKEN=oauth:123123123
DSN=root:password@tcp(database:3306)/gempbotgo
IRCTOKEN=oauth:123123123

3
.gitignore vendored
View file

@ -3,4 +3,5 @@ vendor/
.idea/
coverage-all.out
coverage.out
.env
.env
logs/

View file

@ -3,23 +3,9 @@ services:
gempbotgo:
build: ./
restart: always
depends_on:
- "database"
ports:
- "8025:8025"
env_file:
- .env
volumes:
- /var/twitch_logs:/var/twitch_logs
database:
image: mariadb
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
volumes:
- /data:/var/lib/mysql
- ./schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
logging:
driver: "none"
- ./logs:/var/twitch_logs

View file

@ -31,8 +31,7 @@ func main() {
apiServer := api.NewServer()
go apiServer.Init()
time.Sleep(time.Second * 10)
store, err := store.NewClient(os.Getenv("DSN"))
store, err := store.NewClient("/var/twitch_logs/channels")
if err != nil {
log.Fatal(err)
}

View file

@ -8,4 +8,4 @@ services:
env_file:
- .env
volumes:
- /var/twitch_logs:/var/twitch_logs
- /var/twitch_logs:/var/twitch_logs

View file

@ -1,39 +0,0 @@
-- MySQL dump 10.16 Distrib 10.2.10-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: gempbotgo
-- ------------------------------------------------------
-- Server version 10.2.10-MariaDB-10.2.10+maria~jessie-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `channels`
--
DROP TABLE IF EXISTS `channels`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `channels` (
`name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2018-03-02 19:03:03

View file

@ -1,57 +1,58 @@
package store
import (
"database/sql"
"bufio"
"log"
"os"
)
// Client store client for persisting and reading data
type Client struct {
db *sql.DB
channelsFile string
}
// NewClient create new store client
func NewClient(dsn string) (*Client, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
func NewClient(channelsFile string) (*Client, error) {
return &Client{
db: db,
channelsFile: channelsFile,
}, nil
}
// GetAllChannels get all channels to
func (c *Client) GetAllChannels() []string {
rows, err := c.db.Query("SELECT name FROM channels")
channels := []string{}
f, err := os.Open(c.channelsFile)
if err != nil {
log.Println(err.Error())
return []string{}
}
var channels []string
scanner := bufio.NewScanner(f)
if err != nil {
log.Println(err.Error())
return []string{}
}
for rows.Next() {
var channel string
err = rows.Scan(&channel)
if err != nil {
log.Println(err)
}
channels = append(channels, channel)
for scanner.Scan() {
channels = append(channels, scanner.Text())
}
return channels
}
// AddChannel persist channel
func (c *Client) AddChannel(channel string) {
stmt, err := c.db.Prepare("INSERT channels SET name=?")
func (c *Client) AddChannel(channel string) error {
f, err := os.OpenFile(c.channelsFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
log.Println(err.Error())
return err
}
defer f.Close()
_, err = stmt.Exec(channel)
_, err = f.WriteString(channel + "\n")
if err != nil {
log.Println(err.Error())
return err
}
return nil
}