package main import ( "io/ioutil" "log" "os" uuid "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 }