1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

Db config DSN.

This commit is contained in:
Ferdinand Mütsch 2019-05-06 00:47:38 +02:00
parent 9df289b7ed
commit 5247255d67
2 changed files with 27 additions and 6 deletions

25
main.go
View File

@ -12,27 +12,44 @@ import (
"github.com/codegangsta/negroni" "github.com/codegangsta/negroni"
"github.com/gorilla/mux" "github.com/gorilla/mux"
_ "github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"github.com/n1try/wakapi/middlewares" "github.com/n1try/wakapi/middlewares"
"github.com/n1try/wakapi/models" "github.com/n1try/wakapi/models"
"github.com/n1try/wakapi/routes" "github.com/n1try/wakapi/routes"
"github.com/n1try/wakapi/services" "github.com/n1try/wakapi/services"
) )
func getConfig() models.Config { func readConfig() models.Config {
portPtr := flag.Int("port", 8080, "Port for the webserver to listen on") portPtr := flag.Int("port", 8080, "Port for the webserver to listen on")
dbUser := flag.String("u", "admin", "Database user")
dbPassword := flag.String("p", "admin", "Database password")
dbHost := flag.String("h", "localhost", "Database host")
dbName := flag.String("db", "wakapi", "Database name")
flag.Parse() flag.Parse()
return models.Config{ return models.Config{
Port: *portPtr, Port: *portPtr,
DbHost: *dbHost,
DbUser: *dbUser,
DbPassword: *dbPassword,
DbName: *dbName,
} }
} }
func main() { func main() {
// Read Config // Read Config
config := getConfig() config := readConfig()
// Connect Database // Connect Database
db, _ := sql.Open("mysql", "fakatime_user:eB2zyLt2heqWj5Y9@tcp(muetsch.io:3306)/fakatime") dbConfig := mysql.Config{
User: config.DbUser,
Passwd: config.DbPassword,
Net: "tcp",
Addr: config.DbHost,
DBName: config.DbName,
}
db, _ := sql.Open("mysql", dbConfig.FormatDSN())
defer db.Close() defer db.Close()
err := db.Ping() err := db.Ping()
if err != nil { if err != nil {

View File

@ -2,4 +2,8 @@ package models
type Config struct { type Config struct {
Port int Port int
DbHost string
DbUser string
DbPassword string
DbName string
} }