This repository has been archived on 2022-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
hydra_oidc_poc/idp/services/database.go
2021-01-27 22:22:54 +01:00

64 lines
1.5 KiB
Go

/*
Copyright 2020, 2021 Jan Dittberner
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package services
import (
"context"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type dbContextKey int
const (
ctxDbConnection dbContextKey = iota
)
type DatabaseParams struct {
ConnMaxLifeTime time.Duration
DSN string
MaxOpenConnections int
MaxIdleConnections int
}
func NewDatabaseParams(dsn string) *DatabaseParams {
return &DatabaseParams{
DSN: dsn,
ConnMaxLifeTime: time.Minute * 3,
MaxOpenConnections: 10,
MaxIdleConnections: 10,
}
}
func InitDatabase(ctx context.Context, params *DatabaseParams) (context.Context, error) {
db, err := sqlx.Connect("mysql", params.DSN)
if err != nil {
return nil, err
}
db.SetConnMaxLifetime(params.ConnMaxLifeTime)
db.SetMaxOpenConns(params.MaxOpenConnections)
db.SetMaxIdleConns(params.MaxIdleConnections)
return context.WithValue(ctx, ctxDbConnection, db), nil
}
func GetDb(ctx context.Context) *sqlx.DB {
return ctx.Value(ctxDbConnection).(*sqlx.DB)
}