mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-10 09:02:49 +00:00
switched to gin - use logrus
This commit is contained in:
@@ -82,7 +82,7 @@ export class Soundboard extends React.Component<Props, State> {
|
|||||||
formData.append("file", file);
|
formData.append("file", file);
|
||||||
formData.append("password", this.state.password);
|
formData.append("password", this.state.password);
|
||||||
|
|
||||||
axios.put("/api/upload", formData, this.config)
|
axios.post("/api/upload", formData, this.config)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.setState({
|
this.setState({
|
||||||
password: "",
|
password: "",
|
||||||
|
|||||||
25
main.go
25
main.go
@@ -1,16 +1,39 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mgerb/go-discord-bot/server/bot"
|
"github.com/mgerb/go-discord-bot/server/bot"
|
||||||
"github.com/mgerb/go-discord-bot/server/bothandlers"
|
"github.com/mgerb/go-discord-bot/server/bothandlers"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver"
|
"github.com/mgerb/go-discord-bot/server/webserver"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func init() {
|
||||||
//read config file
|
//read config file
|
||||||
config.Init()
|
config.Init()
|
||||||
|
|
||||||
|
// Log as JSON instead of the default ASCII formatter.
|
||||||
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
|
|
||||||
|
// Output to stdout instead of the default stderr
|
||||||
|
// Can be any io.Writer, see below for File example
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
|
||||||
|
if config.Flags.Prod {
|
||||||
|
// prod
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
// Only log the warning severity or above.
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
} else {
|
||||||
|
// debug
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
//connect bot to account with token
|
//connect bot to account with token
|
||||||
bot.Connect(config.Config.Token)
|
bot.Connect(config.Config.Token)
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Variables used for command line parameters
|
// Variables used for command line parameters
|
||||||
@@ -19,7 +17,7 @@ func Connect(token string) {
|
|||||||
Session, err = discordgo.New("Bot " + token)
|
Session, err = discordgo.New("Bot " + token)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
log.Fatal("Error creating Discord session.", err)
|
log.Fatal("Error creating Discord session.", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,14 +25,14 @@ func Connect(token string) {
|
|||||||
u, err := Session.User("@me")
|
u, err := Session.User("@me")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error obtaining account details. Make sure you have the correct bot token.")
|
log.Error("Error obtaining account details. Make sure you have the correct bot token.")
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the account ID for later use.
|
// Store the account ID for later use.
|
||||||
BotID = u.ID
|
BotID = u.ID
|
||||||
|
|
||||||
fmt.Println("Bot connected")
|
log.Debug("Bot connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start - blocking function that starts a websocket listenting for discord callbacks
|
// Start - blocking function that starts a websocket listenting for discord callbacks
|
||||||
@@ -45,11 +43,11 @@ func Start() {
|
|||||||
// Open the websocket and begin listening.
|
// Open the websocket and begin listening.
|
||||||
err := Session.Open()
|
err := Session.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error opening connection,", err)
|
log.Error("error opening connection,", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Bot is now running...")
|
log.Debug("Bot is now running...")
|
||||||
|
|
||||||
// Simple way to keep program running until CTRL-C is pressed.
|
// Simple way to keep program running until CTRL-C is pressed.
|
||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package bothandlers
|
package bothandlers
|
||||||
|
|
||||||
|
/***** DEPRECATED *****/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -19,6 +18,7 @@ import (
|
|||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/cryptix/wav"
|
"github.com/cryptix/wav"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -61,7 +61,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
c, err := s.State.Channel(m.ChannelID)
|
c, err := s.State.Channel(m.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Could not find channel.
|
// Could not find channel.
|
||||||
log.Println("Unable to find channel.")
|
log.Error("Unable to find channel.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
// Find the guild for that channel.
|
// Find the guild for that channel.
|
||||||
newGuild, err := s.State.Guild(c.GuildID)
|
newGuild, err := s.State.Guild(c.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,14 +138,14 @@ func (conn *audioConnection) summon(m *discordgo.MessageCreate) {
|
|||||||
c, err := conn.session.State.Channel(m.ChannelID)
|
c, err := conn.session.State.Channel(m.ChannelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Could not find channel.
|
// Could not find channel.
|
||||||
log.Println("User channel not found.")
|
log.Error("User channel not found.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the guild for that channel.
|
// Find the guild for that channel.
|
||||||
g, err := conn.session.State.Guild(c.GuildID)
|
g, err := conn.session.State.Guild(c.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ func (conn *audioConnection) summon(m *discordgo.MessageCreate) {
|
|||||||
conn.voiceConnection, err = conn.session.ChannelVoiceJoin(g.ID, vs.ChannelID, false, false)
|
conn.voiceConnection, err = conn.session.ChannelVoiceJoin(g.ID, vs.ChannelID, false, false)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the current channel
|
// set the current channel
|
||||||
@@ -184,7 +184,7 @@ func (conn *audioConnection) playAudio(soundName string, m *discordgo.MessageCre
|
|||||||
err := conn.loadFile(soundName)
|
err := conn.loadFile(soundName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,7 +224,7 @@ func (conn *audioConnection) loadFile(fileName string) error {
|
|||||||
return errors.New("File not found")
|
return errors.New("File not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Loading file: " + fname + fextension)
|
log.Debug("Loading file: " + fname + fextension)
|
||||||
|
|
||||||
// use ffmpeg to convert file into a format we can use
|
// use ffmpeg to convert file into a format we can use
|
||||||
cmd := exec.Command("ffmpeg", "-i", config.Config.SoundsPath+fname+fextension, "-f", "s16le", "-ar", strconv.Itoa(frameRate), "-ac", strconv.Itoa(channels), "pipe:1")
|
cmd := exec.Command("ffmpeg", "-i", config.Config.SoundsPath+fname+fextension, "-f", "s16le", "-ar", strconv.Itoa(frameRate), "-ac", strconv.Itoa(channels), "pipe:1")
|
||||||
@@ -353,14 +353,14 @@ loop:
|
|||||||
if !ok {
|
if !ok {
|
||||||
speakers[opusChannel.SSRC], err = gopus.NewDecoder(frameRate, 1)
|
speakers[opusChannel.SSRC], err = gopus.NewDecoder(frameRate, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error creating opus decoder", err)
|
log.Error("error creating opus decoder", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opusChannel.PCM, err = speakers[opusChannel.SSRC].Decode(opusChannel.Opus, frameSize, false)
|
opusChannel.PCM, err = speakers[opusChannel.SSRC].Decode(opusChannel.Opus, frameSize, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error decoding opus data", err)
|
log.Error("Error decoding opus data", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -424,6 +424,6 @@ func (conn *audioConnection) toggleSoundPlayingLock(playing bool) {
|
|||||||
|
|
||||||
func checkErr(err error) {
|
func checkErr(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,8 @@ package config
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Variables used for command line parameters
|
// Variables used for command line parameters
|
||||||
@@ -30,52 +29,45 @@ type configFile struct {
|
|||||||
|
|
||||||
type configFlags struct {
|
type configFlags struct {
|
||||||
Prod bool
|
Prod bool
|
||||||
TLS bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init -
|
// Init -
|
||||||
func Init() {
|
func Init() {
|
||||||
|
|
||||||
parseConfig()
|
parseConfig()
|
||||||
parseFlags()
|
parseFlags()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig() {
|
func parseConfig() {
|
||||||
|
|
||||||
log.Println("Reading config file...")
|
log.Debug("Reading config file...")
|
||||||
|
|
||||||
file, e := ioutil.ReadFile("./config.json")
|
file, e := ioutil.ReadFile("./config.json")
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
log.Printf("File error: %v\n", e)
|
log.Fatal("File error: %v\n", e)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s\n", string(file))
|
log.Debug("%s\n", string(file))
|
||||||
|
|
||||||
err := json.Unmarshal(file, &Config)
|
err := json.Unmarshal(file, &Config)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFlags() {
|
func parseFlags() {
|
||||||
|
|
||||||
Flags.Prod = false
|
Flags.Prod = false
|
||||||
Flags.TLS = false
|
|
||||||
|
|
||||||
prod := flag.Bool("p", false, "Run in production")
|
prod := flag.Bool("p", false, "Run in production")
|
||||||
tls := flag.Bool("tls", false, "Use TLS")
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
Flags.Prod = *prod
|
Flags.Prod = *prod
|
||||||
Flags.TLS = *tls
|
|
||||||
|
|
||||||
if *prod {
|
if Flags.Prod {
|
||||||
log.Println("Running in production mode")
|
log.Warn("Running in production mode")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
package response
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
DefaultUnauthorized = []byte("Unauthorized.")
|
|
||||||
DefaultInternalError = []byte("Internal error.")
|
|
||||||
)
|
|
||||||
|
|
||||||
// JSON - marshals the provided interface and returns JSON to client
|
|
||||||
func JSON(w http.ResponseWriter, content interface{}) {
|
|
||||||
|
|
||||||
output, err := json.Marshal(content)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
ERR(w, http.StatusInternalServerError, []byte("Internal error."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("content-type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ERR - send error response
|
|
||||||
func ERR(w http.ResponseWriter, status int, content []byte) {
|
|
||||||
w.WriteHeader(status)
|
|
||||||
w.Write(content)
|
|
||||||
}
|
|
||||||
@@ -2,19 +2,18 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/mgerb/chi_auth_server/response"
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Downloader -
|
// Downloader -
|
||||||
func Downloader(w http.ResponseWriter, r *http.Request) {
|
func Downloader(c *gin.Context) {
|
||||||
url := r.FormValue("url")
|
url := c.Query("url")
|
||||||
fileType := r.FormValue("fileType")
|
fileType := c.Query("fileType")
|
||||||
|
|
||||||
// create youtube folder if it does not exist
|
// create youtube folder if it does not exist
|
||||||
if _, err := os.Stat("youtube"); os.IsNotExist(err) {
|
if _, err := os.Stat("youtube"); os.IsNotExist(err) {
|
||||||
@@ -29,8 +28,8 @@ func Downloader(w http.ResponseWriter, r *http.Request) {
|
|||||||
err := titleCmd.Run()
|
err := titleCmd.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
response.ERR(w, http.StatusInternalServerError, response.DefaultInternalError)
|
c.JSON(400, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +39,7 @@ func Downloader(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// remove all special characters from title
|
// remove all special characters from title
|
||||||
cleanTitle := cleanseTitle(titleOut.String())
|
cleanTitle := cleanseTitle(titleOut.String())
|
||||||
log.Println(cleanTitle)
|
log.Debug(cleanTitle)
|
||||||
|
|
||||||
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", "./youtube/"+cleanTitle+".%(ext)s", url)
|
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", "./youtube/"+cleanTitle+".%(ext)s", url)
|
||||||
|
|
||||||
@@ -50,13 +49,13 @@ func Downloader(w http.ResponseWriter, r *http.Request) {
|
|||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(out.String())
|
log.Error(out.String())
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
response.ERR(w, http.StatusInternalServerError, response.DefaultInternalError)
|
c.JSON(400, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.JSON(w, map[string]interface{}{"fileName": cleanTitle + "." + fileType})
|
c.JSON(200, map[string]interface{}{"fileName": cleanTitle + "." + fileType})
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanseTitle(title string) string {
|
func cleanseTitle(title string) string {
|
||||||
@@ -64,7 +63,7 @@ func cleanseTitle(title string) string {
|
|||||||
// Make a Regex to say we only want
|
// Make a Regex to say we only want
|
||||||
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return reg.ReplaceAllString(title, "")
|
return reg.ReplaceAllString(title, "")
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/mgerb/chi_auth_server/response"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var soundList []sound
|
var soundList []sound
|
||||||
@@ -20,17 +20,17 @@ type sound struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SoundList -
|
// SoundList -
|
||||||
func SoundList(w http.ResponseWriter, r *http.Request) {
|
func SoundList(c *gin.Context) {
|
||||||
|
|
||||||
if len(soundList) < 1 {
|
if len(soundList) < 1 {
|
||||||
err := PopulateSoundList()
|
err := PopulateSoundList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte(response.DefaultInternalError))
|
c.JSON(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response.JSON(w, soundList)
|
c.JSON(200, soundList)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PopulateSoundList -
|
// PopulateSoundList -
|
||||||
@@ -61,15 +61,15 @@ func PopulateSoundList() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ClipList -
|
// ClipList -
|
||||||
func ClipList(w http.ResponseWriter, r *http.Request) {
|
func ClipList(c *gin.Context) {
|
||||||
|
|
||||||
clipList := []sound{}
|
clipList := []sound{}
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(config.Config.ClipsPath)
|
files, err := ioutil.ReadDir(config.Config.ClipsPath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
response.ERR(w, 500, response.DefaultInternalError)
|
c.JSON(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,5 +85,5 @@ func ClipList(w http.ResponseWriter, r *http.Request) {
|
|||||||
clipList = append(clipList, listItem)
|
clipList = append(clipList, listItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
response.JSON(w, clipList)
|
c.JSON(200, clipList)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,67 +1,54 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/mgerb/chi_auth_server/response"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileUpload
|
// FileUpload
|
||||||
func FileUpload(w http.ResponseWriter, r *http.Request) {
|
func FileUpload(c *gin.Context) {
|
||||||
|
|
||||||
password := r.FormValue("password")
|
password := c.PostForm("password")
|
||||||
|
|
||||||
if string(password) != config.Config.UploadPassword {
|
if string(password) != config.Config.UploadPassword {
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("Invalid password."))
|
c.JSON(http.StatusInternalServerError, "Invalid password.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
file, header, err := r.FormFile("file")
|
file, err := c.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("Error reading file."))
|
log.Error(err)
|
||||||
|
c.JSON(http.StatusInternalServerError, "Error reading file.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
src, err := header.Open()
|
|
||||||
if err != nil {
|
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("Error opening file."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer src.Close()
|
|
||||||
|
|
||||||
// create uploads folder if it does not exist
|
// create uploads folder if it does not exist
|
||||||
if _, err := os.Stat(config.Config.SoundsPath); os.IsNotExist(err) {
|
if _, err := os.Stat(config.Config.SoundsPath); os.IsNotExist(err) {
|
||||||
os.Mkdir(config.Config.SoundsPath, os.ModePerm)
|
os.Mkdir(config.Config.SoundsPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert file name to lower case and trim spaces
|
// convert file name to lower case and trim spaces
|
||||||
header.Filename = strings.ToLower(header.Filename)
|
file.Filename = strings.ToLower(file.Filename)
|
||||||
header.Filename = strings.Replace(header.Filename, " ", "", -1)
|
file.Filename = strings.Replace(file.Filename, " ", "", -1)
|
||||||
|
|
||||||
// check if file already exists
|
// check if file already exists
|
||||||
if _, err := os.Stat(config.Config.SoundsPath + header.Filename); err == nil {
|
if _, err := os.Stat(config.Config.SoundsPath + file.Filename); err == nil {
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("File already exists."))
|
c.JSON(http.StatusInternalServerError, "File already exists.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dst, err := os.Create(config.Config.SoundsPath + header.Filename)
|
err = c.SaveUploadedFile(file, config.Config.SoundsPath+file.Filename)
|
||||||
|
log.Debug("Saving file", config.Config.SoundsPath+file.Filename)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("Error creating file."))
|
log.Error(err)
|
||||||
return
|
c.JSON(http.StatusInternalServerError, "Error creating file.")
|
||||||
}
|
|
||||||
|
|
||||||
defer dst.Close()
|
|
||||||
|
|
||||||
if _, err = io.Copy(dst, src); err != nil {
|
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("Error writing file."))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,9 +56,10 @@ func FileUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
err = PopulateSoundList()
|
err = PopulateSoundList()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.ERR(w, http.StatusInternalServerError, []byte("Error populating sound list."))
|
log.Error(err)
|
||||||
|
c.JSON(http.StatusInternalServerError, "Error populating sound list.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.JSON(w, []byte("Success"))
|
c.JSON(200, "Success")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
package pubg
|
package pubg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mgerb/chi_auth_server/response"
|
"github.com/gin-gonic/gin"
|
||||||
pubgClient "github.com/mgerb/go-pubg"
|
pubgClient "github.com/mgerb/go-pubg"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -21,7 +20,7 @@ func Start(key string, players []string) {
|
|||||||
|
|
||||||
apiKey = key
|
apiKey = key
|
||||||
|
|
||||||
log.Println("Gathering pubg data...")
|
log.Debug("Gathering pubg data...")
|
||||||
|
|
||||||
go fetchStats(players)
|
go fetchStats(players)
|
||||||
}
|
}
|
||||||
@@ -41,7 +40,7 @@ func fetchStats(players []string) {
|
|||||||
newStats, err := api.GetPlayer(player)
|
newStats, err := api.GetPlayer(player)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +57,6 @@ func fetchStats(players []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handler - returns the pubg stats
|
// Handler - returns the pubg stats
|
||||||
func Handler(w http.ResponseWriter, r *http.Request) {
|
func Handler(c *gin.Context) {
|
||||||
response.JSON(w, stats)
|
c.JSON(200, stats)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,53 +1,32 @@
|
|||||||
package webserver
|
package webserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/acme/autocert"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
|
||||||
"github.com/go-chi/chi/middleware"
|
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver/pubg"
|
"github.com/mgerb/go-discord-bot/server/webserver/pubg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getRouter() *chi.Mux {
|
func getRouter() *gin.Engine {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
r := chi.NewRouter()
|
router.Static("/static", "./dist/static")
|
||||||
|
router.Static("/public/sounds", config.Config.SoundsPath)
|
||||||
|
router.Static("/public/youtube", "./youtube")
|
||||||
|
router.Static("/public/clips", config.Config.ClipsPath)
|
||||||
|
|
||||||
r.Use(middleware.RequestID)
|
router.NoRoute(func(c *gin.Context) {
|
||||||
r.Use(middleware.DefaultCompress)
|
c.File("./dist/index.html")
|
||||||
|
|
||||||
if !config.Flags.Prod {
|
|
||||||
r.Use(middleware.Logger)
|
|
||||||
}
|
|
||||||
|
|
||||||
workDir, _ := os.Getwd()
|
|
||||||
|
|
||||||
FileServer(r, "/static", http.Dir(filepath.Join(workDir, "./dist/static")))
|
|
||||||
FileServer(r, "/public/sounds", http.Dir(filepath.Join(workDir, config.Config.SoundsPath)))
|
|
||||||
FileServer(r, "/public/youtube", http.Dir(filepath.Join(workDir, "./youtube")))
|
|
||||||
FileServer(r, "/public/clips", http.Dir(filepath.Join(workDir, config.Config.ClipsPath)))
|
|
||||||
|
|
||||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
http.ServeFile(w, r, "./dist/index.html")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Route("/api", func(r chi.Router) {
|
api := router.Group("/api")
|
||||||
// configure api end points
|
api.GET("/stats/pubg", pubg.Handler)
|
||||||
r.Get("/soundlist", handlers.SoundList)
|
api.GET("/ytdownloader", handlers.Downloader)
|
||||||
r.Get("/cliplist", handlers.ClipList)
|
api.GET("/soundlist", handlers.SoundList)
|
||||||
r.Put("/upload", handlers.FileUpload)
|
api.GET("/cliplist", handlers.ClipList)
|
||||||
r.Get("/ytdownloader", handlers.Downloader)
|
api.POST("/upload", handlers.FileUpload)
|
||||||
r.Get("/stats/pubg", pubg.Handler)
|
|
||||||
})
|
|
||||||
|
|
||||||
return r
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start -
|
// Start -
|
||||||
@@ -59,49 +38,5 @@ func Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router := getRouter()
|
router := getRouter()
|
||||||
|
router.Run(config.Config.ServerAddr)
|
||||||
if config.Flags.TLS {
|
|
||||||
|
|
||||||
// start server on port 80 to redirect
|
|
||||||
go http.ListenAndServe(":80", http.HandlerFunc(redirect))
|
|
||||||
|
|
||||||
// start TLS server
|
|
||||||
log.Fatal(http.Serve(autocert.NewListener(), router))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// start basic server
|
|
||||||
http.ListenAndServe(config.Config.ServerAddr, router)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FileServer conveniently sets up a http.FileServer handler to serve
|
|
||||||
// static files from a http.FileSystem.
|
|
||||||
func FileServer(r chi.Router, path string, root http.FileSystem) {
|
|
||||||
if strings.ContainsAny(path, "{}*") {
|
|
||||||
panic("FileServer does not permit URL parameters.")
|
|
||||||
}
|
|
||||||
|
|
||||||
fs := http.StripPrefix(path, http.FileServer(root))
|
|
||||||
|
|
||||||
if path != "/" && path[len(path)-1] != '/' {
|
|
||||||
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
|
|
||||||
path += "/"
|
|
||||||
}
|
|
||||||
path += "*"
|
|
||||||
|
|
||||||
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fs.ServeHTTP(w, r)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
// redirect to https
|
|
||||||
func redirect(w http.ResponseWriter, req *http.Request) {
|
|
||||||
// remove/add not default ports from req.Host
|
|
||||||
target := "https://" + req.Host + req.URL.Path
|
|
||||||
if len(req.URL.RawQuery) > 0 {
|
|
||||||
target += "?" + req.URL.RawQuery
|
|
||||||
}
|
|
||||||
|
|
||||||
http.Redirect(w, req, target, http.StatusTemporaryRedirect)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user