1
0
mirror of https://github.com/thomiceli/opengist synced 2024-11-12 07:05:50 +01:00
opengist/internal/web/admin.go

227 lines
5.7 KiB
Go
Raw Normal View History

2023-03-14 16:22:52 +01:00
package web
import (
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
2023-05-15 21:07:29 +02:00
"github.com/thomiceli/opengist/internal/config"
2023-09-03 00:30:57 +02:00
"github.com/thomiceli/opengist/internal/db"
2023-05-15 21:07:29 +02:00
"github.com/thomiceli/opengist/internal/git"
"os"
"path/filepath"
2023-03-14 16:22:52 +01:00
"runtime"
2023-03-17 14:56:39 +01:00
"strconv"
"strings"
)
var (
syncReposFromFS = false
syncReposFromDB = false
2023-09-04 11:11:54 +02:00
gitGcRepos = false
2023-03-14 16:22:52 +01:00
)
func adminIndex(ctx echo.Context) error {
setData(ctx, "title", "Admin panel")
2023-04-17 22:13:19 +02:00
setData(ctx, "htmlTitle", "Admin panel")
2023-03-14 16:22:52 +01:00
setData(ctx, "adminHeaderPage", "index")
setData(ctx, "opengistVersion", config.OpengistVersion)
setData(ctx, "goVersion", runtime.Version())
gitVersion, err := git.GetGitVersion()
if err != nil {
return errorRes(500, "Cannot get git version", err)
}
setData(ctx, "gitVersion", gitVersion)
2023-09-03 00:30:57 +02:00
countUsers, err := db.CountAll(&db.User{})
2023-03-14 16:22:52 +01:00
if err != nil {
return errorRes(500, "Cannot count users", err)
}
setData(ctx, "countUsers", countUsers)
2023-09-03 00:30:57 +02:00
countGists, err := db.CountAll(&db.Gist{})
2023-03-14 16:22:52 +01:00
if err != nil {
return errorRes(500, "Cannot count gists", err)
}
setData(ctx, "countGists", countGists)
2023-09-03 00:30:57 +02:00
countKeys, err := db.CountAll(&db.SSHKey{})
2023-03-14 16:22:52 +01:00
if err != nil {
return errorRes(500, "Cannot count SSH keys", err)
}
setData(ctx, "countKeys", countKeys)
setData(ctx, "syncReposFromFS", syncReposFromFS)
setData(ctx, "syncReposFromDB", syncReposFromDB)
2023-09-04 11:11:54 +02:00
setData(ctx, "gitGcRepos", gitGcRepos)
2023-03-14 16:22:52 +01:00
return html(ctx, "admin_index.html")
}
func adminUsers(ctx echo.Context) error {
setData(ctx, "title", "Users")
2023-04-17 22:13:19 +02:00
setData(ctx, "htmlTitle", "Users - Admin panel")
2023-03-14 16:22:52 +01:00
setData(ctx, "adminHeaderPage", "users")
pageInt := getPage(ctx)
2023-09-03 00:30:57 +02:00
var data []*db.User
2023-03-14 16:22:52 +01:00
var err error
2023-09-03 00:30:57 +02:00
if data, err = db.GetAllUsers(pageInt - 1); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(500, "Cannot get users", err)
}
2023-04-17 00:35:04 +02:00
if err = paginate(ctx, data, pageInt, 10, "data", "admin-panel/users", 1); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(404, "Page not found", nil)
}
return html(ctx, "admin_users.html")
}
func adminGists(ctx echo.Context) error {
2023-04-17 22:13:19 +02:00
setData(ctx, "title", "Gists")
setData(ctx, "htmlTitle", "Gists - Admin panel")
2023-03-14 16:22:52 +01:00
setData(ctx, "adminHeaderPage", "gists")
pageInt := getPage(ctx)
2023-09-03 00:30:57 +02:00
var data []*db.Gist
2023-03-14 16:22:52 +01:00
var err error
2023-09-03 00:30:57 +02:00
if data, err = db.GetAllGists(pageInt - 1); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(500, "Cannot get gists", err)
}
2023-04-17 00:35:04 +02:00
if err = paginate(ctx, data, pageInt, 10, "data", "admin-panel/gists", 1); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(404, "Page not found", nil)
}
return html(ctx, "admin_gists.html")
}
func adminUserDelete(ctx echo.Context) error {
2023-03-17 14:56:39 +01:00
userId, _ := strconv.ParseUint(ctx.Param("user"), 10, 64)
2023-09-03 00:30:57 +02:00
user, err := db.GetUserById(uint(userId))
2023-03-17 14:56:39 +01:00
if err != nil {
return errorRes(500, "Cannot retrieve user", err)
}
if err := user.Delete(); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(500, "Cannot delete this user", err)
}
addFlash(ctx, "User has been deleted", "success")
return redirect(ctx, "/admin-panel/users")
2023-03-14 16:22:52 +01:00
}
func adminGistDelete(ctx echo.Context) error {
2023-09-03 00:30:57 +02:00
gist, err := db.GetGistByID(ctx.Param("gist"))
2023-03-14 16:22:52 +01:00
if err != nil {
return errorRes(500, "Cannot retrieve gist", err)
}
if err = gist.DeleteRepository(); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(500, "Cannot delete the repository", err)
}
2023-03-17 14:56:39 +01:00
if err = gist.Delete(); err != nil {
2023-03-14 16:22:52 +01:00
return errorRes(500, "Cannot delete this gist", err)
}
addFlash(ctx, "Gist has been deleted", "success")
return redirect(ctx, "/admin-panel/gists")
}
func adminSyncReposFromFS(ctx echo.Context) error {
addFlash(ctx, "Syncing repositories from filesystem...", "success")
go func() {
if syncReposFromFS {
return
}
syncReposFromFS = true
2023-09-03 00:30:57 +02:00
gists, err := db.GetAllGistsRows()
if err != nil {
log.Error().Err(err).Msg("Cannot get gists")
syncReposFromFS = false
return
}
for _, gist := range gists {
// if repository does not exist, delete gist from database
if _, err := os.Stat(git.RepositoryPath(gist.User.Username, gist.Uuid)); err != nil && !os.IsExist(err) {
if err2 := gist.Delete(); err2 != nil {
log.Error().Err(err2).Msg("Cannot delete gist")
syncReposFromFS = false
return
}
}
}
syncReposFromFS = false
}()
return redirect(ctx, "/admin-panel")
}
2023-03-14 16:22:52 +01:00
func adminSyncReposFromDB(ctx echo.Context) error {
addFlash(ctx, "Syncing repositories from database...", "success")
go func() {
if syncReposFromDB {
return
}
syncReposFromDB = true
entries, err := filepath.Glob(filepath.Join(config.GetHomeDir(), "repos", "*", "*"))
if err != nil {
log.Error().Err(err).Msg("Cannot read repos directories")
syncReposFromDB = false
return
}
for _, e := range entries {
path := strings.Split(e, string(os.PathSeparator))
2023-09-03 00:30:57 +02:00
gist, _ := db.GetGist(path[len(path)-2], path[len(path)-1])
if gist.ID == 0 {
if err := git.DeleteRepository(path[len(path)-2], path[len(path)-1]); err != nil {
log.Error().Err(err).Msg("Cannot delete repository")
syncReposFromDB = false
return
}
}
}
syncReposFromDB = false
}()
return redirect(ctx, "/admin-panel")
2023-03-14 16:22:52 +01:00
}
2023-09-04 11:11:54 +02:00
func adminGcRepos(ctx echo.Context) error {
addFlash(ctx, "Garbage collecting repositories...", "success")
go func() {
if gitGcRepos {
return
}
gitGcRepos = true
if err := git.GcRepos(); err != nil {
log.Error().Err(err).Msg("Error garbage collecting repositories")
gitGcRepos = false
return
}
gitGcRepos = false
}()
return redirect(ctx, "/admin-panel")
}
2023-06-07 20:50:30 +02:00
func adminConfig(ctx echo.Context) error {
setData(ctx, "title", "Configuration")
setData(ctx, "htmlTitle", "Configuration - Admin panel")
setData(ctx, "adminHeaderPage", "config")
2023-05-11 15:16:05 +02:00
2023-06-07 20:50:30 +02:00
return html(ctx, "admin_config.html")
2023-05-11 15:16:05 +02:00
}
2023-06-07 20:50:30 +02:00
func adminSetConfig(ctx echo.Context) error {
key := ctx.FormValue("key")
value := ctx.FormValue("value")
2023-09-03 00:30:57 +02:00
if err := db.UpdateSetting(key, value); err != nil {
return errorRes(500, "Cannot set setting", err)
}
return ctx.JSON(200, map[string]interface{}{
"success": true,
})
}