All checks were successful
Create Release & Upload Assets / Upload Assets To Gitea w/ goreleaser (push) Successful in 2m30s
161 lines
3.8 KiB
Go
161 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
fifo "github.com/foize/go.fifo"
|
|
sentryecho "github.com/getsentry/sentry-go/echo"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo-jwt/v4"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"golang.org/x/time/rate"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"uberbringer/config"
|
|
"uberbringer/snowflake"
|
|
)
|
|
|
|
type Queue struct {
|
|
Data []Item `json:"data"`
|
|
}
|
|
|
|
type InputData struct {
|
|
Data []Item `json:"data"`
|
|
}
|
|
|
|
type DataStruct struct {
|
|
JobID string `json:"job"`
|
|
Data []Item `json:"data"`
|
|
}
|
|
|
|
type Item struct {
|
|
Type int `json:"type"`
|
|
Size int `json:"size"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
type jwtCustomClaims struct {
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func StartEcho(server chan bool, webserver_addr string, webserver_port int) {
|
|
|
|
queue := fifo.NewQueue()
|
|
|
|
// Start Echo
|
|
e := echo.New()
|
|
|
|
e.Use(middleware.Logger())
|
|
e.Use(middleware.Recover())
|
|
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(rate.Limit(1))))
|
|
|
|
// Echo GZIP
|
|
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
|
Level: 3,
|
|
}))
|
|
|
|
// Echo Request ID
|
|
e.Use(middleware.RequestIDWithConfig(middleware.RequestIDConfig{
|
|
Generator: func() string {
|
|
var idGeneratorSettings *snowflake.Settings
|
|
|
|
// build snowflake using the IdGenerator API
|
|
idGeneratorSettings = &snowflake.Settings{}
|
|
idGeneratorSettings.StartTime = time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
SnowFlakeID, err := snowflake.GenerateID(idGeneratorSettings)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
return strconv.FormatUint(SnowFlakeID.List[0], 10)
|
|
},
|
|
}))
|
|
|
|
// Once it's done, you can attach the handler as one of your middleware
|
|
e.Use(sentryecho.New(sentryecho.Options{
|
|
Repanic: true,
|
|
}))
|
|
|
|
// API Routes
|
|
|
|
// API Version 1
|
|
api_v1 := e.Group("/api/v1")
|
|
|
|
// Echo JWT - Won't accept valid keys? Bug To-Do
|
|
api_v1.Use(echojwt.WithConfig(echojwt.Config{
|
|
SigningKey: []byte(config.JWT_Secret),
|
|
SigningMethod: "HS256",
|
|
TokenLookup: "header:Authorization:Bearer ,query:token, param:token",
|
|
ContextKey: "User",
|
|
NewClaimsFunc: func(c echo.Context) jwt.Claims {
|
|
return new(jwtCustomClaims)
|
|
},
|
|
}))
|
|
|
|
api_v1.POST("/print", func(c echo.Context) error {
|
|
|
|
var idGeneratorSettings *snowflake.Settings
|
|
idGeneratorSettings = &snowflake.Settings{}
|
|
idGeneratorSettings.StartTime = time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
SnowFlakeID, err := snowflake.GenerateID(idGeneratorSettings)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
var printJob string
|
|
printJob = strconv.FormatUint(SnowFlakeID.List[0], 10)
|
|
|
|
data := new(InputData)
|
|
if err := c.Bind(data); err != nil {
|
|
return c.String(http.StatusBadRequest, "bad request")
|
|
}
|
|
|
|
// Load into separate struct for security
|
|
toPrint := DataStruct{
|
|
JobID: printJob,
|
|
}
|
|
|
|
toPrint.Data = data.Data
|
|
|
|
printJobCreate(toPrint, queue)
|
|
|
|
return c.String(http.StatusOK, "OK - "+printJob)
|
|
})
|
|
|
|
e.GET("/robots.txt", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, "User-agent: *\nDisallow:\n")
|
|
})
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
// Project Information
|
|
type ProjectInformation struct {
|
|
Project string `json:"Project" xml:"Project"`
|
|
Description string `json:"Description" xml:"Description"`
|
|
Authors string `json:"Authors" xml:"Authors"`
|
|
Version string `json:"Version" xml:"Version"`
|
|
Build string `json:"Build" xml:"Build"`
|
|
}
|
|
|
|
// Build Response
|
|
Response := &ProjectInformation{
|
|
Project: string(config.Project),
|
|
Description: string(config.Description),
|
|
Authors: string(config.Authors),
|
|
Version: string(config.Version),
|
|
Build: string(config.Build),
|
|
}
|
|
|
|
// Return Response
|
|
return c.JSONPretty(http.StatusOK, Response, " ")
|
|
}).Name = "Base"
|
|
|
|
e.Logger.Fatal(e.Start(webserver_addr + ":" + strconv.Itoa(webserver_port)))
|
|
|
|
server <- true
|
|
|
|
}
|