Skip to content

Commit

Permalink
Tweaked project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Sep 2, 2023
1 parent 25316d7 commit c51e5ee
Show file tree
Hide file tree
Showing 25 changed files with 122 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
templates/**/* linguist-vendored
public/**/*.css linguist-vendored
public/**/*.scss linguist-vendored
*.config.js linguist-vendored
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down
8 changes: 0 additions & 8 deletions fs_embed.go

This file was deleted.

7 changes: 0 additions & 7 deletions fs_os.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package db

import (
"gorm.io/gorm/clause"
Expand Down
2 changes: 1 addition & 1 deletion internal/models/db.go → internal/db/db.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package db

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion internal/models/gist.go → internal/db/gist.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package db

import (
"github.com/thomiceli/opengist/internal/git"
Expand Down
2 changes: 1 addition & 1 deletion internal/models/migration.go → internal/db/migration.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package db

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/models/sshkey.go → internal/db/sshkey.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package db

import (
"crypto/sha256"
Expand Down
2 changes: 1 addition & 1 deletion internal/models/user.go → internal/db/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package db

import (
"gorm.io/gorm"
Expand Down
10 changes: 5 additions & 5 deletions internal/ssh/git_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/ssh/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
26 changes: 13 additions & 13 deletions internal/web/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand All @@ -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)
}

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

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
20 changes: 10 additions & 10 deletions internal/web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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))))),
Expand All @@ -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")
}
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit c51e5ee

Please sign in to comment.