-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
44 lines (34 loc) · 836 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main // Look README.md
import (
"fmt"
"log"
"os"
"myapp/api"
"myapp/sql"
"github.com/kataras/iris/v12"
)
func main() {
dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?parseTime=true&charset=utf8mb4&collation=utf8mb4_unicode_ci",
getenv("MYSQL_USER", "user_myapp"),
getenv("MYSQL_PASSWORD", "dbpassword"),
getenv("MYSQL_HOST", "localhost"),
getenv("MYSQL_DATABASE", "myapp"),
)
db, err := sql.ConnectMySQL(dsn)
if err != nil {
log.Fatalf("error connecting to the MySQL database: %v", err)
}
secret := getenv("JWT_SECRET", "EbnJO3bwmX")
app := iris.New()
subRouter := api.Router(db, secret)
app.PartyFunc("/", subRouter)
addr := fmt.Sprintf(":%s", getenv("PORT", "8080"))
app.Listen(addr)
}
func getenv(key string, def string) string {
v := os.Getenv(key)
if v == "" {
return def
}
return v
}