add hostname
This commit is contained in:
12
main.go
12
main.go
@@ -4,6 +4,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -38,11 +39,17 @@ func main() {
|
||||
PrepareStmt: true,
|
||||
SkipDefaultTransaction: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Error to connect sqlite database: %s", err)
|
||||
}
|
||||
case "postgres":
|
||||
db, err = gorm.Open(postgres.Open(config.DBAddr), &gorm.Config{
|
||||
PrepareStmt: true,
|
||||
SkipDefaultTransaction: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Error to connect postgres database: %s", err)
|
||||
}
|
||||
default:
|
||||
log.Fatalf("Unsupported database type: '%s'", config.DBType)
|
||||
}
|
||||
@@ -88,8 +95,13 @@ func main() {
|
||||
})
|
||||
|
||||
engine.POST("/v1/*any", func(c *gin.Context) {
|
||||
hostname, err := os.Hostname()
|
||||
if config.Hostname != "" {
|
||||
hostname = config.Hostname
|
||||
}
|
||||
record := Record{
|
||||
IP: c.ClientIP(),
|
||||
Hostname: hostname,
|
||||
CreatedAt: time.Now(),
|
||||
Authorization: c.Request.Header.Get("Authorization"),
|
||||
UserAgent: c.Request.Header.Get("User-Agent"),
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
type Record struct {
|
||||
ID int64 `gorm:"primaryKey,autoIncrement"`
|
||||
Hostname string
|
||||
UpstreamEndpoint string
|
||||
UpstreamSK string
|
||||
CreatedAt time.Time
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
type Config struct {
|
||||
Address string `yaml:"address"`
|
||||
Hostname string `yaml:"hostname"`
|
||||
DBType string `yaml:"dbtype"`
|
||||
DBAddr string `yaml:"dbaddr"`
|
||||
Authorization string `yaml:"authorization"`
|
||||
|
||||
Reference in New Issue
Block a user