node-api/config.go
2020-07-14 00:01:42 +02:00

42 lines
742 B
Go

package main
import (
"io/ioutil"
"os"
"log"
"github.com/satori/go.uuid"
)
const configDirectory = "/var/lib/node-api"
const tokenFilename = "/var/lib/node-api/token"
func setToken() string {
if _, err := os.Stat(configDirectory); os.IsNotExist(err) {
err = os.MkdirAll(configDirectory, 0700)
if err != nil {
log.Fatalln(err)
}
}
// Load token from the file
var token string
if _, err := os.Stat(tokenFilename); os.IsNotExist(err) {
token = uuid.NewV4().String()
err = ioutil.WriteFile(tokenFilename, []byte(token), 0600)
if err != nil {
log.Fatalln(err)
}
} else {
tokenRaw, err := ioutil.ReadFile(tokenFilename)
if err != nil {
log.Fatalln(err)
}
token = string(tokenRaw)
}
return token
}