rogueserver/db/db.go

59 lines
1.5 KiB
Go
Raw Normal View History

2024-04-29 17:26:46 -04:00
/*
2025-04-25 19:50:49 -04:00
Copyright (C) 2024 - 2025 Pagefault Games
2024-04-29 17:26:46 -04:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-04-29 15:32:58 -04:00
2023-12-05 13:28:08 -05:00
package db
import (
2025-04-25 14:55:40 -04:00
"context"
2023-12-05 13:28:08 -05:00
"database/sql"
"fmt"
2025-04-25 14:55:40 -04:00
"os"
2024-05-08 20:19:33 -04:00
2025-04-25 14:55:40 -04:00
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
2024-05-08 20:19:33 -04:00
_ "github.com/go-sql-driver/mysql"
2023-12-05 13:28:08 -05:00
)
var handle *sql.DB
2025-04-25 14:55:40 -04:00
var s3client *s3.Client
2023-12-05 13:28:08 -05:00
func Init(username, password, protocol, address, database string) error {
2024-04-25 16:27:24 -04:00
var err error
handle, err = sql.Open("mysql", username+":"+password+"@"+protocol+"("+address+")/"+database)
2023-12-05 13:28:08 -05:00
if err != nil {
return fmt.Errorf("failed to open database connection: %s", err)
}
2024-05-10 15:49:26 -04:00
2025-04-25 14:55:40 -04:00
if os.Getenv("AWS_ENDPOINT_URL_S3") != "" {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}
s3client = s3.NewFromConfig(cfg)
}
// Conditionally run DB setup (devsetup build tag controls behavior)
err = MaybeSetupDb(handle)
2024-05-11 14:06:47 +02:00
if err != nil {
return err
2024-05-11 14:06:47 +02:00
}
2023-12-05 13:28:08 -05:00
return nil
}