2020-07-13 22:01:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2021-01-08 21:37:38 +00:00
|
|
|
"os"
|
2020-07-13 22:01:42 +00:00
|
|
|
|
2021-01-08 21:37:38 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2020-07-13 22:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|