vtacquet / redbase-plugin

Redbase Plugin is a middleware for Traefik (https://github.com/traefik/traefik) that redirects according to records in an SQLite database served by a Redbase daemon (https://hub.docker.com/vtacquet/redbase)
3 stars 0 forks source link

opensource redbase? #2

Closed dreik closed 1 week ago

dreik commented 1 month ago

Is there any chance to see the sources of the redbase?

vtacquet commented 1 week ago
package main

import (
    "os"
    "log"
    "database/sql"
    _ "github.com/mattn/go-sqlite3"
)

func createDatabase(file string) {
    db, dbok := os.Create(file)
    if dbok != nil {
        log.Fatalln(dbok.Error())
        os.Exit(1)
    }
    db.Close()
}

func createRedirectTable(db *sql.DB) {
    createRedirectTableSQL := `CREATE TABLE IF NOT EXISTS redirect ("from_url" TEXT NOT NULL PRIMARY KEY, "to_url" TEXT NOT NULL, "permanent" BOOL DEFAULT "False")`
    statement, statementok := db.Prepare(createRedirectTableSQL)
    if statementok != nil {
        log.Fatalln(statementok.Error())
        os.Exit(1)
    }
    statement.Exec()
}

func createRedirectIndex(db *sql.DB) {
    createRedirectIndexSQL := `CREATE UNIQUE INDEX IF NOT EXISTS redirect_from_url ON redirect ("from_url")`
    statement, statementok := db.Prepare(createRedirectIndexSQL)
    if statementok != nil {
        log.Fatalln(statementok.Error())
        os.Exit(1)
    }
    statement.Exec()
}

func insertRedirectRecord(db *sql.DB, from_url string, to_url string, permanent bool) {
    insertRedirectRecordSQL := `INSERT OR IGNORE INTO redirect("from_url", "to_url", "permanent") VALUES (?, ?, ?)`
    statement, statementok := db.Prepare(insertRedirectRecordSQL)
    if statementok != nil {
        log.Fatalln(statementok.Error())
    }
    _, statementok = statement.Exec(from_url, to_url, permanent)
    if statementok != nil {
        log.Fatalln(statementok.Error())
    }
    defer statement.Close()
}

func selectRedirectRecords(db *sql.DB) {
        selectRedirectRecordsSQL := `SELECT "from_url", "to_url", "permanent" FROM redirect ORDER BY from_url`
    statement, statementok := db.Query(selectRedirectRecordsSQL)
    if statementok != nil {
        log.Fatal(statementok)
    }
    defer statement.Close()
    for statement.Next() {
        var out_from_url string
        var out_to_url string
        var out_permanent bool
        var out_redirtype string
        statement.Scan(&out_from_url, &out_to_url, &out_permanent)
        if out_permanent {
            out_redirtype = "PERMANENT"
        } else {
            out_redirtype = "TEMPORARY"
        }
        log.Println("Redirect: " + out_from_url + " -> " + out_to_url + " -- " + out_redirtype)
    }
}

func selectRedirectRecord(db *sql.DB, from_url string) {
        selectRedirectRecordSQL := `SELECT "from_url", "to_url", "permanent" FROM redirect WHERE "from_url" IN (?, "@default") ORDER BY "from_url" DESC LIMIT 1`
    var out_from_url string
    var out_to_url string
    var out_permanent bool
    var out_redirtype string
    statementok := db.QueryRow(selectRedirectRecordSQL, from_url).Scan(&out_from_url, &out_to_url, &out_permanent)
    if statementok != nil {
        log.Fatalln(statementok)
    } else {
        if out_permanent {
            out_redirtype = "PERMANENT"
        } else {
            out_redirtype = "TEMPORARY"
        }
        log.Println("Redirect: " + out_from_url + " -> " + out_to_url + " -- " + out_redirtype)
    }
}

func main() {
    env, envok := os.LookupEnv("DBFILE")
    if ! envok {
        log.Println("environment DBFILE not found")
        os.Exit(1)
    }

    file, fileok := os.Open(env)
    if fileok != nil {
        createDatabase(env);
    }
    defer file.Close()

    database, _ := sql.Open("sqlite3", env+"?_vacuum=1")
    defer database.Close()

    // CREATE TABLE IF NOT EXISTS
    createRedirectTable(database)

    // CREATE INDEX IF NOT EXISTS
    createRedirectIndex(database)

        // INSERT DEFAULT RECORD
    insertRedirectRecord(database, "@default", "https://www.tacquet.be", false)

        args := len(os.Args)
    if args < 2 {
            // SELECT ALL RECORDS
        selectRedirectRecords(database)
    } else if args == 2 {
        // SELECT ARGUMENT RECORD
        selectRedirectRecord(database,os.Args[1])
    }
}