mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Read config from env.
This commit is contained in:
parent
5247255d67
commit
b403d4105f
5
.env.example
Normal file
5
.env.example
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
WAKAPI_PORT=3000
|
||||||
|
WAKAPI_DB_USER=myuser
|
||||||
|
WAKAPI_DB_PASSWORD=mysecretpassword
|
||||||
|
WAKAPI_DB_HOST=localhost
|
||||||
|
WAKAPI_DB_NAME=wakapi_db
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
launch.json
|
launch.json
|
||||||
.vscode
|
.vscode
|
||||||
|
.env
|
8
README.md
Normal file
8
README.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# wakapi
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
* Copy `.env.example` to `.env` and set config parameters
|
||||||
|
* `go run *.go`
|
||||||
|
|
||||||
|
## License
|
||||||
|
* GPL-v3 @ [Ferdinand Mütsch](https://muetsch.io)
|
52
main.go
52
main.go
@ -2,8 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"flag"
|
"log"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -11,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
"github.com/go-sql-driver/mysql"
|
"github.com/go-sql-driver/mysql"
|
||||||
"github.com/n1try/wakapi/middlewares"
|
"github.com/n1try/wakapi/middlewares"
|
||||||
@ -20,20 +20,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func readConfig() models.Config {
|
func readConfig() models.Config {
|
||||||
portPtr := flag.Int("port", 8080, "Port for the webserver to listen on")
|
err := godotenv.Load()
|
||||||
dbUser := flag.String("u", "admin", "Database user")
|
if err != nil {
|
||||||
dbPassword := flag.String("p", "admin", "Database password")
|
log.Fatal(err)
|
||||||
dbHost := flag.String("h", "localhost", "Database host")
|
}
|
||||||
dbName := flag.String("db", "wakapi", "Database name")
|
|
||||||
|
|
||||||
flag.Parse()
|
portPtr, err := strconv.Atoi(os.Getenv("WAKAPI_PORT"))
|
||||||
|
dbUser, valid := os.LookupEnv("WAKAPI_DB_USER")
|
||||||
|
dbPassword, valid := os.LookupEnv("WAKAPI_DB_PASSWORD")
|
||||||
|
dbHost, valid := os.LookupEnv("WAKAPI_DB_HOST")
|
||||||
|
dbName, valid := os.LookupEnv("WAKAPI_DB_NAME")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if !valid {
|
||||||
|
log.Fatal("Config parameters missing.")
|
||||||
|
}
|
||||||
|
|
||||||
return models.Config{
|
return models.Config{
|
||||||
Port: *portPtr,
|
Port: portPtr,
|
||||||
DbHost: *dbHost,
|
DbHost: dbHost,
|
||||||
DbUser: *dbUser,
|
DbUser: dbUser,
|
||||||
DbPassword: *dbPassword,
|
DbPassword: dbPassword,
|
||||||
DbName: *dbName,
|
DbName: dbName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,18 +53,18 @@ func main() {
|
|||||||
|
|
||||||
// Connect Database
|
// Connect Database
|
||||||
dbConfig := mysql.Config{
|
dbConfig := mysql.Config{
|
||||||
User: config.DbUser,
|
User: config.DbUser,
|
||||||
Passwd: config.DbPassword,
|
Passwd: config.DbPassword,
|
||||||
Net: "tcp",
|
Net: "tcp",
|
||||||
Addr: config.DbHost,
|
Addr: config.DbHost,
|
||||||
DBName: config.DbName,
|
DBName: config.DbName,
|
||||||
|
AllowNativePasswords: true,
|
||||||
}
|
}
|
||||||
db, _ := sql.Open("mysql", dbConfig.FormatDSN())
|
db, _ := sql.Open("mysql", dbConfig.FormatDSN())
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
err := db.Ping()
|
err := db.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Could not connect to database.")
|
log.Fatal("Could not connect to database.")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
@ -91,6 +101,6 @@ func main() {
|
|||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
WriteTimeout: 10 * time.Second,
|
WriteTimeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
fmt.Printf("Listening on %+s\n", portString)
|
log.Printf("Listening on %+s\n", portString)
|
||||||
s.ListenAndServe()
|
s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user