Tweaked project structure (#88)

This commit is contained in:
Thomas Miceli 2023-09-03 00:30:57 +02:00 committed by GitHub
parent 25316d7bf2
commit a7b346d8df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 122 additions and 116 deletions

2
.gitattributes vendored
View file

@ -1,2 +1,4 @@
templates/**/* linguist-vendored
public/**/*.css linguist-vendored
public/**/*.scss linguist-vendored
*.config.js linguist-vendored

View file

@ -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..."

View file

@ -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

View file

@ -1,7 +0,0 @@
//go:build !fs_embed
package main
import "os"
var dirFS = os.DirFS(".")

View file

@ -1,4 +1,4 @@
package models
package db
import (
"gorm.io/gorm/clause"

View file

@ -1,4 +1,4 @@
package models
package db
import (
"errors"

View file

@ -1,4 +1,4 @@
package models
package db
import (
"github.com/thomiceli/opengist/internal/git"

View file

@ -1,4 +1,4 @@
package models
package db
import (
"fmt"

View file

@ -1,4 +1,4 @@
package models
package db
import (
"crypto/sha256"

View file

@ -1,4 +1,4 @@
package models
package db
import (
"gorm.io/gorm"

View file

@ -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)

View file

@ -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

View file

@ -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)
}

View file

@ -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,

View file

@ -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)

View file

@ -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-") {

View file

@ -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")
}

View file

@ -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")

View file

@ -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
}

View file

@ -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() {

8
public/fs_embed.go Normal file
View file

@ -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

7
public/fs_os.go Normal file
View file

@ -0,0 +1,7 @@
//go:build !fs_embed
package public
import "os"
var Files = os.DirFS(".")

View file

@ -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();
}

6
templates/fs_embed.go vendored Normal file
View file

@ -0,0 +1,6 @@
package templates
import "embed"
//go:embed */*.html
var Files embed.FS