use json config and enable arguments

This commit is contained in:
gempir 2018-11-29 22:20:57 +01:00
parent c7e61f33a1
commit 48aeb14a85
15 changed files with 100 additions and 75 deletions

View File

@ -1,4 +0,0 @@
ADMIN=gempir
IRCUSER=gempbot
IRCTOKEN=oauth:123123123
CHANNELS=gempir,pajlada

View File

@ -1,11 +0,0 @@
FROM golang:latest
WORKDIR /go/src/github.com/gempir/gempbotgo
COPY . .
RUN go get ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=0 /go/src/github.com/gempir/gempbotgo/app .
CMD ["./app"]
EXPOSE 8025

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017 gempir
Copyright (c) 2018 gempir
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -1,9 +1,11 @@
---
api_domain: api2.gempir.com
api_domain: justlog.gempir.com
email: !vault |
$ANSIBLE_VAULT;1.1;AES256
65373739353434323038366266343639376262633166356330336339396664326264343334633864
6164633261343062623666613536343265663038343330360a313637663739343464396133366565
35666538386231343533333231646131383734663834663039636235313237636366343535376366
3438353430666537630a323338663565366439353163646435633738653563393330613064363163
66356363623063373532626262343361663933623132653162633034396131623038
66356363623063373532626262343361663933623132653162633034396131623038
admin: gempir
channels: "gempir,pajlada"

View File

@ -5,9 +5,6 @@
roles:
- {role: 'common', tags: ['common']}
- {role: 'geerlingguy.docker', tags: ['docker']}
- {role: 'traefik', tags: ['traefik']}
- {role: 'portainer', tags: ['portainer']}
- {role: 'bot', tags: ['bot']}
handlers:

View File

@ -1,16 +1,23 @@
---
- name: create directory
- name: create log directory
file:
path: /var/twitch_logs
path: /var/justlog
state: directory
owner: root
group: docker
owner: justlog
group: justlog
mode: 0740
- name: create channels file
- name: create env file
template:
src: templates/channels.j2
dest: /var/logstv_channels
owner: root
group: docker
mode: 0640
src: templates/env.j2
dest: /etc/justlog.env
owner: justlog
group: justlog
mode: 0640
- name: install systemd unit file
template: src=myservice.j2 dest=/etc/systemd/system/myservice.service
- name: reload units
systemd:
daemon_reload: yes

View File

@ -1,5 +0,0 @@
gempir
pajlada
forsen
nymn
nani

View File

@ -0,0 +1,8 @@
{
"admin: "{{ admin }}",
"logDirectory": "/var/justlog"
"channels": [
"gempir",
"pajlada"
]
}

View File

@ -0,0 +1,11 @@
[Unit]
Description=justlog service
After=network.target
[Service]
Type=simple
ExecStart=/home/justlog/justlog
TimeoutStopSec=5
[Install]
WantedBy=multi-user.target

View File

@ -14,3 +14,15 @@
- name: install ufw firewall
include: ufw.yml
- name: Ensure group "justlog" exists
group:
name: justlog
state: present
- name: Create justlog user
user:
name: justlog
shell: /bin/bash
groups: justlog
append: yes

5
config.json Normal file
View File

@ -0,0 +1,5 @@
{
"admin": "gempir",
"logsDirectory": "./logs",
"channels": ["gempir", "pajlada"]
}

View File

@ -1,11 +0,0 @@
version: "2.1"
services:
gempbotgo:
build: ./
restart: always
ports:
- "8025:8025"
env_file:
- .env
volumes:
- ./logs:/var/twitch_logs

View File

@ -13,9 +13,9 @@ type Logger struct {
}
// NewFileLogger create file logger
func NewFileLogger() Logger {
func NewFileLogger(logPath string) Logger {
return Logger{
logPath: "/var/twitch_logs",
logPath: logPath,
}
}

55
main.go
View File

@ -1,8 +1,9 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"time"
@ -13,26 +14,33 @@ import (
"github.com/gempir/gempbotgo/humanize"
"github.com/gempir/go-twitch-irc"
_ "github.com/go-sql-driver/mysql"
log "github.com/sirupsen/logrus"
)
type config struct {
LogsDirectory string `json:"logsDirectory"`
Admin string `json:"admin"`
Channels []string `json:"channels"`
}
var (
admin string
cfg config
)
func main() {
startTime := time.Now()
admin = os.Getenv("ADMIN")
configFile := flag.String("config", "config.json", "json config file")
flag.Parse()
cfg = loadConfiguration(*configFile)
apiServer := api.NewServer()
go apiServer.Init()
twitchClient := twitch.NewClient(os.Getenv("IRCUSER"), os.Getenv("IRCTOKEN"))
twitchClient := twitch.NewClient("justinfan123123", "oauth:123123123")
fileLogger := filelog.NewFileLogger(cfg.LogsDirectory)
fileLogger := filelog.NewFileLogger()
channels := strings.Split(os.Getenv("CHANNELS"), ",")
for _, channel := range channels {
for _, channel := range cfg.Channels {
fmt.Println("Joining " + channel)
twitchClient.Join(channel)
apiServer.AddChannel(channel)
@ -43,20 +51,20 @@ func main() {
go func() {
err := fileLogger.LogMessageForUser(channel, user, message)
if err != nil {
log.Println(err.Error())
log.Error(err.Error())
}
}()
go func() {
err := fileLogger.LogMessageForChannel(channel, user, message)
if err != nil {
log.Println(err.Error())
log.Error(err.Error())
}
}()
if user.Username == admin && strings.HasPrefix(message.Text, "!status") {
if user.Username == cfg.Admin && strings.HasPrefix(message.Text, "!status") {
uptime := humanize.TimeSince(startTime)
twitchClient.Say(channel, admin+", uptime: "+uptime)
twitchClient.Say(channel, cfg.Admin+", uptime: "+uptime)
}
})
@ -65,17 +73,34 @@ func main() {
go func() {
err := fileLogger.LogMessageForUser(channel, user, message)
if err != nil {
log.Println(err.Error())
log.Error(err.Error())
}
}()
go func() {
err := fileLogger.LogMessageForChannel(channel, user, message)
if err != nil {
log.Println(err.Error())
log.Error(err.Error())
}
}()
})
twitchClient.Connect()
}
func loadConfiguration(file string) config {
log.Info("Loading config from " + file)
var cfg config
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
log.Fatal(err)
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&cfg)
cfg.LogsDirectory = strings.TrimSuffix(cfg.LogsDirectory, "/")
return cfg
}

View File

@ -1,11 +0,0 @@
version: "3"
services:
gempbotgo:
image: gempir/gempbotgo
restart: always
ports:
- "8025:8025"
env_file:
- .env
volumes:
- /var/twitch_logs:/var/twitch_logs