mirror of
				https://github.com/therootcompany/telebit.git
				synced 2025-10-31 05:12:47 +00:00 
			
		
		
		
	bugfix: error instead of panic for invalid connection string
This commit is contained in:
		
							parent
							
								
									b51f7992cd
								
							
						
					
					
						commit
						85147ca776
					
				| @ -143,15 +143,7 @@ func main() { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	connStr := dbURL | 	store, err = authstore.NewStore(dbURL, mgmt.InitSQL) | ||||||
| 	// TODO url.Parse |  | ||||||
| 	if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") { |  | ||||||
| 		connStr += "?sslmode=disable" |  | ||||||
| 	} else { |  | ||||||
| 		connStr += "?sslmode=required" |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	store, err = authstore.NewStore(connStr, mgmt.InitSQL) |  | ||||||
| 	if nil != err { | 	if nil != err { | ||||||
| 		log.Fatal("connection error", err) | 		log.Fatal("connection error", err) | ||||||
| 		return | 		return | ||||||
|  | |||||||
| @ -10,10 +10,18 @@ import ( | |||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 	connStr := "postgres://postgres:postgres@localhost:5432/postgres" | 	connStr := "postgres://postgres:postgres@localhost:5432/postgres" | ||||||
| 	if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") { | 
 | ||||||
| 		connStr += "?sslmode=disable" | 	if !strings.Contains(connStr, "sslmode=") { | ||||||
|  | 		sep := "?" | ||||||
|  | 		if strings.Contains(connStr, sep) { | ||||||
|  | 			sep = "&" | ||||||
|  | 		} | ||||||
|  | 		if strings.Contains(connStr, "@localhost/") || | ||||||
|  | 			strings.Contains(connStr, "@localhost:") { | ||||||
|  | 			connStr += sep + "sslmode=disable" | ||||||
| 		} else { | 		} else { | ||||||
| 		connStr += "?sslmode=required" | 			connStr += sep + "sslmode=required" | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	store, err := authstore.NewStore(connStr, initSQL) | 	store, err := authstore.NewStore(connStr, initSQL) | ||||||
|  | |||||||
| @ -1,14 +1,6 @@ | |||||||
| package authstore | package authstore | ||||||
| 
 | 
 | ||||||
| import "strings" |  | ||||||
| 
 |  | ||||||
| var connStr = "postgres://postgres:postgres@localhost/postgres" | var connStr = "postgres://postgres:postgres@localhost/postgres" | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
| 	// TODO url.Parse |  | ||||||
| 	if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") { |  | ||||||
| 		connStr += "?sslmode=disable" |  | ||||||
| 	} else { |  | ||||||
| 		connStr += "?sslmode=required" |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ import ( | |||||||
| 	"database/sql" | 	"database/sql" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
|  | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"git.rootprojects.org/root/telebit/assets/files" | 	"git.rootprojects.org/root/telebit/assets/files" | ||||||
| @ -16,9 +17,22 @@ import ( | |||||||
| 
 | 
 | ||||||
| var initSQL = "./postgres.init.sql" | var initSQL = "./postgres.init.sql" | ||||||
| 
 | 
 | ||||||
| func NewStore(pgURL, initSQL string) (Store, error) { | func NewStore(dbURL, initSQL string) (Store, error) { | ||||||
| 	// https://godoc.org/github.com/lib/pq | 	// https://godoc.org/github.com/lib/pq | ||||||
| 
 | 
 | ||||||
|  | 	// TODO url.Parse | ||||||
|  | 	if !strings.Contains(dbURL, "sslmode=") { | ||||||
|  | 		sep := "?" | ||||||
|  | 		if strings.Contains(connStr, sep) { | ||||||
|  | 			sep = "&" | ||||||
|  | 		} | ||||||
|  | 		if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") { | ||||||
|  | 			connStr += sep + "sslmode=disable" | ||||||
|  | 		} else { | ||||||
|  | 			connStr += sep + "sslmode=required" | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	f, err := files.Open(initSQL) | 	f, err := files.Open(initSQL) | ||||||
| 	if nil != err { | 	if nil != err { | ||||||
| 		return nil, err | 		return nil, err | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user