Skip to content

Commit d0fd3f1

Browse files
authored
Scaffolding for configuration + preparation for setting passphrase (#192)
* Scaffolding for configuration + preparation for setting passphrase * Setup config on dev & user installation * dev installation/uninstallation fixes
1 parent 9513b8c commit d0fd3f1

File tree

13 files changed

+128
-245
lines changed

13 files changed

+128
-245
lines changed

.goreleaser.yml

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ nfpms:
9090
file_info:
9191
mode: 0644
9292

93+
- src: etc/piwebagent2/config/config.env
94+
dst: /etc/piwebagent2/config/config.env
95+
type: "config|noreplace"
96+
9397
- src: usr/share/piwebagent2/assets
9498
dst: /usr/share/piwebagent2/assets
9599

install.sh

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ function install_assets() {
2929
cp -r $SHARED_PATH /$SHARED_PATH
3030
}
3131

32+
function install_config() {
33+
mkdir -p $CONFIG_DIR
34+
cp $CONFIG_FILE /$CONFIG_FILE
35+
chown -R piwebagent2 /$CONFIG_FILE
36+
}
37+
3238
function prepare_unpack() {
3339
target=$(mktemp -d)
3440
cp target/piwebagent2.zip $target/
@@ -45,6 +51,7 @@ install_binary
4551
install_assets
4652
create_user
4753
chown piwebagent2 -R /$SHARED_PATH
54+
install_config
4855
sudoer_user_priviledges
4956
register_service
5057
start_service

installation_paths

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ SERVICE_PATH=lib/systemd/system/piwebagent2.service
33
BINARY_PATH=usr/bin/piwebagent2
44
SHARED_PATH=usr/share/piwebagent2
55
SUDOERS_PATH=etc/sudoers.d/piwebagent2
6+
CONFIG_DIR=etc/piwebagent2/config
7+
CONFIG_FILE=${CONFIG_DIR}/config.env

scripts/postinstall.sh

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ echo "Enabling piwebagent2.service"
66
systemctl enable piwebagent2.service
77
echo "Starting piwebagent2.service"
88
systemctl start piwebagent2.service
9+
10+
echo "Giving permissions to the config directory"
11+
chown -R piwebagent2 /etc/piwebagent2/config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PASSWORD_HASH=
2+
PORT_NUMBER=8080

service/web/cmd/pi-web-agent.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"io"
56
"log"
67
"net/http"
@@ -27,5 +28,7 @@ func main() {
2728
http.HandleFunc("/api/control/stream", action_dispatcher_handler)
2829
http.Handle("/", http.FileServer(http.Dir("assets")))
2930

30-
log.Fatal(http.ListenAndServe(":8080", nil))
31+
addr := fmt.Sprintf(":%d", single_user_session.PWA_Config.Port())
32+
33+
log.Fatal(http.ListenAndServe(addr, nil))
3134
}

service/web/configs/.actions

-92
This file was deleted.

service/web/configs/config.cfg

-131
This file was deleted.

service/web/configs/pm/recommendationsList.txt

-18
This file was deleted.

service/web/internal/configuration.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package shell
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
type RPI_Config interface {
10+
Port() uint32
11+
Password_Hash() string
12+
Update_Passphrase(phrase string)
13+
}
14+
15+
type Dynamic_RPI_Config struct {
16+
config *viper.Viper
17+
}
18+
19+
const (
20+
PASSWORD_HASH_KEY = "PASSWORD_HASH"
21+
PORT_NUMBER_KEY = "PORT_NUMBER"
22+
)
23+
24+
func (config *Dynamic_RPI_Config) Port() uint32 {
25+
return config.config.GetUint32(PORT_NUMBER_KEY)
26+
}
27+
28+
func (config *Dynamic_RPI_Config) Password_Hash() string {
29+
return config.config.GetString(PASSWORD_HASH_KEY)
30+
}
31+
32+
func (config *Dynamic_RPI_Config) Update_Passphrase(phrase string) {
33+
config.config.Set(PASSWORD_HASH_KEY, phrase)
34+
config.config.WriteConfig()
35+
}
36+
37+
func Load_PWA_config(path string) RPI_Config {
38+
viper_config, _ := load_PWA_config(path)
39+
return &Dynamic_RPI_Config{viper_config}
40+
41+
}
42+
43+
func Load_RPI_PWA_Config() RPI_Config {
44+
return Load_PWA_config("/etc/piwebagent2/config")
45+
}
46+
47+
func load_PWA_config(path string) (*viper.Viper, error) {
48+
config := viper.New()
49+
config.SetDefault(PASSWORD_HASH_KEY, "")
50+
config.SetDefault(PORT_NUMBER_KEY, uint32(8080))
51+
config.AddConfigPath(path)
52+
config.SetConfigType("env")
53+
config.WatchConfig()
54+
config.ReadInConfig()
55+
err := config.SafeWriteConfig()
56+
if err != nil {
57+
fmt.Printf("Error writing config for the first time %s\n", err.Error())
58+
}
59+
return config, err
60+
61+
}

0 commit comments

Comments
 (0)