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 go 1.23.1
require ( 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.ailur.dev/ailur/fg-nucleus-library v1.0.0
git.oreonproject.org/oreonproject/eternity v1.0.0 git.oreonproject.org/oreonproject/eternity v1.0.0
github.com/go-git/go-git/v5 v5.12.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= 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 h1:61RkJW9g4PqAiZFjpwUnx6QUYYSeXJXLjLi1d47NfTA=
git.ailur.dev/ailur/fg-library v1.0.0/go.mod h1:hOUkxs2rRouSwNnNZlo7CsFVH12kmjqheyzPQ4to1N8= 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 h1:TT1V4cfka+uUpvV1zU7bc4KXFkgnsI/sIvaZDDxXk+k=
git.ailur.dev/ailur/fg-nucleus-library v1.0.0/go.mod h1:m4gNSEypfgrUV8bXaR8NLB8zchUM59y0ellV1wp/C+I= 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= 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 // Check if the secret is in the secrets table
var secretCheck string 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 { if err != nil || secretCheck != secret {
return false return false
} }
@ -93,7 +93,7 @@ func verifySecret(secret string, conn *sql.DB) bool {
return true 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) { parsedToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return publicKey, nil 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 // Check if the token is in users
var subCheck string var idCheck []byte
err = conn.QueryRow("SELECT id FROM users WHERE id = ?", claims["sub"]).Scan(&subCheck) err = conn.DB.QueryRow("SELECT id FROM users WHERE id = $1", claims["sub"]).Scan(&idCheck)
if err != nil || subCheck != claims["sub"] { if err != nil || claims["sub"] != uuid.Must(uuid.FromBytes(idCheck)).String() {
return claims, false return claims, false
} }
@ -127,7 +127,7 @@ func verifyJwt(token string, publicKey ed25519.PublicKey, conn *sql.DB) (jwt.Map
} }
func Main(information library.ServiceInitializationInformation) { func Main(information library.ServiceInitializationInformation) {
var conn *sql.DB var conn library.Database
gitDir := information.Configuration["gitDir"].(string) gitDir := information.Configuration["gitDir"].(string)
outputDir := information.Configuration["outputDir"].(string) outputDir := information.Configuration["outputDir"].(string)
hostName := information.Configuration["hostName"].(string) hostName := information.Configuration["hostName"].(string)
@ -147,21 +147,39 @@ func Main(information library.ServiceInitializationInformation) {
if response.MessageType == 2 { if response.MessageType == 2 {
// This is the connection information // This is the connection information
// Set up the database connection // Set up the database connection
conn = response.Message.(*sql.DB) conn = response.Message.(library.Database)
// Create the packages table if conn.DBType == library.Sqlite {
_, err := conn.Exec("CREATE TABLE IF NOT EXISTS packages (creator BLOB NOT NULL, name BLOB NOT NULL, path STRING NOT NULL)") // Create the packages table
if err != nil { _, err := conn.DB.Exec("CREATE TABLE IF NOT EXISTS packages (creator BLOB NOT NULL, name TEXT NOT NULL, path TEXT NOT NULL)")
logFunc(err.Error(), 3, information) 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)") // Create the secrets table
if err != nil { _, err = conn.DB.Exec("CREATE TABLE IF NOT EXISTS secrets (secret BLOB NOT NULL)")
logFunc(err.Error(), 3, information) 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)") // Create the users table
if err != nil { _, err = conn.DB.Exec("CREATE TABLE IF NOT EXISTS users (id BLOB NOT NULL)")
logFunc(err.Error(), 3, information) 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 { } else {
// This is an error message // This is an error message
@ -260,7 +278,7 @@ func Main(information library.ServiceInitializationInformation) {
// Set up the API routes // Set up the API routes
router.Post("/api/packages/list", func(w http.ResponseWriter, r *http.Request) { router.Post("/api/packages/list", func(w http.ResponseWriter, r *http.Request) {
// Get the list of packages // 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 { if err != nil {
renderJSON(500, w, map[string]interface{}{"error": "Internal server error", "code": "01"}, information) renderJSON(500, w, map[string]interface{}{"error": "Internal server error", "code": "01"}, information)
return 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 { if err != nil {
renderJSON(500, w, map[string]interface{}{"error": "Internal server error", "code": "04"}, information) renderJSON(500, w, map[string]interface{}{"error": "Internal server error", "code": "04"}, information)
return 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 err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information) renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information)
@ -443,7 +461,7 @@ func Main(information library.ServiceInitializationInformation) {
} }
var nameCheck string 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 || nameCheck != data.Name {
if err != nil && errors.Is(err, sql.ErrNoRows) { if err != nil && errors.Is(err, sql.ErrNoRows) {
renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information) 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 // Fetch the package from the database
var nameCheck, path string 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 || nameCheck != data.Name {
if err != nil && errors.Is(err, sql.ErrNoRows) { if err != nil && errors.Is(err, sql.ErrNoRows) {
renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information) renderJSON(404, w, map[string]interface{}{"error": "Package not found"}, information)