Compare commits

..

No commits in common. "3950f8b672615a2fa78adf2585595f69819e7f03" and "8a9b37cb02824c6d4c99be6d5a5b775b6e14fbfb" have entirely different histories.

9 changed files with 26 additions and 150 deletions

View File

@ -1,14 +0,0 @@
BUILD=`date +%FT%T%z`
LDFLAGS=-ldflags "-X main.buildDate=${BUILD}"
.PHONY: build deps static
build:
go build ${LDFLAGS}
deps:
go get
static:
CGO_ENABLED=0 GOOS=linux go build ${LDFLAGS} -a -installsuffix cgo -o karaokards .

View File

@ -2,8 +2,9 @@ FROM golang@sha256:cee6f4b901543e8e3f20da3a4f7caac6ea643fd5a46201c3c2387183a332d
RUN apk update && apk add --no-cache git make ca-certificates && update-ca-certificates
COPY main.go /go/src/git.martyn.berlin/martyn/karaokards/
COPY internal/ /go/src/git.martyn.berlin/martyn/karaokards/internal/
COPY Makefile /go/src/git.martyn.berlin/martyn/karaokards/
RUN cd /go/src/git.martyn.berlin/martyn/karaokards/; make deps ; make static
RUN cd /go/src/git.martyn.berlin/martyn/karaokards/; go get; CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o karaokards .
#RUN ls /go/src/github.com/karaokards/ -l
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

View File

@ -1,4 +1,3 @@
{
"channels": ["iMartynOnTwitch"],
"externalUrl": "karaokards.ing.martyn.berlin"
"channels": ["iMartynOnTwitch"]
}

View File

@ -1,12 +1,12 @@
apiVersion: v1
data:
config.json: |
{
"channels": ["iMartynOnTwitch"],
"externalUrl": "karaokards.ing.martyn.berlin"
}
strings.json: "{\r\n \"strings\": [\r\n\t \"They're from the North\",\r\n
\ \"Refers to food\"\r\n ]\r\n}\n"
strings.json: "{\r\n \"strings\": [\r\n\t \"Let Pineboy choose!\",\r\n\t \"They're
from the North\",\r\n \"Refers to food\"\r\n ]\r\n}"
kind: ConfigMap
metadata:
name: kardconfig
creationTimestamp: "2020-01-27T20:04:58Z"
name: extracards
namespace: karaokards
resourceVersion: "48537355"
selfLink: /api/v1/namespaces/karaokards/configmaps/extracards
uid: 4b1d73b0-4140-11ea-94d8-9cb6540931b5

View File

@ -24,7 +24,7 @@ spec:
run: kardbot
spec:
containers:
- image: imartyn/karaokardbot:devel
- image: imartyn/karaokardbot:0.0.3-linux-amd64
imagePullPolicy: IfNotPresent
name: kardbot
ports:
@ -39,9 +39,6 @@ spec:
- mountPath: /app/strings.json
name: extracards
subPath: strings.json
- mountPath: /app/config.json
name: config
subPath: config.json
- mountPath: /data
name: data
dnsPolicy: ClusterFirst
@ -59,15 +56,8 @@ spec:
items:
- key: strings.json
path: strings.json
name: kardconfig
name: extracards
name: extracards
- configMap:
defaultMode: 420
items:
- key: config.json
path: config.json
name: kardconfig
name: config
- name: data
persistentVolumeClaim:
claimName: kkard-data

0
deployments/kubernetes/pvc.yaml Normal file → Executable file
View File

View File

