Skip to content

Commit

Permalink
main: autodetect OCTOPRINT_HOST, fixes Z-Bolt#9
Browse files Browse the repository at this point in the history
Signed-off-by: Máximo Cuadros <[email protected]>
  • Loading branch information
mcuadros committed Jan 13, 2018
1 parent 1218dd5 commit 7ce7128
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ Configuration
The basic configuration is handled via environment variables, if you are using
the `.deb` package you can configure it at `/etc/octoprint-tft-environment`.

- `OCTOPRINT_HOST` - OctoPrint HTTP address, default `http://localhost`
- `OCTOPRINT_APIKEY` - OctoPrint-TFT expects an [API key]( http://docs.octoprint.org/en/master/api/general.html) to be supplied. This API key can be either the globally configured one or a user specific one if “Access Control”.
- `OCTOPRINT_CONFIG_FILE` - Location of the OctoPrint's config.yaml file, if `OCTOPRINT_APIKEY` is empty a the global API key will be read from the config file. If empty the file will be searched at the `pi` home folder or the current user.
- `OCTOPRINT_CONFIG_FILE` - Location of the OctoPrint's config.yaml file. If empty the file will be searched at the `pi` home folder or the current user. Only used for locally installed OctoPrint servers.

- `OCTOPRINT_HOST` - OctoPrint HTTP address, example `http://localhost:5000`, if OctoPrint is locally installed will be read from the config file.

- `OCTOPRINT_APIKEY` - OctoPrint-TFT expects an [API key]( http://docs.octoprint.org/en/master/api/general.html) to be supplied. This API key can be either the globally configured one or a user specific one if “Access Control”. if OctoPrint is locally installed will be read from the config file.

- `OCTOPRINT_TFT_STYLE_PATH` - Several themes are supported, and style configurations can be done through CSS. This variable defines the location of the application theme.

- `OCTOPRINT_TFT_RESOLUTION` - Resolution of the application, should be configured to the resolution of your screen, for example `800x480`. By default `480x320`.


Expand Down
16 changes: 9 additions & 7 deletions debian/local/octoprint-tft-environment
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# OctoPrint HTTP address, default http://localhost
# Location of the OctoPrint's config.yaml file. If empty the file will
# be search at the `pi` home folder or the current user. Only used for locally
# installed OctoPrint servers.
OCTOPRINT_CONFIG_FILE=

# OctoPrint HTTP address, example `http://localhost:5000`, if OctoPrint is
# locally installed will be read from the config file.
OCTOPRINT_HOST=

# OctoPrint-TFT expects an API key to be supplied. This API key can be either
# the globally configured one or a user specific one if “Access Control”.
# http://docs.octoprint.org/en/master/api/general.html
# http://docs.octoprint.org/en/master/api/general.html, if OctoPrint is
# locally installed will be read from the config file.
OCTOPRINT_APIKEY=

# Location of the OctoPrint's config.yaml file, if OCTOPRINT_APIKEY is empty
# a the gobal API will be read from the config file. If empty the file will
# be search at the `pi` home folder or the current user.
OCTOPRINT_CONFIG_FILE=

# Location of the application theme.
OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/

Expand Down
74 changes: 54 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"

"github.com/gotk3/gotk3/gtk"
"github.com/mcuadros/OctoPrint-TFT/ui"

"github.com/gotk3/gotk3/gtk"
"gopkg.in/yaml.v1"
)

Expand All @@ -19,8 +21,6 @@ const (
EnvBaseURL = "OCTOPRINT_HOST"
EnvAPIKey = "OCTOPRINT_APIKEY"
EnvConfigFile = "OCTOPRINT_CONFIG_FILE"

DefaultBaseURL = "http://localhost"
)

var (
Expand All @@ -32,22 +32,28 @@ var (

func init() {
ui.StylePath = os.Getenv(EnvStylePath)
APIKey = os.Getenv(EnvAPIKey)
Resolution = os.Getenv(EnvResolution)

BaseURL = os.Getenv(EnvBaseURL)
if BaseURL == "" {
BaseURL = DefaultBaseURL
}

ConfigFile = os.Getenv(EnvConfigFile)
if ConfigFile == "" {
ConfigFile = findConfigFile()
}

if APIKey == "" && ConfigFile != "" {
APIKey = readAPIKey(ConfigFile)
ui.Logger.Infof("Found API key at %q file", ConfigFile)
cfg := readConfig(ConfigFile)

BaseURL = os.Getenv(EnvBaseURL)
if BaseURL == "" {
BaseURL = fmt.Sprintf("http://%s:%d", cfg.Server.Host, cfg.Server.Port)
ui.Logger.Infof("Using %q as server address", BaseURL)

}

APIKey = os.Getenv(EnvAPIKey)
if APIKey == "" {
APIKey = cfg.API.Key
if cfg.API.Key != "" {
ui.Logger.Infof("Found API key at %q file", ConfigFile)
}
}
}

Expand All @@ -68,21 +74,49 @@ var (
homeOctoPi = "/home/pi/"
)

func readAPIKey(config string) string {
var cfg struct{ API struct{ Key string } }
type config struct {
// API Settings.
API struct {
// Key is the current API key needed for accessing the API.
Key string
}
// Server settings.
Server struct {
// Hosts define the host to which to bind the server, defaults to "0.0.0.0".
Host string
// Port define the port to which to bind the server, defaults to 5000.
Port int
}
}

func readConfig(configFile string) *config {
cfg := &config{}
if configFile == "" {
return cfg
}

ui.Logger.Infof("OctoPrint's config file found: %q", configFile)

data, err := ioutil.ReadFile(config)
data, err := ioutil.ReadFile(configFile)
if err != nil {
ui.Logger.Fatal(err)
return ""
return cfg
}

if err := yaml.Unmarshal([]byte(data), &cfg); err != nil {
ui.Logger.Fatalf("Error decoding YAML config file %q: %s", config, err)
return ""
if err := yaml.Unmarshal([]byte(data), cfg); err != nil {
ui.Logger.Fatalf("Error decoding YAML config file %q: %s", configFile, err)
return cfg
}

if cfg.Server.Host == "" {
cfg.Server.Host = "localhost"
}

if cfg.Server.Port == 0 {
cfg.Server.Port = 5000
}

return cfg.API.Key
return cfg
}

func findConfigFile() string {
Expand Down

0 comments on commit 7ce7128

Please sign in to comment.