-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (44 loc) · 1.14 KB
/
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
45
46
47
48
49
50
51
package main
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
db_host := os.Getenv("db_host")
db_port := os.Getenv("db_port")
db_username := os.Getenv("db_username")
db_password := os.Getenv("db_password")
db_name := os.Getenv("db_name")
db_status := "Connected to database"
dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
db_username, db_password, db_host, db_port, db_name)
_, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
db_status = err.Error()
}
if os.Getenv("db_host") == "" {
db_status = "Database invalid configuration"
}
c.JSON(http.StatusOK, gin.H{
"version": "v2.0",
"description": "sample golang application",
"database": gin.H{
"info": gin.H{
"host": db_host,
"port": db_port,
"username": db_username,
"password": db_password,
"db_name": db_name,
},
"status": db_status,
},
})
})
r.Run("0.0.0.0:8000") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}