add hostname

This commit is contained in:
2023-11-30 10:18:01 +08:00
parent 7d93332e51
commit 2c3532f12f
3 changed files with 14 additions and 0 deletions

12
main.go
View File

@@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"os"
"strings" "strings"
"time" "time"
@@ -38,11 +39,17 @@ func main() {
PrepareStmt: true, PrepareStmt: true,
SkipDefaultTransaction: true, SkipDefaultTransaction: true,
}) })
if err != nil {
log.Fatalf("Error to connect sqlite database: %s", err)
}
case "postgres": case "postgres":
db, err = gorm.Open(postgres.Open(config.DBAddr), &gorm.Config{ db, err = gorm.Open(postgres.Open(config.DBAddr), &gorm.Config{
PrepareStmt: true, PrepareStmt: true,
SkipDefaultTransaction: true, SkipDefaultTransaction: true,
}) })
if err != nil {
log.Fatalf("Error to connect postgres database: %s", err)
}
default: default:
log.Fatalf("Unsupported database type: '%s'", config.DBType) log.Fatalf("Unsupported database type: '%s'", config.DBType)
} }
@@ -88,8 +95,13 @@ func main() {
}) })
engine.POST("/v1/*any", func(c *gin.Context) { engine.POST("/v1/*any", func(c *gin.Context) {
hostname, err := os.Hostname()
if config.Hostname != "" {
hostname = config.Hostname
}
record := Record{ record := Record{
IP: c.ClientIP(), IP: c.ClientIP(),
Hostname: hostname,
CreatedAt: time.Now(), CreatedAt: time.Now(),
Authorization: c.Request.Header.Get("Authorization"), Authorization: c.Request.Header.Get("Authorization"),
UserAgent: c.Request.Header.Get("User-Agent"), UserAgent: c.Request.Header.Get("User-Agent"),

View File

@@ -6,6 +6,7 @@ import (
type Record struct { type Record struct {
ID int64 `gorm:"primaryKey,autoIncrement"` ID int64 `gorm:"primaryKey,autoIncrement"`
Hostname string
UpstreamEndpoint string UpstreamEndpoint string
UpstreamSK string UpstreamSK string
CreatedAt time.Time CreatedAt time.Time

View File

@@ -9,6 +9,7 @@ import (
type Config struct { type Config struct {
Address string `yaml:"address"` Address string `yaml:"address"`
Hostname string `yaml:"hostname"`
DBType string `yaml:"dbtype"` DBType string `yaml:"dbtype"`
DBAddr string `yaml:"dbaddr"` DBAddr string `yaml:"dbaddr"`
Authorization string `yaml:"authorization"` Authorization string `yaml:"authorization"`