2023-09-10 23:40:19 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-11 23:26:50 +02:00
|
|
|
"fmt"
|
2023-09-10 23:40:19 +02:00
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
"git.myrkvi.com/myrkvi/bin/controllers"
|
|
|
|
"git.myrkvi.com/myrkvi/bin/global"
|
|
|
|
"git.myrkvi.com/myrkvi/bin/models"
|
2023-09-10 23:40:19 +02:00
|
|
|
"github.com/glebarez/sqlite"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-09-11 23:26:50 +02:00
|
|
|
"github.com/spf13/viper"
|
2023-09-10 23:40:19 +02:00
|
|
|
prefixed "github.com/x-cray/logrus-prefixed-formatter"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetFormatter(&prefixed.TextFormatter{
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
logrus.Infoln("Application started.")
|
2023-09-11 23:26:50 +02:00
|
|
|
initConfig()
|
2023-09-10 23:40:19 +02:00
|
|
|
|
|
|
|
db_string := "bin.db"
|
|
|
|
var err error
|
2023-09-11 23:26:50 +02:00
|
|
|
global.DB, err = gorm.Open(sqlite.Open(db_string), &gorm.Config{})
|
2023-09-10 23:40:19 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatalln("Could not open database connection.")
|
|
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"db connection": db_string,
|
|
|
|
}).Infoln("Connected to database.")
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
models.DatabaseMigrations(global.DB)
|
2023-09-10 23:40:19 +02:00
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
e.Debug = false
|
|
|
|
|
|
|
|
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
|
|
|
LogURI: true,
|
|
|
|
LogStatus: true,
|
|
|
|
LogRemoteIP: true,
|
|
|
|
LogValuesFunc: func(c echo.Context, values middleware.RequestLoggerValues) error {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"URI": values.URI,
|
|
|
|
"status": values.Status,
|
|
|
|
"remote": values.RemoteIP,
|
|
|
|
}).Info("request")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
e.Static("/static", "static")
|
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
e.GET("/", controllers.IndexHandler)
|
|
|
|
e.GET("/new", controllers.GetNewHandler)
|
|
|
|
e.POST("/new", controllers.PostNewHandler)
|
|
|
|
e.GET("/b/:id", controllers.GetBinHandler)
|
|
|
|
e.POST("/b/:id/delete", controllers.DeleteBinHandler)
|
2023-09-10 23:40:19 +02:00
|
|
|
|
|
|
|
partial := e.Group("/partial")
|
|
|
|
new := partial.Group("/new")
|
2023-09-11 23:26:50 +02:00
|
|
|
new.GET("/upload", controllers.GetPartialUploadHandler)
|
|
|
|
new.GET("/text", controllers.GetPartialTextHandler)
|
2023-09-10 23:40:19 +02:00
|
|
|
|
2023-09-11 23:26:50 +02:00
|
|
|
listenAt := fmt.Sprintf(
|
|
|
|
"%s:%d",
|
|
|
|
viper.GetString("host"),
|
|
|
|
viper.GetUint16("port"),
|
|
|
|
)
|
|
|
|
e.Logger.Fatal(e.Start(listenAt))
|
2023-09-10 23:40:19 +02:00
|
|
|
}
|