aboutsummaryrefslogtreecommitdiff
path: root/internal/db/models.go
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-04-04 21:14:15 +0800
committerGitHub <noreply@github.com>2020-04-04 21:14:15 +0800
commit34145c990d4fd9f278f29cdf9c61378a75e9b934 (patch)
tree7b151bbd5aef9e487759953e3a775a82244d268d /internal/db/models.go
parent2bd9d0b9c8238ded727cd98a3ace20b53c10a44f (diff)
lfs: implement HTTP routes (#6035)
* Bootstrap with GORM * Fix lint error * Set conn max lifetime to one minute * Fallback to use gorm v1 * Define HTTP routes * Finish authentication * Save token updated * Add docstring * Finish authorization * serveBatch rundown * Define types in lfsutil * Finish Batch * authutil * Finish basic * Formalize response error * Fix lint errors * authutil: add tests * dbutil: add tests * lfsutil: add tests * strutil: add tests * Formalize 401 response
Diffstat (limited to 'internal/db/models.go')
-rw-r--r--internal/db/models.go55
1 files changed, 8 insertions, 47 deletions
diff --git a/internal/db/models.go b/internal/db/models.go
index cf00727e..3bb35e7f 100644
--- a/internal/db/models.go
+++ b/internal/db/models.go
@@ -7,7 +7,6 @@ package db
import (
"bufio"
"database/sql"
- "errors"
"fmt"
"net/url"
"os"
@@ -15,10 +14,7 @@ import (
"strings"
"time"
- _ "github.com/denisenkom/go-mssqldb"
- _ "github.com/go-sql-driver/mysql"
"github.com/json-iterator/go"
- _ "github.com/lib/pq"
"github.com/unknwon/com"
log "unknwon.dev/clog/v2"
"xorm.io/core"
@@ -48,8 +44,6 @@ var (
x *xorm.Engine
tables []interface{}
HasEngine bool
-
- EnableSQLite3 bool
)
func init() {
@@ -70,35 +64,6 @@ func init() {
}
}
-// parsePostgreSQLHostPort parses given input in various forms defined in
-// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
-// and returns proper host and port number.
-func parsePostgreSQLHostPort(info string) (string, string) {
- host, port := "127.0.0.1", "5432"
- if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
- idx := strings.LastIndex(info, ":")
- host = info[:idx]
- port = info[idx+1:]
- } else if len(info) > 0 {
- host = info
- }
- return host, port
-}
-
-func parseMSSQLHostPort(info string) (string, string) {
- host, port := "127.0.0.1", "1433"
- if strings.Contains(info, ":") {
- host = strings.Split(info, ":")[0]
- port = strings.Split(info, ":")[1]
- } else if strings.Contains(info, ",") {
- host = strings.Split(info, ",")[0]
- port = strings.TrimSpace(strings.Split(info, ",")[1])
- } else if len(info) > 0 {
- host = info
- }
- return host, port
-}
-
func getEngine() (*xorm.Engine, error) {
Param := "?"
if strings.Contains(conf.Database.Name, Param) {
@@ -133,12 +98,9 @@ func getEngine() (*xorm.Engine, error) {
case "mssql":
conf.UseMSSQL = true
host, port := parseMSSQLHostPort(conf.Database.Host)
- connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, conf.Database.Name, conf.Database.User, conf.Database.Passwd)
+ connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, conf.Database.Name, conf.Database.User, conf.Database.Password)
case "sqlite3":
- if !EnableSQLite3 {
- return nil, errors.New("this binary version does not build support for SQLite3")
- }
if err := os.MkdirAll(path.Dir(conf.Database.Path), os.ModePerm); err != nil {
return nil, fmt.Errorf("create directories: %v", err)
}
@@ -183,9 +145,8 @@ func SetEngine() (err error) {
return fmt.Errorf("create 'xorm.log': %v", err)
}
- // To prevent mystery "MySQL: invalid connection" error,
- // see https://gogs.io/gogs/issues/5532.
- x.SetMaxIdleConns(0)
+ x.SetMaxOpenConns(conf.Database.MaxOpenConns)
+ x.SetMaxIdleConns(conf.Database.MaxIdleConns)
x.SetConnMaxLifetime(time.Second)
if conf.IsProdMode() {
@@ -194,7 +155,7 @@ func SetEngine() (err error) {
x.SetLogger(xorm.NewSimpleLogger(logger))
}
x.ShowSQL(true)
- return nil
+ return Init()
}
func NewEngine() (err error) {
@@ -331,13 +292,13 @@ func ImportDatabase(dirPath string, verbose bool) (err error) {
tp := LoginType(com.StrTo(com.ToStr(meta["Type"])).MustInt64())
switch tp {
- case LOGIN_LDAP, LOGIN_DLDAP:
+ case LoginLDAP, LoginDLDAP:
bean.Cfg = new(LDAPConfig)
- case LOGIN_SMTP:
+ case LoginSMTP:
bean.Cfg = new(SMTPConfig)
- case LOGIN_PAM:
+ case LoginPAM:
bean.Cfg = new(PAMConfig)
- case LOGIN_GITHUB:
+ case LoginGitHub:
bean.Cfg = new(GitHubConfig)
default:
return fmt.Errorf("unrecognized login source type:: %v", tp)