2023-12-17 19:07:05 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-20 02:06:05 +01:00
|
|
|
"html/template"
|
2023-12-17 19:07:05 +01:00
|
|
|
"net/http"
|
2024-01-17 19:56:33 +01:00
|
|
|
"os"
|
2023-12-20 02:06:05 +01:00
|
|
|
"sort"
|
2023-12-17 19:07:05 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/lyx0/nourybot/internal/common"
|
2023-12-20 02:06:05 +01:00
|
|
|
"github.com/lyx0/nourybot/internal/data"
|
2023-12-17 19:07:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (app *application) startRouter() {
|
|
|
|
router := httprouter.New()
|
2023-12-20 02:06:05 +01:00
|
|
|
router.GET("/", app.homeRoute)
|
2023-12-17 19:07:05 +01:00
|
|
|
router.GET("/status", app.statusPageRoute)
|
|
|
|
router.GET("/commands/:channel", app.channelCommandsRoute)
|
2023-12-18 00:04:27 +01:00
|
|
|
router.GET("/commands", app.commandsRoute)
|
2023-12-17 20:11:02 +01:00
|
|
|
router.GET("/timer/:channel", app.channelTimersRoute)
|
2023-12-17 19:07:05 +01:00
|
|
|
|
2024-01-17 19:56:33 +01:00
|
|
|
// Serve files uploaded by the meme command, but don't list the directory contents.
|
|
|
|
fs := justFilesFilesystem{http.Dir("/public/uploads/")}
|
|
|
|
router.Handler("GET", "/uploads/*filepath", http.StripPrefix("/uploads", http.FileServer(fs)))
|
|
|
|
|
2023-12-17 19:07:05 +01:00
|
|
|
app.Log.Fatal(http.ListenAndServe(":8080", router))
|
|
|
|
}
|
|
|
|
|
2023-12-20 02:06:05 +01:00
|
|
|
type commandsRouteData struct {
|
|
|
|
Commands map[string]command
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) commandsRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
t, err := template.ParseFiles("./web/templates/commands.page.gohtml")
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
2023-12-18 00:04:27 +01:00
|
|
|
var cs []string
|
|
|
|
|
2023-12-20 02:06:05 +01:00
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range helpText {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
var c string
|
|
|
|
|
|
|
|
if v.Alias == nil {
|
|
|
|
c = fmt.Sprintf("Name: %s\nDescription: %s\nLevel: %s\nUsage: %s\n\n", i, v.Description, v.Level, v.Usage)
|
|
|
|
} else {
|
|
|
|
c = fmt.Sprintf("Name: %s\nAliases: %s\nDescription: %s\nLevel: %s\nUsage: %s\n\n", i, v.Alias, v.Description, v.Level, v.Usage)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(cs)
|
|
|
|
data := &commandsRouteData{helpText}
|
|
|
|
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type homeRouteData struct {
|
|
|
|
Channels []*data.Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) homeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
t, err := template.ParseFiles("./web/templates/home.page.gohtml")
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allChannel, err := app.Models.Channels.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.Log.Infow("All channels:",
|
|
|
|
"channel", allChannel)
|
2023-12-20 02:08:14 +01:00
|
|
|
data := &homeRouteData{allChannel}
|
2023-12-18 00:04:27 +01:00
|
|
|
|
2023-12-20 02:06:05 +01:00
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-12-18 00:04:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-17 19:07:05 +01:00
|
|
|
func (app *application) channelCommandsRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
channel := ps.ByName("channel")
|
2023-12-17 19:49:24 +01:00
|
|
|
var cs []string
|
|
|
|
var text string
|
2023-12-17 19:07:05 +01:00
|
|
|
|
|
|
|
command, err := app.Models.Commands.GetAllChannel(channel)
|
|
|
|
if err != nil {
|
2023-12-17 19:49:24 +01:00
|
|
|
app.Log.Errorw("Error trying to retrieve all commands for a channel from database", err)
|
2023-12-17 19:07:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
|
2023-12-18 00:04:27 +01:00
|
|
|
heading := fmt.Sprintf("Commands in %s\n\n", channel)
|
|
|
|
cs = append(cs, heading)
|
2023-12-17 19:07:05 +01:00
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range command {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
var c string
|
|
|
|
|
2023-12-18 00:04:27 +01:00
|
|
|
c = fmt.Sprintf(
|
|
|
|
"Name: %v\n"+
|
|
|
|
"Description: %v\n"+
|
|
|
|
"Level: %v\n"+
|
|
|
|
"Text: %v\n"+
|
|
|
|
"\n",
|
|
|
|
v.Name, v.Description, v.Level, v.Text,
|
|
|
|
)
|
2023-12-17 19:07:05 +01:00
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
|
2023-12-17 19:49:24 +01:00
|
|
|
text = strings.Join(cs, "")
|
|
|
|
fmt.Fprintf(w, fmt.Sprint(text))
|
|
|
|
|
2023-12-17 19:07:05 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 20:11:02 +01:00
|
|
|
func (app *application) channelTimersRoute(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
channel := ps.ByName("channel")
|
|
|
|
var ts []string
|
|
|
|
var text string
|
|
|
|
|
|
|
|
timer, err := app.Models.Timers.GetChannelTimer(channel)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorw("Error trying to retrieve all timer for a channel from database", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// The slice of timers is only used to log them at
|
|
|
|
// the start so it looks a bit nicer.
|
|
|
|
|
|
|
|
// Iterate over all timers and then add them onto the scheduler.
|
|
|
|
for i, v := range timer {
|
|
|
|
// idk why this works but it does so no touchy touchy.
|
|
|
|
// https://github.com/robfig/cron/issues/420#issuecomment-940949195
|
|
|
|
i, v := i, v
|
|
|
|
_ = i
|
|
|
|
var t string
|
|
|
|
|
|
|
|
t = fmt.Sprintf(
|
|
|
|
"Name: \t%v\n"+
|
|
|
|
"Text: \t%v\n"+
|
|
|
|
"Repeat: %v\n"+
|
|
|
|
"\n",
|
|
|
|
v.Name, v.Text, v.Repeat,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add new value to the slice
|
|
|
|
ts = append(ts, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
text = strings.Join(ts, "")
|
|
|
|
fmt.Fprintf(w, fmt.Sprint(text))
|
|
|
|
}
|
|
|
|
|
2023-12-17 19:07:05 +01:00
|
|
|
func (app *application) statusPageRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
commit := common.GetVersion()
|
|
|
|
started := common.GetUptime().Format("2006-1-2 15:4:5")
|
|
|
|
commitLink := fmt.Sprintf("https://github.com/lyx0/nourybot/commit/%v", common.GetVersionPure())
|
|
|
|
|
|
|
|
fmt.Fprintf(w, fmt.Sprintf("started: \t%v\nenvironment: \t%v\ncommit: \t%v\ngithub: \t%v", started, app.Environment, commit, commitLink))
|
|
|
|
}
|
2024-01-17 19:56:33 +01:00
|
|
|
|
|
|
|
// Since I want to serve the files that I uploaded with the meme command to the /public/uploads
|
|
|
|
// folder, but not list the directory on the `/uploads/` route I found this issue that solves
|
|
|
|
// that problem with the httprouter.
|
|
|
|
//
|
|
|
|
// https://github.com/julienschmidt/httprouter/issues/25#issuecomment-74977940
|
|
|
|
type justFilesFilesystem struct {
|
|
|
|
fs http.FileSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs justFilesFilesystem) Open(name string) (http.File, error) {
|
|
|
|
f, err := fs.fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return neuteredReaddirFile{f}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type neuteredReaddirFile struct {
|
|
|
|
http.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|