85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"code.gurenya.net/carlsmei/muninn-aio/config"
|
|
"code.gurenya.net/carlsmei/muninn-aio/db"
|
|
"code.gurenya.net/carlsmei/muninn-aio/ent"
|
|
"code.gurenya.net/carlsmei/muninn-aio/services"
|
|
"github.com/asaskevich/govalidator"
|
|
"github.com/go-telegram/bot"
|
|
"github.com/go-telegram/bot/models"
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/lib/pq"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var (
|
|
Version = "dev"
|
|
)
|
|
|
|
func main() {
|
|
config.Load()
|
|
db.EntInit()
|
|
|
|
// TELEGRAM
|
|
opts := []bot.Option{
|
|
// bot.WithDefaultHandler(handler),
|
|
}
|
|
|
|
b, err := bot.New(config.Env.TelegramToken, opts...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
b.RegisterHandler(bot.HandlerTypeMessageText, "/start", bot.MatchTypeExact, startHandler)
|
|
b.RegisterHandlerMatchFunc(func(update *models.Update) bool {
|
|
return update.Message != nil && govalidator.IsURL(update.Message.Text)
|
|
}, generateLinkHandler)
|
|
|
|
go b.Start(context.Background())
|
|
|
|
// FIBER
|
|
app := fiber.New()
|
|
|
|
app.Use(func(c *fiber.Ctx) error {
|
|
c.Set("Server", fmt.Sprintf("muninn-aio (%s)", Version))
|
|
|
|
return c.Next()
|
|
})
|
|
|
|
app.Get("/:key", func(c *fiber.Ctx) error {
|
|
key := c.Params("key")
|
|
|
|
link, err := services.GetLinkByKey(c.Context(), key)
|
|
if err != nil {
|
|
if ent.IsNotFound(err) {
|
|
return c.Status(404).SendString("not found")
|
|
}
|
|
}
|
|
|
|
c.SendString(fmt.Sprintf("Found. Redirecting to %s", link.OriginalURL))
|
|
return c.Redirect(link.OriginalURL, 302)
|
|
})
|
|
|
|
var addr string
|
|
if strings.Contains(config.Env.AppPort, ":") {
|
|
addr = config.Env.AppPort
|
|
} else {
|
|
addr = ":" + config.Env.AppPort
|
|
}
|
|
|
|
app.Listen(addr)
|
|
|
|
// quit := make(chan os.Signal, 1)
|
|
// signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
|
|
// sig := <-quit
|
|
|
|
// fmt.Printf("Shutting down server... Reason: %s\n", sig)
|
|
}
|