Updated fulgens library, fixed SQL data types

This commit is contained in:
Arzumify 2024-10-15 14:32:13 +01:00
parent d3b0aec1b9
commit a9ede05d37
3 changed files with 48 additions and 28 deletions

2
go.mod
View file

@ -3,7 +3,7 @@ module git.oreonproject.org/oreonproject/eternity-web
go 1.23.1
require (
git.ailur.dev/ailur/fg-library v1.0.0
git.ailur.dev/ailur/fg-library v1.0.1
git.ailur.dev/ailur/fg-nucleus-library v1.0.0
git.oreonproject.org/oreonproject/eternity v1.0.0
github.com/go-git/go-git/v5 v5.12.0

2
go.sum
View file

@ -2,6 +2,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
git.ailur.dev/ailur/fg-library v1.0.0 h1:61RkJW9g4PqAiZFjpwUnx6QUYYSeXJXLjLi1d47NfTA=
git.ailur.dev/ailur/fg-library v1.0.0/go.mod h1:hOUkxs2rRouSwNnNZlo7CsFVH12kmjqheyzPQ4to1N8=
git.ailur.dev/ailur/fg-library v1.0.1 h1:7TY2shmYNfKPzCTeYC80uj+sFZPbBWeOlqKT6ZsKFmc=
git.ailur.dev/ailur/fg-library v1.0.1/go.mod h1:hOUkxs2rRouSwNnNZlo7CsFVH12kmjqheyzPQ4to1N8=
git.ailur.dev/ailur/fg-nucleus-library v1.0.0 h1:TT1V4cfka+uUpvV1zU7bc4KXFkgnsI/sIvaZDDxXk+k=
git.ailur.dev/ailur/fg-nucleus-library v1.0.0/go.mod h1:m4gNSEypfgrUV8bXaR8NLB8zchUM59y0ellV1wp/C+I=
git.oreonproject.org/oreonproject/eternity v1.0.0 h1:EHKJsLI84+L/yFz8yECys0AZCUn0bnGNSoD7KpXzgYY=

72
main.go
View file

@ -82,10 +82,10 @@ func renderJSON(statusCode int, w http.ResponseWriter, data map[string]interface
}
}
func verifySecret(secret string, conn *sql.DB) bool {
func verifySecret(secret string, conn library.Database) bool {
// Check if the secret is in the secrets table
var secretCheck string
err := conn.QueryRow("SELECT secret FROM secrets WHERE secret = ?", secret).Scan(&secretCheck)
err := conn.DB.QueryRow("SELECT secret FROM secrets WHERE secret = $1", secret).Scan(&secretCheck)
if err != nil || secretCheck != secret {
return false
}
@ -93,7 +93,7 @@ func verifySecret(secret string, conn *sql.DB) bool {
return true
}
func verifyJwt(token string, publicKey ed25519.PublicKey, conn *sql.DB) (jwt.MapClaims, bool) {
func verifyJwt(token string, publicKey ed25519.PublicKey, conn library.Database) (jwt.MapClaims, bool) {
parsedToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return publicKey, nil
})
@ -117,9 +117,9 @@ func verifyJwt(token string, publicKey ed25519.PublicKey, conn *sql.DB) (jwt.Map
}
// Check if the token is in users
var subCheck string
err = conn.QueryRow("SELECT id FROM users WHERE id = ?", claims["sub"]).Scan(&subCheck)
if err != nil || subCheck != claims["sub"] {
var idCheck []byte
err = conn.DB.QueryRow("SELECT id FROM users WHERE id = $1", claims["sub"]).Scan(&idCheck)
if err != nil || claims["sub"] != uuid.Must(uuid.FromBytes(idCheck)).String() {
return claims, false
}
@ -127,7 +127,7 @@ func verifyJwt(token string, publicKey ed25519.PublicKey, conn *sql.DB) (jwt.Map
}
func Main(information library.ServiceInitializationInformation) {
var conn *sql.DB
var conn library.Database
gitDir := information.Configuration["gitDir"].(string)
outputDir := information.Configuration["outputDir"].(string)
hostName := information.Configuration["hostName"].(string)
@ -147,21 +147,39 @@ func Main(information library.ServiceInitializationInformation) {
if response.MessageType == 2 {
// This is the connection information
// Set up the database connection
conn = response.Message.(*sql.DB)
// Create the packages table
_, err := conn.Exec("CREATE TABLE IF NOT EXISTS packages (creator BLOB NOT NULL, name BLOB NOT NULL, path STRING NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
// Create the secrets table
_, err = conn.Exec("CREATE TABLE IF NOT EXISTS secrets (secret BLOB NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
// Create the users table
_, err = conn.Exec("CREATE TABLE IF NOT EXISTS users (id BLOB NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
conn = response.Message.(library.Database)
if conn.DBType == library.Sqlite {
// Create the packages table
_, err := conn.DB.Exec("CREATE TABLE IF NOT EXISTS packages (creator BLOB NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
// Create the secrets table
_, err = conn.DB.Exec("CREATE TABLE IF NOT EXISTS secrets (secret BLOB NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
// Create the users table
_, err = conn.DB.Exec("CREATE TABLE IF NOT EXISTS users (id BLOB NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
} else {
// Create the packages table
_, err := conn.DB.Exec("CREATE TABLE IF NOT EXISTS packages (creator BYTEA NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
// Create the secrets table
_, err = conn.DB.Exec("CREATE TABLE IF NOT EXISTS secrets (secret BYTEA NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
// Create the users table
_, err = conn.DB.Exec("CREATE TABLE IF NOT EXISTS users (id BYTEA NOT NULL)")
if err != nil {
logFunc(err.Error(), 3, information)
}
}
} else {
// This is an error message
@ -260,7 +278,7 @@ func Main(information library.ServiceInitializationInformation) {
// Set up the API routes
router.Post("/api/packages/list", func(w http.ResponseWriter, r *http.Request) {
// Get the list of packages
rows, err := conn.Query("SELECT name FROM packages")
rows, err := conn.DB.Query("SELECT name FROM packages")
if err != nil {
renderJSON(500, w, map[string]interface{}{"error": "Internal server error", "code": "01"}, information)
return
@ -337,7 +355,7 @@ func Main(information library.ServiceInitializationInformation) {
}
}
_, err = conn.Exec("INSERT INTO packages (creator, name, path) VALUES (?, ?, ?)", userid, data.Name, data.RepositoryPath)
_, err = conn.DB.Exec("INSERT INTO packages (creator, name, path) VALUES ($1, $2, $3)", userid, data.Name, data.RepositoryPath)
if err != nil {
renderJSON(500, w, map[string]interface{}{"error": "Internal server error", "code": "04"}, information)
return
@ -391,7 +409,7 @@ func Main(information library.ServiceInitializationInformation) {
}
}
_, err = conn.Exec("DELETE FROM packages WHERE creator = ? AND name = ?", userid, data.Name)
_, err = conn.DB.Exec("DELETE FROM packages WHERE creator = $1 AND name = $2", userid, data.Name)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information)
@ -443,7 +461,7 @@ func Main(information library.ServiceInitializationInformation) {
}
var nameCheck string
err = conn.QueryRow("SELECT name FROM packages WHERE name = ?", data.Name).Scan(&nameCheck)
err = conn.DB.QueryRow("SELECT name FROM packages WHERE name = $1", data.Name).Scan(&nameCheck)
if err != nil || nameCheck != data.Name {
if err != nil && errors.Is(err, sql.ErrNoRows) {
renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information)
@ -503,7 +521,7 @@ func Main(information library.ServiceInitializationInformation) {
// Fetch the package from the database
var nameCheck, path string
err = conn.QueryRow("SELECT name, path FROM packages WHERE name = ?", data.Name).Scan(&nameCheck, &path)
err = conn.DB.QueryRow("SELECT name, path FROM packages WHERE name = $1", data.Name).Scan(&nameCheck, &path)
if err != nil || nameCheck != data.Name {
if err != nil && errors.Is(err, sql.ErrNoRows) {
renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information)