Improving code...

This commit is contained in:
Juan Font Alonso 2021-02-21 22:14:38 +01:00
parent 5d3c80a192
commit 1b7d99d483

5
app.go
View File

@ -10,6 +10,7 @@ import (
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
) )
// Config contains the initial Headscale configuration
type Config struct { type Config struct {
ServerURL string ServerURL string
Addr string Addr string
@ -23,6 +24,7 @@ type Config struct {
DBpass string DBpass string
} }
// Headscale represents the base app of the service
type Headscale struct { type Headscale struct {
cfg Config cfg Config
dbString string dbString string
@ -30,6 +32,7 @@ type Headscale struct {
privateKey *wgcfg.PrivateKey privateKey *wgcfg.PrivateKey
} }
// NewHeadscale returns the Headscale app
func NewHeadscale(cfg Config) (*Headscale, error) { func NewHeadscale(cfg Config) (*Headscale, error) {
content, err := ioutil.ReadFile(cfg.PrivateKeyPath) content, err := ioutil.ReadFile(cfg.PrivateKeyPath)
if err != nil { if err != nil {
@ -54,6 +57,7 @@ func NewHeadscale(cfg Config) (*Headscale, error) {
return &h, nil return &h, nil
} }
// Serve launches a GIN server with the Headscale API
func (h *Headscale) Serve() error { func (h *Headscale) Serve() error {
r := gin.Default() r := gin.Default()
r.GET("/key", h.KeyHandler) r.GET("/key", h.KeyHandler)
@ -67,6 +71,7 @@ func (h *Headscale) Serve() error {
return err return err
} }
// RegisterMachine is executed from the CLI to register a new Machine using its MachineKey
func (h *Headscale) RegisterMachine(key string) error { func (h *Headscale) RegisterMachine(key string) error {
mKey, err := wgcfg.ParseHexKey(key) mKey, err := wgcfg.ParseHexKey(key)
if err != nil { if err != nil {