@ -3,7 +3,6 @@ package irc
import (
"bufio"
"encoding/json"
"encoding/base64"
"errors"
"fmt"
"io"
@ -16,8 +15,6 @@ import (
"time"
rgb "github.com/foresthoffman/rgblog"
scribble "github.com/nanobox-io/golang-scribble"
uuid "github.com/google/uuid"
)
const PSTFormat = "Jan 2 15:04:05 PST"
@ -41,6 +38,9 @@ type OAuthCred struct {
// The developer application client ID. Used for API calls to Twitch.
ClientID string `json:"client_id,omitempty"`
// List of Channels to join
Channels []string `json:"channels,omitempty"`
}
type KardBot struct {
@ -54,16 +54,6 @@ type KardBot struct {
Server string
startTime time.Time
Prompts []string
Database scribble.Driver
channelData map[string]ChannelData
}
type ChannelData struct {
Name string `json:"name"`
AdminKey string `json:"value,omitempty"`
CustomCommand string `json:"customcommand,omitempty"`
ExtraStrings string `json:"extrastrings,omitempty"`
JoinTime time.Time `json:"jointime"`
}
// Connects the bot to the Twitch IRC server. The bot will continue to try to connect until it
@ -144,7 +134,7 @@ func (bb *KardBot) HandleChat() error {
}
// channel-owner specific commands
if userName == channel {
if userName == bb.Channel {
switch cmd {
case "tbdown":
rgb.CPrintf(
@ -154,14 +144,6 @@ func (bb *KardBot) HandleChat() error {
bb.Disconnect()
return nil
case "wat":
magicCode := bb.readOrCreateChannelKey(channel)
rgb.CPrintf(
"[%s] Magic code is %s - https://karaokards.ing.martyn.berlin/admin/%s/%s\n",
TimeStamp(),
magicCode, userName, magicCode,
)
bb.Say("Ack.")
default:
// do nothing
}
@ -250,24 +232,15 @@ func (bb *KardBot) Start() {
err := bb.ReadCredentials()
if nil != err {
fmt.Println(err)
fmt.Println("Aborting!")
return
}
err = bb.readChannelData()
if nil != err {
fmt.Println(err)
fmt.Println("Aborting!")
fmt.Println("Aborting...")
return
}
for {
bb.Connect()
bb.Login()
if len(bb.channelData) > 0 {
for channelName := range(bb.channelData) {
bb.JoinChannel(channelName)
}
if len(bb.Credentials.Channels) > 0 {
bb.JoinChannel(bb.Credentials.Channels...)
} else {
bb.JoinChannel()
}
@ -284,63 +257,6 @@ func (bb *KardBot) Start() {
}
}
func (bb *KardBot) readChannelData() error {
records, err := bb.Database.ReadAll("channelData")
if err != nil {
// no db? initialise one?
record := ChannelData{Name: bb.Channel, JoinTime: time.Now()}
rgb.YPrintf("[%s] No channel data for #%s exists, creating...\n", TimeStamp(), bb.Channel)
if err := bb.Database.Write("channelData", bb.Channel, record); err != nil {
return err
}
bb.channelData = make(map[string]ChannelData)
bb.channelData[bb.Channel] = record;
} else {
bb.channelData = make(map[string]ChannelData)
}
for _, data := range records {
record := ChannelData{}
err := json.Unmarshal([]byte(data), &record);
if err != nil {
return err
}
bb.channelData[record.Name] = record
}
return nil
}
func (bb *KardBot) readOrCreateChannelKey(channel string) string {
magicCode := ""
var err error
var record ChannelData
if record, ok := bb.channelData[channel]; !ok {
rgb.YPrintf("[%s] No channel data for #%s exists, creating\n", TimeStamp(), channel)
err = bb.Database.Read("channelData", channel, &record);
if err == nil {
bb.channelData[channel] = record
}
}
record = bb.channelData[channel]
if err != nil || record.AdminKey == "" {
rgb.YPrintf("[%s] No channel key for #%s exists, creating one\n", TimeStamp(), channel)
newuu, _ := uuid.NewRandom()
magicCode = base64.StdEncoding.EncodeToString([]byte(newuu.String()))
record.AdminKey = magicCode
if record.Name == "" {
record.Name = channel
}
if err := bb.Database.Write("channelData", channel, record); err != nil {
rgb.RPrintf("[%s] Error writing channel data for #%s\n", TimeStamp(), channel)
}
bb.channelData[record.Name] = record
rgb.YPrintf("[%s] Cached channel key for #%s\n", TimeStamp(), record.Name)
} else {
magicCode = record.AdminKey
rgb.YPrintf("[%s] Loaded data for #%s\n", TimeStamp(), channel)
}
return magicCode
}
func TimeStamp() string {
return TimeStampFmt(PSTFormat)
}

View File

@ -26,7 +26,6 @@ func HealthHandler(response http.ResponseWriter, request *http.Request) {
func NotFoundHandler(response http.ResponseWriter, request *http.Request) {
response.Header().Add("X-Template-File", "html"+request.URL.Path)
response.WriteHeader(404)
tmpl := template.Must(template.ParseFiles("web/404.html"))
tmpl.Execute(response, nil)
}
@ -73,7 +72,7 @@ func TemplateHandler(response http.ResponseWriter, request *http.Request) {
// NotFoundHandler(response, request)
// return
}
var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), 0, 0}
var td = TemplateData{ircBot.Prompts[rand.Intn(len(ircBot.Prompts))], len(ircBot.Prompts), len(ircBot.Credentials.Channels), 0}
err = tmpl.Execute(response, td)
if err != nil {
http.Error(response, err.Error(), http.StatusInternalServerError)
@ -81,11 +80,6 @@ func TemplateHandler(response http.ResponseWriter, request *http.Request) {
}
}
func AdminHandler(response http.ResponseWriter, request *http.Request) {
request.URL.Path = "/index.html"
TemplateHandler(response, request)
}
func HandleHTTP(passedIrcBot irc.KardBot) {
ircBot = passedIrcBot
r := mux.NewRouter()
@ -95,7 +89,6 @@ func HandleHTTP(passedIrcBot irc.KardBot) {
r.HandleFunc("/healthz", HealthHandler)
r.HandleFunc("/example/{.*}", TemplateHandler)
r.HandleFunc("/cover.css", CSSHandler)
r.HandleFunc("/admin/{channel}/{key}", AdminHandler)
http.Handle("/", r)
srv := &http.Server{
Handler: loggedRouter,

15
main.go
View File

@ -70,7 +70,7 @@ func readConfig() {
rgb.RPrintf("[%s] Could not unmarshal `%s`. Unmarshal error: %s\n", irc.TimeStamp(), configFile, err)
os.Exit(1)
}
rgb.YPrintf("[%s] Read config file from `%s`\n", irc.TimeStamp(), configFile)
rgb.YPrintf("[%s] Read config file from `%s\n`", irc.TimeStamp(), configFile)
return
}
@ -147,10 +147,7 @@ func readBonusStrings() []string {
return customStrings.Strings
}
var buildDate string
func main() {
rgb.YPrintf("[%s] starting karaokard bot build %s\n", irc.TimeStamp(), buildDate)
readConfig()
rand.Seed(time.Now().UnixNano())
for _, val := range builtins.Karaokards {
@ -159,13 +156,6 @@ func main() {
for _, val := range readBonusStrings() {
selectablePrompts = append(selectablePrompts, val)
}
persistentData := openDatabase()
var dbGlobalPrompts []string
if err := persistentData.Read("prompts", "global", &dbGlobalPrompts); err != nil {
persistentData.Write("prompts", "common", dbGlobalPrompts)
}
selectablePrompts := append(selectablePrompts, dbGlobalPrompts...)
rgb.YPrintf("[%s] %d prompts available.\n", irc.TimeStamp(), len(selectablePrompts))
oauthPath := ""
if config.OAuthPath == "" {
@ -193,6 +183,8 @@ func main() {
}
oauthPath = config.OAuthPath
}
persistentData := openDatabase()
persistentData.Write("prompts", "common", selectablePrompts)
// Replace the channel name, bot name, and the path to the private directory with your respective
// values.
@ -204,7 +196,6 @@ func main() {
PrivatePath: oauthPath,
Server: "irc.chat.twitch.tv",
Prompts: selectablePrompts,
Database: *persistentData,
}
go func() {
rgb.YPrintf("[%s] Starting webserver on port %s\n", irc.TimeStamp(), "5353")