1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00

Remove dependency on postgres' jsonb

This commit is contained in:
Juan Font Alonso 2021-05-15 00:05:41 +02:00
parent e7a626d3cc
commit e0d916bb5a
4 changed files with 17 additions and 17 deletions

10
api.go
View File

@ -11,8 +11,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"gorm.io/datatypes"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
@ -40,7 +40,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
<body> <body>
<h1>headscale</h1> <h1>headscale</h1>
<p> <p>
Run the command below on the headscale server to add this machine to your network: Run the command below in the headscale server to add this machine to your network:
</p> </p>
<p> <p>
@ -174,13 +174,13 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
defer db.Close() defer db.Close()
var m Machine var m Machine
if db.First(&m, "machine_key = ?", mKey.HexString()).RecordNotFound() { if db.First(&m, "machine_key = ?", mKey.HexString()).RecordNotFound() {
log.Printf("Cannot find machine: %s", err) log.Printf("Cannot fingitd machine: %s", err)
return return
} }
hostinfo, _ := json.Marshal(req.Hostinfo) hostinfo, _ := json.Marshal(req.Hostinfo)
m.Name = req.Hostinfo.Hostname m.Name = req.Hostinfo.Hostname
m.HostInfo = postgres.Jsonb{RawMessage: json.RawMessage(hostinfo)} m.HostInfo = datatypes.JSON(hostinfo)
m.DiscoKey = wgcfg.Key(req.DiscoKey).HexString() m.DiscoKey = wgcfg.Key(req.DiscoKey).HexString()
now := time.Now().UTC() now := time.Now().UTC()
@ -194,7 +194,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
// before their first real endpoint update. // before their first real endpoint update.
if !req.ReadOnly { if !req.ReadOnly {
endpoints, _ := json.Marshal(req.Endpoints) endpoints, _ := json.Marshal(req.Endpoints)
m.Endpoints = postgres.Jsonb{RawMessage: json.RawMessage(endpoints)} m.Endpoints = datatypes.JSON(endpoints)
m.LastSeen = &now m.LastSeen = &now
} }
db.Save(&m) db.Save(&m)

View File

@ -8,7 +8,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/jinzhu/gorm/dialects/postgres" "gorm.io/datatypes"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
@ -33,9 +33,9 @@ type Machine struct {
LastSeen *time.Time LastSeen *time.Time
Expiry *time.Time Expiry *time.Time
HostInfo postgres.Jsonb HostInfo datatypes.JSON
Endpoints postgres.Jsonb Endpoints datatypes.JSON
EnabledRoutes postgres.Jsonb EnabledRoutes datatypes.JSON
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
@ -79,7 +79,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
allowedIPs = append(allowedIPs, ip) // we append the node own IP, as it is required by the clients allowedIPs = append(allowedIPs, ip) // we append the node own IP, as it is required by the clients
routesStr := []string{} routesStr := []string{}
if len(m.EnabledRoutes.RawMessage) != 0 { if len(m.EnabledRoutes) != 0 {
allwIps, err := m.EnabledRoutes.MarshalJSON() allwIps, err := m.EnabledRoutes.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err
@ -99,7 +99,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
} }
endpoints := []string{} endpoints := []string{}
if len(m.Endpoints.RawMessage) != 0 { if len(m.Endpoints) != 0 {
be, err := m.Endpoints.MarshalJSON() be, err := m.Endpoints.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err
@ -111,7 +111,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
} }
hostinfo := tailcfg.Hostinfo{} hostinfo := tailcfg.Hostinfo{}
if len(m.HostInfo.RawMessage) != 0 { if len(m.HostInfo) != 0 {
hi, err := m.HostInfo.MarshalJSON() hi, err := m.HostInfo.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err
@ -198,7 +198,7 @@ func (h *Headscale) GetMachine(namespace string, name string) (*Machine, error)
// GetHostInfo returns a Hostinfo struct for the machine // GetHostInfo returns a Hostinfo struct for the machine
func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) { func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) {
hostinfo := tailcfg.Hostinfo{} hostinfo := tailcfg.Hostinfo{}
if len(m.HostInfo.RawMessage) != 0 { if len(m.HostInfo) != 0 {
hi, err := m.HostInfo.MarshalJSON() hi, err := m.HostInfo.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -5,7 +5,7 @@ import (
"errors" "errors"
"log" "log"
"github.com/jinzhu/gorm/dialects/postgres" "gorm.io/datatypes"
"inet.af/netaddr" "inet.af/netaddr"
) )
@ -49,7 +49,7 @@ func (h *Headscale) EnableNodeRoute(namespace string, nodeName string, routeStr
} }
routes, _ := json.Marshal([]string{routeStr}) // TODO: only one for the time being, so overwriting the rest routes, _ := json.Marshal([]string{routeStr}) // TODO: only one for the time being, so overwriting the rest
m.EnabledRoutes = postgres.Jsonb{RawMessage: json.RawMessage(routes)} m.EnabledRoutes = datatypes.JSON(routes)
db.Save(&m) db.Save(&m)
db.Close() db.Close()

View File

@ -3,8 +3,8 @@ package headscale
import ( import (
"encoding/json" "encoding/json"
"github.com/jinzhu/gorm/dialects/postgres"
"gopkg.in/check.v1" "gopkg.in/check.v1"
"gorm.io/datatypes"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
) )
@ -46,7 +46,7 @@ func (s *Suite) TestGetRoutes(c *check.C) {
Registered: true, Registered: true,
RegisterMethod: "authKey", RegisterMethod: "authKey",
AuthKeyID: uint(pak.ID), AuthKeyID: uint(pak.ID),
HostInfo: postgres.Jsonb{RawMessage: json.RawMessage(hostinfo)}, HostInfo: datatypes.JSON(hostinfo),
} }
db.Save(&m) db.Save(&m)