From c51e5eea93ca85dbf359b9017ff022d804c259b7 Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Sat, 2 Sep 2023 04:04:26 +0200 Subject: [PATCH] Tweaked project structure --- .gitattributes | 2 + Makefile | 2 +- fs_embed.go | 8 --- fs_os.go | 7 --- internal/{models => db}/admin_setting.go | 2 +- internal/{models => db}/db.go | 2 +- internal/{models => db}/gist.go | 2 +- internal/{models => db}/migration.go | 2 +- internal/{models => db}/sshkey.go | 2 +- internal/{models => db}/user.go | 2 +- internal/ssh/git_ssh.go | 10 ++-- internal/ssh/run.go | 4 +- internal/web/admin.go | 26 +++++----- internal/web/auth.go | 20 +++---- internal/web/gist.go | 66 ++++++++++++------------ internal/web/git_http.go | 8 +-- internal/web/run.go | 25 ++++----- internal/web/settings.go | 8 +-- internal/web/util.go | 8 +-- opengist.go | 6 +-- public/fs_embed.go | 8 +++ public/fs_os.go | 7 +++ public/main.ts | 5 +- public/{hljs.scss => style.scss} | 0 templates/fs_embed.go | 6 +++ 25 files changed, 122 insertions(+), 116 deletions(-) delete mode 100644 fs_embed.go delete mode 100644 fs_os.go rename internal/{models => db}/admin_setting.go (98%) rename internal/{models => db}/db.go (99%) rename internal/{models => db}/gist.go (99%) rename internal/{models => db}/migration.go (99%) rename internal/{models => db}/sshkey.go (99%) rename internal/{models => db}/user.go (99%) create mode 100644 public/fs_embed.go create mode 100644 public/fs_os.go rename public/{hljs.scss => style.scss} (100%) create mode 100644 templates/fs_embed.go diff --git a/.gitattributes b/.gitattributes index 77d15c59..bf3cf2ec 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,4 @@ templates/**/* linguist-vendored public/**/*.css linguist-vendored +public/**/*.scss linguist-vendored +*.config.js linguist-vendored diff --git a/Makefile b/Makefile index 447a0759..bf29fb3a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # Specify the name of your Go binary output BINARY_NAME := opengist -all: install build +all: clean install build install: @echo "Installing NPM dependencies..." diff --git a/fs_embed.go b/fs_embed.go deleted file mode 100644 index 30d7fd40..00000000 --- a/fs_embed.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build fs_embed - -package main - -import "embed" - -//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg public/assets/*.png -var dirFS embed.FS diff --git a/fs_os.go b/fs_os.go deleted file mode 100644 index a608301c..00000000 --- a/fs_os.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !fs_embed - -package main - -import "os" - -var dirFS = os.DirFS(".") diff --git a/internal/models/admin_setting.go b/internal/db/admin_setting.go similarity index 98% rename from internal/models/admin_setting.go rename to internal/db/admin_setting.go index e23e9380..7fb8cb49 100644 --- a/internal/models/admin_setting.go +++ b/internal/db/admin_setting.go @@ -1,4 +1,4 @@ -package models +package db import ( "gorm.io/gorm/clause" diff --git a/internal/models/db.go b/internal/db/db.go similarity index 99% rename from internal/models/db.go rename to internal/db/db.go index 8605a3ca..c995e363 100644 --- a/internal/models/db.go +++ b/internal/db/db.go @@ -1,4 +1,4 @@ -package models +package db import ( "errors" diff --git a/internal/models/gist.go b/internal/db/gist.go similarity index 99% rename from internal/models/gist.go rename to internal/db/gist.go index 84e7591b..9aad3da3 100644 --- a/internal/models/gist.go +++ b/internal/db/gist.go @@ -1,4 +1,4 @@ -package models +package db import ( "github.com/thomiceli/opengist/internal/git" diff --git a/internal/models/migration.go b/internal/db/migration.go similarity index 99% rename from internal/models/migration.go rename to internal/db/migration.go index 3240bf03..907e2b30 100644 --- a/internal/models/migration.go +++ b/internal/db/migration.go @@ -1,4 +1,4 @@ -package models +package db import ( "fmt" diff --git a/internal/models/sshkey.go b/internal/db/sshkey.go similarity index 99% rename from internal/models/sshkey.go rename to internal/db/sshkey.go index 2dcdabc6..ee62151c 100644 --- a/internal/models/sshkey.go +++ b/internal/db/sshkey.go @@ -1,4 +1,4 @@ -package models +package db import ( "crypto/sha256" diff --git a/internal/models/user.go b/internal/db/user.go similarity index 99% rename from internal/models/user.go rename to internal/db/user.go index 9208d0db..f73b89bf 100644 --- a/internal/models/user.go +++ b/internal/db/user.go @@ -1,4 +1,4 @@ -package models +package db import ( "gorm.io/gorm" diff --git a/internal/ssh/git_ssh.go b/internal/ssh/git_ssh.go index e8ade427..7847b170 100644 --- a/internal/ssh/git_ssh.go +++ b/internal/ssh/git_ssh.go @@ -3,8 +3,8 @@ package ssh import ( "errors" "github.com/rs/zerolog/log" + "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/git" - "github.com/thomiceli/opengist/internal/models" "golang.org/x/crypto/ssh" "gorm.io/gorm" "io" @@ -32,12 +32,12 @@ func runGitCommand(ch ssh.Channel, gitCmd string, key string, ip string) error { userName := strings.ToLower(repoFields[0]) gistName := strings.TrimSuffix(strings.ToLower(repoFields[1]), ".git") - gist, err := models.GetGist(userName, gistName) + gist, err := db.GetGist(userName, gistName) if err != nil { return errors.New("gist not found") } - requireLogin, err := models.GetSetting(models.SettingRequireLogin) + requireLogin, err := db.GetSetting(db.SettingRequireLogin) if err != nil { return errors.New("internal server error") } @@ -52,7 +52,7 @@ func runGitCommand(ch ssh.Channel, gitCmd string, key string, ip string) error { gist.ID == 0 || requireLogin == "1" { - pubKey, err := models.SSHKeyExistsForUser(key, gist.UserID) + pubKey, err := db.SSHKeyExistsForUser(key, gist.UserID) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { log.Warn().Msg("Invalid SSH authentication attempt from " + ip) @@ -61,7 +61,7 @@ func runGitCommand(ch ssh.Channel, gitCmd string, key string, ip string) error { errorSsh("Failed to get user by SSH key id", err) return errors.New("internal server error") } - _ = models.SSHKeyLastUsedNow(pubKey.Content) + _ = db.SSHKeyLastUsedNow(pubKey.Content) } repositoryPath := git.RepositoryPath(gist.User.Username, gist.Uuid) diff --git a/internal/ssh/run.go b/internal/ssh/run.go index c7d3dcd7..d5c4e993 100644 --- a/internal/ssh/run.go +++ b/internal/ssh/run.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/rs/zerolog/log" "github.com/thomiceli/opengist/internal/config" - "github.com/thomiceli/opengist/internal/models" + "github.com/thomiceli/opengist/internal/db" "golang.org/x/crypto/ssh" "gorm.io/gorm" "io" @@ -24,7 +24,7 @@ func Start() { sshConfig := &ssh.ServerConfig{ PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { strKey := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))) - _, err := models.SSHKeyDoesExists(strKey) + _, err := db.SSHKeyDoesExists(strKey) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return nil, err diff --git a/internal/web/admin.go b/internal/web/admin.go index 25d2c103..c218e134 100644 --- a/internal/web/admin.go +++ b/internal/web/admin.go @@ -4,8 +4,8 @@ import ( "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" "github.com/thomiceli/opengist/internal/config" + "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/git" - "github.com/thomiceli/opengist/internal/models" "os" "path/filepath" "runtime" @@ -31,19 +31,19 @@ func adminIndex(ctx echo.Context) error { } setData(ctx, "gitVersion", gitVersion) - countUsers, err := models.CountAll(&models.User{}) + countUsers, err := db.CountAll(&db.User{}) if err != nil { return errorRes(500, "Cannot count users", err) } setData(ctx, "countUsers", countUsers) - countGists, err := models.CountAll(&models.Gist{}) + countGists, err := db.CountAll(&db.Gist{}) if err != nil { return errorRes(500, "Cannot count gists", err) } setData(ctx, "countGists", countGists) - countKeys, err := models.CountAll(&models.SSHKey{}) + countKeys, err := db.CountAll(&db.SSHKey{}) if err != nil { return errorRes(500, "Cannot count SSH keys", err) } @@ -60,9 +60,9 @@ func adminUsers(ctx echo.Context) error { setData(ctx, "adminHeaderPage", "users") pageInt := getPage(ctx) - var data []*models.User + var data []*db.User var err error - if data, err = models.GetAllUsers(pageInt - 1); err != nil { + if data, err = db.GetAllUsers(pageInt - 1); err != nil { return errorRes(500, "Cannot get users", err) } @@ -79,9 +79,9 @@ func adminGists(ctx echo.Context) error { setData(ctx, "adminHeaderPage", "gists") pageInt := getPage(ctx) - var data []*models.Gist + var data []*db.Gist var err error - if data, err = models.GetAllGists(pageInt - 1); err != nil { + if data, err = db.GetAllGists(pageInt - 1); err != nil { return errorRes(500, "Cannot get gists", err) } @@ -94,7 +94,7 @@ func adminGists(ctx echo.Context) error { func adminUserDelete(ctx echo.Context) error { userId, _ := strconv.ParseUint(ctx.Param("user"), 10, 64) - user, err := models.GetUserById(uint(userId)) + user, err := db.GetUserById(uint(userId)) if err != nil { return errorRes(500, "Cannot retrieve user", err) } @@ -108,7 +108,7 @@ func adminUserDelete(ctx echo.Context) error { } func adminGistDelete(ctx echo.Context) error { - gist, err := models.GetGistByID(ctx.Param("gist")) + gist, err := db.GetGistByID(ctx.Param("gist")) if err != nil { return errorRes(500, "Cannot retrieve gist", err) } @@ -133,7 +133,7 @@ func adminSyncReposFromFS(ctx echo.Context) error { } syncReposFromFS = true - gists, err := models.GetAllGistsRows() + gists, err := db.GetAllGistsRows() if err != nil { log.Error().Err(err).Msg("Cannot get gists") syncReposFromFS = false @@ -170,7 +170,7 @@ func adminSyncReposFromDB(ctx echo.Context) error { for _, e := range entries { path := strings.Split(e, string(os.PathSeparator)) - gist, _ := models.GetGist(path[len(path)-2], path[len(path)-1]) + 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 { @@ -197,7 +197,7 @@ func adminSetConfig(ctx echo.Context) error { key := ctx.FormValue("key") value := ctx.FormValue("value") - if err := models.UpdateSetting(key, value); err != nil { + if err := db.UpdateSetting(key, value); err != nil { return errorRes(500, "Cannot set setting", err) } diff --git a/internal/web/auth.go b/internal/web/auth.go index a5a4e9df..8af6083f 100644 --- a/internal/web/auth.go +++ b/internal/web/auth.go @@ -18,7 +18,7 @@ import ( "github.com/markbates/goth/providers/github" "github.com/rs/zerolog/log" "github.com/thomiceli/opengist/internal/config" - "github.com/thomiceli/opengist/internal/models" + "github.com/thomiceli/opengist/internal/db" "golang.org/x/text/cases" "golang.org/x/text/language" "gorm.io/gorm" @@ -47,7 +47,7 @@ func processRegister(ctx echo.Context) error { sess := getSession(ctx) - dto := new(models.UserDTO) + dto := new(db.UserDTO) if err := ctx.Bind(dto); err != nil { return errorRes(400, "Cannot bind data", err) } @@ -57,7 +57,7 @@ func processRegister(ctx echo.Context) error { return html(ctx, "auth_form.html") } - if exists, err := models.UserExists(dto.Username); err != nil || exists { + if exists, err := db.UserExists(dto.Username); err != nil || exists { addFlash(ctx, "Username already exists", "error") return html(ctx, "auth_form.html") } @@ -101,15 +101,15 @@ func processLogin(ctx echo.Context) error { var err error sess := getSession(ctx) - dto := &models.UserDTO{} + dto := &db.UserDTO{} if err = ctx.Bind(dto); err != nil { return errorRes(400, "Cannot bind data", err) } password := dto.Password - var user *models.User + var user *db.User - if user, err = models.GetUserByUsername(dto.Username); err != nil { + if user, err = db.GetUserByUsername(dto.Username); err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return errorRes(500, "Cannot get user", err) } @@ -161,7 +161,7 @@ func oauthCallback(ctx echo.Context) error { } // if user is not in database, create it - userDB, err := models.GetUserByProvider(user.UserID, user.Provider) + userDB, err := db.GetUserByProvider(user.UserID, user.Provider) if err != nil { if getData(ctx, "DisableSignup") == true { return errorRes(403, "Signing up is disabled", nil) @@ -171,7 +171,7 @@ func oauthCallback(ctx echo.Context) error { return errorRes(500, "Cannot get user", err) } - userDB = &models.User{ + userDB = &db.User{ Username: user.NickName, Email: user.Email, MD5Hash: fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(user.Email))))), @@ -188,7 +188,7 @@ func oauthCallback(ctx echo.Context) error { } if err = userDB.Create(); err != nil { - if models.IsUniqueConstraintViolation(err) { + if db.IsUniqueConstraintViolation(err) { addFlash(ctx, "Username "+user.NickName+" already exists in Opengist", "error") return redirect(ctx, "/login") } @@ -224,7 +224,7 @@ func oauthCallback(ctx echo.Context) error { keys = keys[:len(keys)-1] } for _, key := range keys { - sshKey := models.SSHKey{ + sshKey := db.SSHKey{ Title: "Added from " + user.Provider, Content: key, User: *userDB, diff --git a/internal/web/gist.go b/internal/web/gist.go index 6a44623c..80c1887e 100644 --- a/internal/web/gist.go +++ b/internal/web/gist.go @@ -7,7 +7,7 @@ import ( "github.com/google/uuid" "github.com/labstack/echo/v4" "github.com/thomiceli/opengist/internal/config" - "github.com/thomiceli/opengist/internal/models" + "github.com/thomiceli/opengist/internal/db" "gorm.io/gorm" "html/template" "net/url" @@ -23,7 +23,7 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc { gistName = strings.TrimSuffix(gistName, ".git") - gist, err := models.GetGist(userName, gistName) + gist, err := db.GetGist(userName, gistName) if err != nil { return notFound("Gist not found") } @@ -97,7 +97,7 @@ func gistSoftInit(next echo.HandlerFunc) echo.HandlerFunc { gistName = strings.TrimSuffix(gistName, ".git") - gist, _ := models.GetGist(userName, gistName) + gist, _ := db.GetGist(userName, gistName) setData(ctx, "gist", gist) return next(ctx) @@ -128,7 +128,7 @@ func allGists(ctx echo.Context) error { setData(ctx, "sort", sort) setData(ctx, "order", orderText) - var gists []*models.Gist + var gists []*db.Gist var currentUserId uint if userLogged != nil { currentUserId = userLogged.ID @@ -144,12 +144,12 @@ func allGists(ctx echo.Context) error { setData(ctx, "searchQuery", ctx.QueryParam("q")) setData(ctx, "searchQueryUrl", template.URL("&q="+ctx.QueryParam("q"))) urlPage = "search" - gists, err = models.GetAllGistsFromSearch(currentUserId, ctx.QueryParam("q"), pageInt-1, sort, order) + gists, err = db.GetAllGistsFromSearch(currentUserId, ctx.QueryParam("q"), pageInt-1, sort, order) } else if strings.HasSuffix(urlctx, "all") { setData(ctx, "htmlTitle", "All gists") setData(ctx, "mode", "all") urlPage = "all" - gists, err = models.GetAllGistsForCurrentUser(currentUserId, pageInt-1, sort, order) + gists, err = db.GetAllGistsForCurrentUser(currentUserId, pageInt-1, sort, order) } } else { liked := false @@ -165,9 +165,9 @@ func allGists(ctx echo.Context) error { return errorRes(500, "Error matching regexp", err) } - var fromUser *models.User + var fromUser *db.User - fromUser, err = models.GetUserByUsername(fromUserStr) + fromUser, err = db.GetUserByUsername(fromUserStr) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return notFound("User not found") @@ -176,19 +176,19 @@ func allGists(ctx echo.Context) error { } setData(ctx, "fromUser", fromUser) - if countFromUser, err := models.CountAllGistsFromUser(fromUser.ID, currentUserId); err != nil { + if countFromUser, err := db.CountAllGistsFromUser(fromUser.ID, currentUserId); err != nil { return errorRes(500, "Error counting gists", err) } else { setData(ctx, "countFromUser", countFromUser) } - if countLiked, err := models.CountAllGistsLikedByUser(fromUser.ID, currentUserId); err != nil { + if countLiked, err := db.CountAllGistsLikedByUser(fromUser.ID, currentUserId); err != nil { return errorRes(500, "Error counting liked gists", err) } else { setData(ctx, "countLiked", countLiked) } - if countForked, err := models.CountAllGistsForkedByUser(fromUser.ID, currentUserId); err != nil { + if countForked, err := db.CountAllGistsForkedByUser(fromUser.ID, currentUserId); err != nil { return errorRes(500, "Error counting forked gists", err) } else { setData(ctx, "countForked", countForked) @@ -198,17 +198,17 @@ func allGists(ctx echo.Context) error { urlPage = fromUserStr + "/liked" setData(ctx, "htmlTitle", "All gists liked by "+fromUserStr) setData(ctx, "mode", "liked") - gists, err = models.GetAllGistsLikedByUser(fromUser.ID, currentUserId, pageInt-1, sort, order) + gists, err = db.GetAllGistsLikedByUser(fromUser.ID, currentUserId, pageInt-1, sort, order) } else if forked { urlPage = fromUserStr + "/forked" setData(ctx, "htmlTitle", "All gists forked by "+fromUserStr) setData(ctx, "mode", "forked") - gists, err = models.GetAllGistsForkedByUser(fromUser.ID, currentUserId, pageInt-1, sort, order) + gists, err = db.GetAllGistsForkedByUser(fromUser.ID, currentUserId, pageInt-1, sort, order) } else { urlPage = fromUserStr setData(ctx, "htmlTitle", "All gists from "+fromUserStr) setData(ctx, "mode", "fromUser") - gists, err = models.GetAllGistsFromUser(fromUser.ID, currentUserId, pageInt-1, sort, order) + gists, err = db.GetAllGistsFromUser(fromUser.ID, currentUserId, pageInt-1, sort, order) } } @@ -225,7 +225,7 @@ func allGists(ctx echo.Context) error { } func gistIndex(ctx echo.Context) error { - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) revision := ctx.Param("revision") if revision == "" { @@ -250,7 +250,7 @@ func gistIndex(ctx echo.Context) error { } func revisions(ctx echo.Context) error { - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) userName := gist.User.Username gistName := gist.Uuid @@ -273,7 +273,7 @@ func revisions(ctx echo.Context) error { emailsSet[strings.ToLower(commit.AuthorEmail)] = struct{}{} } - emailsUsers, err := models.GetUsersFromEmails(emailsSet) + emailsUsers, err := db.GetUsersFromEmails(emailsSet) if err != nil { return errorRes(500, "Error fetching users emails", err) } @@ -302,13 +302,13 @@ func processCreate(ctx echo.Context) error { return errorRes(400, "Bad request", err) } - dto := new(models.GistDTO) - var gist *models.Gist + dto := new(db.GistDTO) + var gist *db.Gist if isCreate { setData(ctx, "htmlTitle", "Create a new gist") } else { - gist = getData(ctx, "gist").(*models.Gist) + gist = getData(ctx, "gist").(*db.Gist) setData(ctx, "htmlTitle", "Edit "+gist.Title) } @@ -316,7 +316,7 @@ func processCreate(ctx echo.Context) error { return errorRes(400, "Cannot bind data", err) } - dto.Files = make([]models.FileDTO, 0) + dto.Files = make([]db.FileDTO, 0) fileCounter := 0 for i := 0; i < len(ctx.Request().PostForm["content"]); i++ { name := ctx.Request().PostForm["name"][i] @@ -332,7 +332,7 @@ func processCreate(ctx echo.Context) error { return errorRes(400, "Invalid character unescaped", err) } - dto.Files = append(dto.Files, models.FileDTO{ + dto.Files = append(dto.Files, db.FileDTO{ Filename: strings.Trim(name, " "), Content: escapedValue, }) @@ -414,7 +414,7 @@ func processCreate(ctx echo.Context) error { } func toggleVisibility(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) gist.Private = (gist.Private + 1) % 3 if err := gist.Update(); err != nil { @@ -426,7 +426,7 @@ func toggleVisibility(ctx echo.Context) error { } func deleteGist(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) err := gist.DeleteRepository() if err != nil { @@ -442,7 +442,7 @@ func deleteGist(ctx echo.Context) error { } func like(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) currentUser := getUserLogged(ctx) hasLiked, err := currentUser.HasLiked(gist) @@ -468,7 +468,7 @@ func like(ctx echo.Context) error { } func fork(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) currentUser := getUserLogged(ctx) alreadyForked, err := gist.GetForkParent(currentUser) @@ -490,7 +490,7 @@ func fork(ctx echo.Context) error { return errorRes(500, "Error creating an UUID", err) } - newGist := &models.Gist{ + newGist := &db.Gist{ Uuid: strings.Replace(uuidGist.String(), "-", "", -1), Title: gist.Title, Preview: gist.Preview, @@ -519,7 +519,7 @@ func fork(ctx echo.Context) error { } func rawFile(ctx echo.Context) error { - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) file, err := gist.File(ctx.Param("revision"), ctx.Param("file"), false) if err != nil { @@ -534,7 +534,7 @@ func rawFile(ctx echo.Context) error { } func downloadFile(ctx echo.Context) error { - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) file, err := gist.File(ctx.Param("revision"), ctx.Param("file"), false) if err != nil { @@ -558,7 +558,7 @@ func downloadFile(ctx echo.Context) error { } func edit(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) files, err := gist.Files("HEAD") if err != nil { @@ -572,7 +572,7 @@ func edit(ctx echo.Context) error { } func downloadZip(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) var revision = ctx.Param("revision") files, err := gist.Files(revision) @@ -617,7 +617,7 @@ func downloadZip(ctx echo.Context) error { } func likes(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) pageInt := getPage(ctx) @@ -636,7 +636,7 @@ func likes(ctx echo.Context) error { } func forks(ctx echo.Context) error { - var gist = getData(ctx, "gist").(*models.Gist) + var gist = getData(ctx, "gist").(*db.Gist) pageInt := getPage(ctx) currentUser := getUserLogged(ctx) diff --git a/internal/web/git_http.go b/internal/web/git_http.go index 61b01a04..661e6527 100644 --- a/internal/web/git_http.go +++ b/internal/web/git_http.go @@ -7,8 +7,8 @@ import ( "fmt" "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" + "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/git" - "github.com/thomiceli/opengist/internal/models" "net/http" "os" "os/exec" @@ -45,7 +45,7 @@ func gitHttp(ctx echo.Context) error { continue } - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) // Shows basic auth if : // - user wants to push the gist @@ -148,7 +148,7 @@ func pack(ctx echo.Context, serviceType string) error { // updatedAt is updated only if serviceType is receive-pack if serviceType == "receive-pack" { - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) _ = gist.SetLastActiveNow() _ = gist.UpdatePreviewAndCount() } @@ -159,7 +159,7 @@ func infoRefs(ctx echo.Context) error { noCacheHeaders(ctx) var service string - gist := getData(ctx, "gist").(*models.Gist) + gist := getData(ctx, "gist").(*db.Gist) serviceType := ctx.QueryParam("service") if strings.HasPrefix(serviceType, "git-") { diff --git a/internal/web/run.go b/internal/web/run.go index 3b96b284..169889ad 100644 --- a/internal/web/run.go +++ b/internal/web/run.go @@ -10,11 +10,12 @@ import ( "github.com/markbates/goth/gothic" "github.com/rs/zerolog/log" "github.com/thomiceli/opengist/internal/config" + "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/git" - "github.com/thomiceli/opengist/internal/models" + "github.com/thomiceli/opengist/public" + "github.com/thomiceli/opengist/templates" "html/template" "io" - "io/fs" "net/http" "os" "path/filepath" @@ -70,7 +71,7 @@ var fm = template.FuncMap{ "slug": func(s string) string { return strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-") }, - "avatarUrl": func(user *models.User, noGravatar bool) string { + "avatarUrl": func(user *db.User, noGravatar bool) string { if user.AvatarURL != "" { return user.AvatarURL } @@ -104,8 +105,6 @@ var fm = template.FuncMap{ }, } -var EmbedFS fs.FS - type Template struct { templates *template.Template } @@ -118,8 +117,6 @@ func Start() { store = sessions.NewCookieStore([]byte("opengist")) gothic.Store = store - assetsFS := echo.MustSubFS(EmbedFS, "public/assets") - e := echo.New() e.HideBanner = true e.HidePort = true @@ -143,7 +140,7 @@ func Start() { e.Use(middleware.Secure()) e.Renderer = &Template{ - templates: template.Must(template.New("t").Funcs(fm).ParseFS(EmbedFS, "templates/*/*.html")), + templates: template.Must(template.New("t").Funcs(fm).ParseFS(templates.Files, "*/*.html")), } e.HTTPErrorHandler = func(er error, ctx echo.Context) { if err, ok := er.(*echo.HTTPError); ok { @@ -166,7 +163,7 @@ func Start() { if !dev { parseManifestEntries() - e.GET("/assets/*", cacheControl(echo.WrapHandler(http.StripPrefix("/assets", http.FileServer(http.FS(assetsFS)))))) + e.GET("/assets/*", cacheControl(echo.WrapHandler(http.FileServer(http.FS(public.Files))))) } // Web based routes @@ -285,9 +282,9 @@ func sessionInit(next echo.HandlerFunc) echo.HandlerFunc { sess := getSession(ctx) if sess.Values["user"] != nil { var err error - var user *models.User + var user *db.User - if user, err = models.GetUserById(sess.Values["user"].(uint)); err != nil { + if user, err = db.GetUserById(sess.Values["user"].(uint)); err != nil { sess.Values["user"] = nil saveSession(sess, ctx) setData(ctx, "userLogged", nil) @@ -315,8 +312,8 @@ func writePermission(next echo.HandlerFunc) echo.HandlerFunc { return func(ctx echo.Context) error { gist := getData(ctx, "gist") user := getUserLogged(ctx) - if !gist.(*models.Gist).CanWrite(user) { - return redirect(ctx, "/"+gist.(*models.Gist).User.Username+"/"+gist.(*models.Gist).Uuid) + if !gist.(*db.Gist).CanWrite(user) { + return redirect(ctx, "/"+gist.(*db.Gist).User.Username+"/"+gist.(*db.Gist).Uuid) } return next(ctx) } @@ -377,7 +374,7 @@ type Asset struct { var manifestEntries map[string]Asset func parseManifestEntries() { - file, err := EmbedFS.Open("public/manifest.json") + file, err := public.Files.Open("manifest.json") if err != nil { log.Fatal().Err(err).Msg("Failed to open manifest.json") } diff --git a/internal/web/settings.go b/internal/web/settings.go index fd6f0d3a..4056ba0d 100644 --- a/internal/web/settings.go +++ b/internal/web/settings.go @@ -4,7 +4,7 @@ import ( "crypto/md5" "fmt" "github.com/labstack/echo/v4" - "github.com/thomiceli/opengist/internal/models" + "github.com/thomiceli/opengist/internal/db" "golang.org/x/crypto/ssh" "strconv" "strings" @@ -14,7 +14,7 @@ import ( func userSettings(ctx echo.Context) error { user := getUserLogged(ctx) - keys, err := models.GetSSHKeysByUserID(user.ID) + keys, err := db.GetSSHKeysByUserID(user.ID) if err != nil { return errorRes(500, "Cannot get SSH keys", err) } @@ -61,7 +61,7 @@ func accountDeleteProcess(ctx echo.Context) error { func sshKeysProcess(ctx echo.Context) error { user := getUserLogged(ctx) - var dto = new(models.SSHKeyDTO) + var dto = new(db.SSHKeyDTO) if err := ctx.Bind(dto); err != nil { return errorRes(400, "Cannot bind data", err) } @@ -97,7 +97,7 @@ func sshKeysDelete(ctx echo.Context) error { return redirect(ctx, "/settings") } - key, err := models.GetSSHKeyByID(uint(keyId)) + key, err := db.GetSSHKeyByID(uint(keyId)) if err != nil || key.UserID != user.ID { return redirect(ctx, "/settings") diff --git a/internal/web/util.go b/internal/web/util.go index 8f6fbd2b..da0e94a2 100644 --- a/internal/web/util.go +++ b/internal/web/util.go @@ -11,7 +11,7 @@ import ( "github.com/gorilla/sessions" "github.com/labstack/echo/v4" "github.com/thomiceli/opengist/internal/config" - "github.com/thomiceli/opengist/internal/models" + "github.com/thomiceli/opengist/internal/db" "golang.org/x/crypto/argon2" "html/template" "net/http" @@ -60,10 +60,10 @@ func errorRes(code int, message string, err error) error { return &echo.HTTPError{Code: code, Message: message, Internal: err} } -func getUserLogged(ctx echo.Context) *models.User { +func getUserLogged(ctx echo.Context) *db.User { user := getData(ctx, "userLogged") if user != nil { - return user.(*models.User) + return user.(*db.User) } return nil } @@ -110,7 +110,7 @@ func deleteCsrfCookie(ctx echo.Context) { } func loadSettings(ctx echo.Context) error { - settings, err := models.GetSettings() + settings, err := db.GetSettings() if err != nil { return err } diff --git a/opengist.go b/opengist.go index 9bd0a9d5..dbf3647a 100644 --- a/opengist.go +++ b/opengist.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/rs/zerolog/log" "github.com/thomiceli/opengist/internal/config" + "github.com/thomiceli/opengist/internal/db" "github.com/thomiceli/opengist/internal/git" - "github.com/thomiceli/opengist/internal/models" "github.com/thomiceli/opengist/internal/ssh" "github.com/thomiceli/opengist/internal/web" "os" @@ -51,11 +51,9 @@ func initialize() { } log.Info().Msg("Database file: " + filepath.Join(homePath, config.C.DBFilename)) - if err := models.Setup(filepath.Join(homePath, config.C.DBFilename)); err != nil { + if err := db.Setup(filepath.Join(homePath, config.C.DBFilename)); err != nil { log.Fatal().Err(err).Msg("Failed to initialize database") } - - web.EmbedFS = dirFS } func main() { diff --git a/public/fs_embed.go b/public/fs_embed.go new file mode 100644 index 00000000..330456d0 --- /dev/null +++ b/public/fs_embed.go @@ -0,0 +1,8 @@ +//go:build fs_embed + +package public + +import "embed" + +//go:embed manifest.json assets/*.js assets/*.css assets/*.svg assets/*.png +var Files embed.FS diff --git a/public/fs_os.go b/public/fs_os.go new file mode 100644 index 00000000..e5ca954b --- /dev/null +++ b/public/fs_os.go @@ -0,0 +1,7 @@ +//go:build !fs_embed + +package public + +import "os" + +var Files = os.DirFS(".") diff --git a/public/main.ts b/public/main.ts index 0adb138f..80d1a54c 100644 --- a/public/main.ts +++ b/public/main.ts @@ -1,5 +1,5 @@ import './style.css'; -import './hljs.scss'; +import './style.scss'; import './favicon.svg'; import './default.png'; import moment from 'moment'; @@ -14,6 +14,7 @@ document.addEventListener('DOMContentLoaded', () => { e.stopPropagation() localStorage.theme = 'light'; themeMenu.classList.toggle('hidden'); + // @ts-ignore checkTheme() } @@ -21,6 +22,7 @@ document.addEventListener('DOMContentLoaded', () => { e.stopPropagation() localStorage.theme = 'dark'; themeMenu.classList.toggle('hidden'); + // @ts-ignore checkTheme() } @@ -28,6 +30,7 @@ document.addEventListener('DOMContentLoaded', () => { e.stopPropagation() localStorage.removeItem('theme'); themeMenu.classList.toggle('hidden'); + // @ts-ignore checkTheme(); } diff --git a/public/hljs.scss b/public/style.scss similarity index 100% rename from public/hljs.scss rename to public/style.scss diff --git a/templates/fs_embed.go b/templates/fs_embed.go new file mode 100644 index 00000000..b2f70acd --- /dev/null +++ b/templates/fs_embed.go @@ -0,0 +1,6 @@ +package templates + +import "embed" + +//go:embed */*.html +var Files embed.FS