schema migration and account fixes
very close to rolling this out! just need to address some security concerns first
This commit is contained in:
parent
5566a795da
commit
570cdf6ce2
20 changed files with 641 additions and 392 deletions
257
main.go
257
main.go
|
@ -7,22 +7,28 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"arimelody-web/admin"
|
||||
"arimelody-web/api"
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/view"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/templates"
|
||||
"arimelody-web/view"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// used for database migrations
|
||||
const DB_VERSION = 1
|
||||
|
||||
const DEFAULT_PORT int64 = 8080
|
||||
|
||||
func main() {
|
||||
fmt.Printf("made with <3 by ari melody\n\n")
|
||||
|
||||
// initialise database connection
|
||||
if env := os.Getenv("ARIMELODY_DB_HOST"); env != "" { global.Config.DB.Host = env }
|
||||
if env := os.Getenv("ARIMELODY_DB_NAME"); env != "" { global.Config.DB.Name = env }
|
||||
|
@ -65,39 +71,107 @@ func main() {
|
|||
global.DB.SetMaxIdleConns(10)
|
||||
defer global.DB.Close()
|
||||
|
||||
_, err = global.DB.Exec("DELETE FROM invite WHERE expires_at < CURRENT_TIMESTAMP")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to clear expired invite codes: %v\n", err)
|
||||
os.Exit(1)
|
||||
// handle command arguments
|
||||
if len(os.Args) > 1 {
|
||||
arg := os.Args[1]
|
||||
|
||||
switch arg {
|
||||
case "createInvite":
|
||||
fmt.Printf("Creating invite...\n")
|
||||
invite, err := controller.CreateInvite(global.DB, 16, time.Hour * 24)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to create invite code: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Here you go! This code expires in 24 hours: %s\n", invite.Code)
|
||||
return
|
||||
|
||||
case "purgeInvites":
|
||||
fmt.Printf("Deleting all invites...\n")
|
||||
err := controller.DeleteAllInvites(global.DB)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to delete invites: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Invites deleted successfully.\n")
|
||||
return
|
||||
|
||||
case "deleteAccount":
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Account name not specified for -deleteAccount\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
username := os.Args[2]
|
||||
fmt.Printf("Deleting account \"%s\"...\n", username)
|
||||
|
||||
account, err := controller.GetAccount(global.DB, username)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to fetch account \"%s\": %s\n", username, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if account == nil {
|
||||
fmt.Fprintf(os.Stderr, "Account \"%s\" does not exist.\n", username)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("You are about to delete \"%s\". Are you sure? (y/[N]): ", account.Username)
|
||||
res := ""
|
||||
fmt.Scanln(&res)
|
||||
if !strings.HasPrefix(res, "y") {
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteAccount(global.DB, username)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to delete account: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("Account \"%s\" deleted successfully.\n", account.Username)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
fmt.Printf(
|
||||
"Available commands:\n\n" +
|
||||
"createInvite:\n\tCreates an invite code to register new accounts.\n" +
|
||||
"purgeInvites:\n\tDeletes all available invite codes.\n" +
|
||||
"deleteAccount <username>:\n\tDeletes an account with a given `username`.\n",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
accountsCount := 0
|
||||
global.DB.Get(&accountsCount, "SELECT count(*) FROM account")
|
||||
if accountsCount == 0 {
|
||||
code := controller.GenerateInviteCode(8)
|
||||
// handle DB migrations
|
||||
controller.CheckDBVersionAndMigrate(global.DB)
|
||||
|
||||
tx, err := global.DB.Begin()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to begin transaction: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM invite")
|
||||
// initial invite code
|
||||
accountsCount := 0
|
||||
err = global.DB.Get(&accountsCount, "SELECT count(*) FROM account")
|
||||
if err != nil { panic(err) }
|
||||
if accountsCount == 0 {
|
||||
_, err := global.DB.Exec("DELETE FROM invite")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to clear existing invite codes: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = tx.Exec("INSERT INTO invite (code,expires_at) VALUES ($1, $2)", code, time.Now().Add(60 * time.Minute))
|
||||
|
||||
invite, err := controller.CreateInvite(global.DB, 16, time.Hour * 24)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to create invite codes: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to create invite codes: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to create invite code: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stdout, "INFO: No accounts exist! Generated invite code: " + string(code) + " (Use this at /register or /api/v1/register)")
|
||||
fmt.Fprintf(os.Stdout, "No accounts exist! Generated invite code: " + string(invite.Code) + "\nUse this at %s/admin/register.\n", global.Config.BaseUrl)
|
||||
}
|
||||
|
||||
// delete expired invites
|
||||
err = controller.DeleteExpiredInvites(global.DB)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to clear expired invite codes: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// start the web server!
|
||||
|
@ -109,141 +183,6 @@ func main() {
|
|||
))
|
||||
}
|
||||
|
||||
func initDB(driverName string, dataSourceName string) (*sqlx.DB, error) {
|
||||
db, err := sqlx.Connect(driverName, dataSourceName)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
// ensure tables exist
|
||||
// account
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS account (" +
|
||||
"id uuid PRIMARY KEY DEFAULT gen_random_uuid(), " +
|
||||
"username text NOT NULL UNIQUE, " +
|
||||
"password text NOT NULL, " +
|
||||
"email text, " +
|
||||
"avatar_url text)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create account table: %s", err.Error())) }
|
||||
|
||||
// privilege
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS privilege (" +
|
||||
"account uuid NOT NULL, " +
|
||||
"privilege text NOT NULL, " +
|
||||
"CONSTRAINT privilege_pk PRIMARY KEY (account, privilege), " +
|
||||
"CONSTRAINT privilege_account_fk FOREIGN KEY (account) REFERENCES account(id) ON DELETE CASCADE)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create privilege table: %s", err.Error())) }
|
||||
|
||||
// totp
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS totp (" +
|
||||
"account uuid NOT NULL, " +
|
||||
"name text NOT NULL, " +
|
||||
"created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"CONSTRAINT totp_pk PRIMARY KEY (account, name), " +
|
||||
"CONSTRAINT totp_account_fk FOREIGN KEY (account) REFERENCES account(id) ON DELETE CASCADE)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create TOTP table: %s", err.Error())) }
|
||||
|
||||
// invites
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS invite (" +
|
||||
"code text NOT NULL PRIMARY KEY, " +
|
||||
"created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"expires_at TIMESTAMP NOT NULL)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create TOTP table: %s", err.Error())) }
|
||||
|
||||
// account token
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS token (" +
|
||||
"token TEXT PRIMARY KEY," +
|
||||
"account UUID REFERENCES account(id) ON DELETE CASCADE NOT NULL," +
|
||||
"user_agent TEXT NOT NULL," +
|
||||
"created_at TIMESTAMP NOT NULL DEFAULT current_timestamp)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create token table: %s\n", err.Error())) }
|
||||
|
||||
// artist
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS artist (" +
|
||||
"id character varying(64) PRIMARY KEY, " +
|
||||
"name text NOT NULL, " +
|
||||
"website text, " +
|
||||
"avatar text)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create artist table: %s", err.Error())) }
|
||||
|
||||
// musicrelease
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS musicrelease (" +
|
||||
"id character varying(64) PRIMARY KEY, " +
|
||||
"visible bool DEFAULT false, " +
|
||||
"title text NOT NULL, " +
|
||||
"description text, " +
|
||||
"type text, " +
|
||||
"release_date TIMESTAMP NOT NULL, " +
|
||||
"artwork text, " +
|
||||
"buyname text, " +
|
||||
"buylink text, " +
|
||||
"copyright text, " +
|
||||
"copyrightURL text)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create musicrelease table: %s", err.Error())) }
|
||||
|
||||
// musiclink
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS public.musiclink (" +
|
||||
"release character varying(64) NOT NULL, " +
|
||||
"name text NOT NULL, " +
|
||||
"url text NOT NULL, " +
|
||||
"CONSTRAINT musiclink_pk PRIMARY KEY (release, name), " +
|
||||
"CONSTRAINT musiclink_release_fk FOREIGN KEY (release) REFERENCES musicrelease(id) ON DELETE CASCADE)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create musiclink table: %s", err.Error())) }
|
||||
|
||||
// musiccredit
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS public.musiccredit (" +
|
||||
"release character varying(64) NOT NULL, " +
|
||||
"artist character varying(64) NOT NULL, " +
|
||||
"role text NOT NULL, " +
|
||||
"is_primary boolean DEFAULT false, " +
|
||||
"CONSTRAINT musiccredit_pk PRIMARY KEY (release, artist), " +
|
||||
"CONSTRAINT musiccredit_release_fk FOREIGN KEY (release) REFERENCES musicrelease(id) ON DELETE CASCADE, " +
|
||||
"CONSTRAINT musiccredit_artist_fk FOREIGN KEY (artist) REFERENCES artist(id) ON DELETE CASCADE)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create musiccredit table: %s", err.Error())) }
|
||||
|
||||
// musictrack
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS public.musictrack (" +
|
||||
"id uuid DEFAULT gen_random_uuid() PRIMARY KEY, " +
|
||||
"title text NOT NULL, " +
|
||||
"description text, " +
|
||||
"lyrics text, " +
|
||||
"preview_url text)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create musictrack table: %s", err.Error())) }
|
||||
|
||||
// musicreleasetrack
|
||||
_, err = db.Exec(
|
||||
"CREATE TABLE IF NOT EXISTS public.musicreleasetrack (" +
|
||||
"release character varying(64) NOT NULL, " +
|
||||
"track uuid NOT NULL, " +
|
||||
"number integer NOT NULL, " +
|
||||
"CONSTRAINT musicreleasetrack_pk PRIMARY KEY (release, track), " +
|
||||
"CONSTRAINT musicreleasetrack_release_fk FOREIGN KEY (release) REFERENCES musicrelease(id) ON DELETE CASCADE, " +
|
||||
"CONSTRAINT musicreleasetrack_artist_fk FOREIGN KEY (track) REFERENCES track(id) ON DELETE CASCADE)",
|
||||
)
|
||||
if err != nil { return nil, errors.New(fmt.Sprintf("Failed to create musicreleasetrack table: %s", err.Error())) }
|
||||
|
||||
// TODO: automatic database migration
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func createServeMux() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue