mysql dep removed just use file system
This commit is contained in:
parent
90011d16ba
commit
e626ae83d4
7 changed files with 30 additions and 83 deletions
|
@ -1,4 +1,3 @@
|
||||||
ADMIN=gempir
|
ADMIN=gempir
|
||||||
IRCUSER=gempbot
|
IRCUSER=gempbot
|
||||||
IRCTOKEN=oauth:123123123
|
IRCTOKEN=oauth:123123123
|
||||||
DSN=root:password@tcp(database:3306)/gempbotgo
|
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -3,4 +3,5 @@ vendor/
|
||||||
.idea/
|
.idea/
|
||||||
coverage-all.out
|
coverage-all.out
|
||||||
coverage.out
|
coverage.out
|
||||||
.env
|
.env
|
||||||
|
logs/
|
|
@ -3,23 +3,9 @@ services:
|
||||||
gempbotgo:
|
gempbotgo:
|
||||||
build: ./
|
build: ./
|
||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
|
||||||
- "database"
|
|
||||||
ports:
|
ports:
|
||||||
- "8025:8025"
|
- "8025:8025"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
volumes:
|
volumes:
|
||||||
- /var/twitch_logs:/var/twitch_logs
|
- ./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"
|
|
||||||
|
|
||||||
|
|
3
main.go
3
main.go
|
@ -31,8 +31,7 @@ func main() {
|
||||||
apiServer := api.NewServer()
|
apiServer := api.NewServer()
|
||||||
go apiServer.Init()
|
go apiServer.Init()
|
||||||
|
|
||||||
time.Sleep(time.Second * 10)
|
store, err := store.NewClient("/var/twitch_logs/channels")
|
||||||
store, err := store.NewClient(os.Getenv("DSN"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
2
prod.yml
2
prod.yml
|
@ -8,4 +8,4 @@ services:
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
volumes:
|
volumes:
|
||||||
- /var/twitch_logs:/var/twitch_logs
|
- /var/twitch_logs:/var/twitch_logs
|
39
schema.sql
39
schema.sql
|
@ -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
|
|
|
@ -1,57 +1,58 @@
|
||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"bufio"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client store client for persisting and reading data
|
// Client store client for persisting and reading data
|
||||||
type Client struct {
|
type Client struct {
|
||||||
db *sql.DB
|
channelsFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient create new store client
|
// NewClient create new store client
|
||||||
func NewClient(dsn string) (*Client, error) {
|
func NewClient(channelsFile string) (*Client, error) {
|
||||||
db, err := sql.Open("mysql", dsn)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
db: db,
|
channelsFile: channelsFile,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllChannels get all channels to
|
// GetAllChannels get all channels to
|
||||||
func (c *Client) GetAllChannels() []string {
|
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 {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
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() {
|
for scanner.Scan() {
|
||||||
var channel string
|
channels = append(channels, scanner.Text())
|
||||||
err = rows.Scan(&channel)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
channels = append(channels, channel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return channels
|
return channels
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddChannel persist channel
|
// AddChannel persist channel
|
||||||
func (c *Client) AddChannel(channel string) {
|
func (c *Client) AddChannel(channel string) error {
|
||||||
stmt, err := c.db.Prepare("INSERT channels SET name=?")
|
f, err := os.OpenFile(c.channelsFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
_, err = stmt.Exec(channel)
|
_, err = f.WriteString(channel + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue