106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"log"
|
|
|
|
"git.odit.services/lfk/document-server/docs" // Correct import path for docs
|
|
"git.odit.services/lfk/document-server/handlers"
|
|
"git.odit.services/lfk/document-server/models"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
|
"github.com/gofiber/swagger"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
config *models.Config
|
|
)
|
|
|
|
func validateAPIKey(c *fiber.Ctx, key string) (bool, error) {
|
|
hashedAPIKey := sha256.Sum256([]byte(config.APIKey))
|
|
hashedKey := sha256.Sum256([]byte(key))
|
|
|
|
if subtle.ConstantTimeCompare(hashedAPIKey[:], hashedKey[:]) == 1 {
|
|
return true, nil
|
|
}
|
|
return false, keyauth.ErrMissingOrMalformedAPIKey
|
|
}
|
|
|
|
func loadEnv() error {
|
|
|
|
viper.SetDefault("PORT", "3000")
|
|
viper.SetDefault("CARD_BARCODEFORMAT", "ean13")
|
|
viper.SetDefault("CARD_BARCODEPREFIX", "")
|
|
viper.SetDefault("SPONSORING_BARCODEFORMAT", "code128")
|
|
viper.SetDefault("SPONSORING_BARCODEPREFIX", "")
|
|
|
|
// Load .env file
|
|
viper.SetConfigFile(".env")
|
|
|
|
// Load environment variables
|
|
viper.AutomaticEnv()
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Unmarshal the config from file and env into the config struct
|
|
err = viper.Unmarshal(&config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// @title LfK Document Server API
|
|
// @description This is the API documentation for the LfK Document Server - a tool for pdf generation.
|
|
// @license.name CC BY-NC-SA 4.0
|
|
// @termsOfService https://lauf-fuer-kaya.de/datenschutz
|
|
// @contact.name ODIT.Services UG (haftungsbeschränkt)
|
|
// @contact.url https://odit.services
|
|
// @contact.email info@odit.services
|
|
// @securityDefinitions.apiKey ApiKeyAuth
|
|
// @in query
|
|
// @name key
|
|
func main() {
|
|
|
|
err := loadEnv()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
handler := handlers.DefaultHandler{
|
|
Config: config,
|
|
}
|
|
|
|
// Create a new Fiber instance
|
|
app := fiber.New(fiber.Config{
|
|
Prefork: config.Prod,
|
|
})
|
|
|
|
// Swagger documentation route
|
|
app.Get("/swagger/*", swagger.HandlerDefault)
|
|
|
|
v1 := app.Group("/v1")
|
|
|
|
pdfv1 := v1.Group("/pdfs")
|
|
pdfv1.Use(keyauth.New(keyauth.Config{
|
|
KeyLookup: "query:key",
|
|
Validator: validateAPIKey,
|
|
}))
|
|
|
|
pdfv1.Post("/contracts", handler.GenerateContract)
|
|
pdfv1.Post("/cards", handler.GenerateCard)
|
|
pdfv1.Post("/certificates", handler.GenerateCertificate)
|
|
|
|
v1.Get("/barcodes/:type/:content", handler.GenerateBarcode)
|
|
|
|
app.Use(handler.NotFoundHandler)
|
|
docs.SwaggerInfo.BasePath = "/"
|
|
|
|
log.Fatal(app.Listen("0.0.0.0:" + config.Port))
|
|
}
|