ci: add ci

This commit is contained in:
2025-06-21 13:07:53 +03:00
parent 1874ae3ac1
commit 8ca548a5e3
2 changed files with 79 additions and 11 deletions
+14 -11
View File
@@ -222,7 +222,7 @@ type ReaderJSON struct {
OnBookshelf bool `json:"onBookshelf"`
}
//go:embed web/dist
//go:embed web
var embeddedWeb embed.FS
//go:embed fonts
@@ -327,7 +327,7 @@ func main() {
} else {
log.Fatalln("Not found KEYS_PATH env")
}
priv, pub := loadOrGenerateKeys(keysPath+"/private.pem", keysPath+"/public.pem")
priv, pub := loadOrGenerateKeys(keysPath)
if len(os.Args) == 3 && os.Args[1] == "--authors-whitelist" {
GenWhitelist(db, os.Args[2])
@@ -342,8 +342,8 @@ func main() {
router := gin.Default()
// router.Use(cors.Default())
//router.StaticFileFS("/", "/web/dist/", http.FS(embeddedWeb))
rootFS, _ := fs.Sub(embeddedWeb, "web/dist")
//router.StaticFileFS("/", "/web/", http.FS(embeddedWeb))
rootFS, _ := fs.Sub(embeddedWeb, "web")
router.NoRoute(func(ctx *gin.Context) {
_, err := rootFS.Open(strings.TrimLeft(ctx.Request.URL.Path, "/"))
@@ -1321,19 +1321,22 @@ func (APICollection) TableName() string {
return "collections"
}
func loadOrGenerateKeys(privateKeyFile string, publicKeyFile string) (*rsa.PrivateKey, *rsa.PublicKey) {
_, errPrivate := os.Stat(privateKeyFile)
_, errPublic := os.Stat(publicKeyFile)
func loadOrGenerateKeys(keysPath string) (*rsa.PrivateKey, *rsa.PublicKey) {
privatePath := keysPath + "/private.pem"
publicPath := keysPath + "/public.pem"
_, errPrivate := os.Stat(privatePath)
_, errPublic := os.Stat(publicPath)
if errors.Is(errPrivate, os.ErrNotExist) || errors.Is(errPublic, os.ErrNotExist) {
log.Println("Generating JWT keys...")
os.MkdirAll(keysPath, os.ModePerm)
priv, _ := rsa.GenerateKey(rand.Reader, 2048)
privBytes := x509.MarshalPKCS1PrivateKey(priv)
err := os.WriteFile(privateKeyFile, pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: privBytes}), 0600)
err := os.WriteFile(privatePath, pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: privBytes}), 0600)
if err != nil {
log.Panicln("Unable to write private JWT key")
}
pubBytes, _ := x509.MarshalPKIXPublicKey(&priv.PublicKey)
err = os.WriteFile(publicKeyFile, pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}), 0644)
err = os.WriteFile(publicPath, pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}), 0644)
if err != nil {
log.Panicln("Unable to write public JWT key")
}
@@ -1343,11 +1346,11 @@ func loadOrGenerateKeys(privateKeyFile string, publicKeyFile string) (*rsa.Priva
log.Panicln("Something went wrong with key files")
}
privPem, _ := os.ReadFile(privateKeyFile)
privPem, _ := os.ReadFile(privatePath)
privBlock, _ := pem.Decode(privPem)
priv, _ := x509.ParsePKCS1PrivateKey(privBlock.Bytes)
pubPem, _ := os.ReadFile(publicKeyFile)
pubPem, _ := os.ReadFile(publicPath)
pubBlock, _ := pem.Decode(pubPem)
pubRaw, _ := x509.ParsePKIXPublicKey(pubBlock.Bytes)