From 9c05230173422f95fa5796b63ae5366b717928c6 Mon Sep 17 00:00:00 2001 From: Arkadiy Kukarkin Date: Tue, 23 Dec 2025 12:23:12 +0100 Subject: [PATCH] add go backend scaffold --- config/config.go | 171 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 17 +++++ go.sum | 74 ++++++++++++++++++++ main.go | 26 +++++++ 4 files changed, 288 insertions(+) create mode 100644 config/config.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..9486719 --- /dev/null +++ b/config/config.go @@ -0,0 +1,171 @@ +package config + +import ( + "os" + "strconv" +) + +type AuthMethod int + +const ( + AuthPassword AuthMethod = iota + AuthHtpasswd + AuthLDAP +) + +type VelocityUnit int + +const ( + KilometersPerHour VelocityUnit = iota + MilesPerHour + MetersPerSecond +) + +type LinkStyle int + +const ( + Link4Plus4Upper LinkStyle = iota + Link4Plus4Lower + Link4Plus4Mixed + LinkUUIDv4 + Link16Hex + Link16Upper + Link16Lower + Link16Mixed + Link32Hex + Link32Upper + Link32Lower + Link32Mixed +) + +type Config struct { + ListenAddr string + PublicURL string + + // redis + RedisAddr string + RedisPassword string + RedisPrefix string + + // auth + AuthMethod AuthMethod + PasswordHash string + HtpasswdPath string + + // ldap + LDAPUri string + LDAPBaseDN string + LDAPBindDN string + LDAPBindPass string + LDAPUserFilter string + LDAPStartTLS bool + + // share limits + MaxDuration int + MinInterval float64 + MaxCachedPts int + MaxShownPts int + + // link generation + LinkStyle LinkStyle + AllowLinkReq bool + ReservedLinks map[string][]string + ReserveWL bool + + // map display + MapTileURI string + MapAttribution string + DefaultZoom int + MaxZoom int + + // velocity + VelocityUnit VelocityUnit + VelocityDataPts int + TrailColor string + OfflineTimeout int + RequestTimeout int +} + +func envStr(key, def string) string { + if v := os.Getenv(key); v != "" { + return v + } + return def +} + +func envInt(key string, def int) int { + if v := os.Getenv(key); v != "" { + if i, err := strconv.Atoi(v); err == nil { + return i + } + } + return def +} + +func envFloat(key string, def float64) float64 { + if v := os.Getenv(key); v != "" { + if f, err := strconv.ParseFloat(v, 64); err == nil { + return f + } + } + return def +} + +func envBool(key string, def bool) bool { + if v := os.Getenv(key); v != "" { + return v == "true" || v == "1" + } + return def +} + +func Load() *Config { + authMethod := AuthPassword + switch envStr("HAUK_AUTH_METHOD", "password") { + case "htpasswd": + authMethod = AuthHtpasswd + case "ldap": + authMethod = AuthLDAP + } + + velUnit := KilometersPerHour + switch envStr("HAUK_VELOCITY_UNIT", "km/h") { + case "mph": + velUnit = MilesPerHour + case "m/s": + velUnit = MetersPerSecond + } + + return &Config{ + ListenAddr: envStr("HAUK_LISTEN_ADDR", ":8080"), + PublicURL: envStr("HAUK_PUBLIC_URL", "http://localhost:8080/"), + RedisAddr: envStr("HAUK_REDIS_ADDR", "localhost:6379"), + RedisPassword: envStr("HAUK_REDIS_PASSWORD", ""), + RedisPrefix: envStr("HAUK_REDIS_PREFIX", "hauk"), + AuthMethod: authMethod, + PasswordHash: envStr("HAUK_PASSWORD_HASH", ""), + HtpasswdPath: envStr("HAUK_HTPASSWD_PATH", "/etc/hauk/users.htpasswd"), + LDAPUri: envStr("HAUK_LDAP_URI", ""), + LDAPBaseDN: envStr("HAUK_LDAP_BASE_DN", ""), + LDAPBindDN: envStr("HAUK_LDAP_BIND_DN", ""), + LDAPBindPass: envStr("HAUK_LDAP_BIND_PASS", ""), + LDAPUserFilter: envStr("HAUK_LDAP_USER_FILTER", "(uid=%s)"), + LDAPStartTLS: envBool("HAUK_LDAP_START_TLS", false), + MaxDuration: envInt("HAUK_MAX_DURATION", 86400), + MinInterval: envFloat("HAUK_MIN_INTERVAL", 1), + MaxCachedPts: envInt("HAUK_MAX_CACHED_PTS", 3), + MaxShownPts: envInt("HAUK_MAX_SHOWN_PTS", 100), + LinkStyle: LinkStyle(envInt("HAUK_LINK_STYLE", 0)), + AllowLinkReq: envBool("HAUK_ALLOW_LINK_REQ", true), + ReservedLinks: make(map[string][]string), + ReserveWL: envBool("HAUK_RESERVE_WHITELIST", false), + MapTileURI: envStr("HAUK_MAP_TILE_URI", "https://tile.openstreetmap.org/{z}/{x}/{y}.png"), + MapAttribution: envStr("HAUK_MAP_ATTRIBUTION", `© OpenStreetMap contributors`), + DefaultZoom: envInt("HAUK_DEFAULT_ZOOM", 14), + MaxZoom: envInt("HAUK_MAX_ZOOM", 19), + VelocityUnit: velUnit, + VelocityDataPts: envInt("HAUK_VELOCITY_DATA_PTS", 2), + TrailColor: envStr("HAUK_TRAIL_COLOR", "#d80037"), + OfflineTimeout: envInt("HAUK_OFFLINE_TIMEOUT", 30), + RequestTimeout: envInt("HAUK_REQUEST_TIMEOUT", 10), + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d88bf03 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module github.com/parkan/go-hauk + +go 1.22 + +require ( + github.com/go-ldap/ldap/v3 v3.4.6 + github.com/redis/go-redis/v9 v9.3.0 + golang.org/x/crypto v0.17.0 +) + +require ( + github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect + github.com/google/uuid v1.3.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2201a10 --- /dev/null +++ b/go.sum @@ -0,0 +1,74 @@ +github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 h1:mFRzDkZVAjdal+s7s0MwaRv9igoPqLRdzOLzw/8Xvq8= +github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU= +github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3UuJRqlA3JxYxBZEqCeOmATOvrbT4p9RA= +github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/go-asn1-ber/asn1-ber v1.5.5 h1:MNHlNMBDgEKD4TcKr36vQN68BA00aDfjIt3/bD50WnA= +github.com/go-asn1-ber/asn1-ber v1.5.5/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0= +github.com/go-ldap/ldap/v3 v3.4.6 h1:ert95MdbiG7aWo/oPYp9btL3KJlMPKnP58r09rI8T+A= +github.com/go-ldap/ldap/v3 v3.4.6/go.mod h1:IGMQANNtxpsOzj7uUAMjpGBaOVTC4DYyIy8VsTdxmtc= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0= +github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9f15b08 --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + "net/http" + + "github.com/parkan/go-hauk/api" + "github.com/parkan/go-hauk/config" + "github.com/parkan/go-hauk/store" +) + +func main() { + cfg := config.Load() + + redis, err := store.NewRedis(cfg.RedisAddr, cfg.RedisPassword, cfg.RedisPrefix) + if err != nil { + log.Fatalf("failed to connect to redis: %v", err) + } + + srv := api.NewServer(cfg, redis) + + log.Printf("starting hauk on %s", cfg.ListenAddr) + if err := http.ListenAndServe(cfg.ListenAddr, srv); err != nil { + log.Fatalf("server error: %v", err) + } +}