platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
91
selective-vpn-api/app/transport_policy_runtime_state.go
Normal file
91
selective-vpn-api/app/transport_policy_runtime_state.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
transportPolicyRuntimeStatePath = transportPolicyRuntimePath
|
||||
transportPolicyRuntimeSnapPath = transportPolicyRuntimeSnap
|
||||
)
|
||||
|
||||
type transportPolicyRuntimeState struct {
|
||||
Version int `json:"version"`
|
||||
UpdatedAt string `json:"updated_at,omitempty"`
|
||||
PolicyRevision int64 `json:"policy_revision,omitempty"`
|
||||
ApplyID string `json:"apply_id,omitempty"`
|
||||
InterfaceCount int `json:"interface_count"`
|
||||
RuleCount int `json:"rule_count"`
|
||||
Interfaces []TransportPolicyCompileInterface `json:"interfaces,omitempty"`
|
||||
}
|
||||
|
||||
func loadTransportPolicyRuntimeState() transportPolicyRuntimeState {
|
||||
st := transportPolicyRuntimeState{Version: transportStateVersion}
|
||||
data, err := os.ReadFile(transportPolicyRuntimeStatePath)
|
||||
if err != nil {
|
||||
return st
|
||||
}
|
||||
if err := json.Unmarshal(data, &st); err != nil {
|
||||
return transportPolicyRuntimeState{Version: transportStateVersion}
|
||||
}
|
||||
if st.Version == 0 {
|
||||
st.Version = transportStateVersion
|
||||
}
|
||||
if st.Interfaces == nil {
|
||||
st.Interfaces = nil
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
func saveTransportPolicyRuntimeState(st transportPolicyRuntimeState) error {
|
||||
st.Version = transportStateVersion
|
||||
st.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
|
||||
data, err := json.MarshalIndent(st, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(transportPolicyRuntimeStatePath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp := transportPolicyRuntimeStatePath + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, transportPolicyRuntimeStatePath)
|
||||
}
|
||||
|
||||
func saveTransportPolicyRuntimeSnapshot(st transportPolicyRuntimeState) error {
|
||||
data, err := json.MarshalIndent(st, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(transportPolicyRuntimeSnapPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp := transportPolicyRuntimeSnapPath + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, transportPolicyRuntimeSnapPath)
|
||||
}
|
||||
|
||||
func loadTransportPolicyRuntimeSnapshot() (transportPolicyRuntimeState, bool) {
|
||||
data, err := os.ReadFile(transportPolicyRuntimeSnapPath)
|
||||
if err != nil {
|
||||
return transportPolicyRuntimeState{}, false
|
||||
}
|
||||
var st transportPolicyRuntimeState
|
||||
if err := json.Unmarshal(data, &st); err != nil {
|
||||
return transportPolicyRuntimeState{}, false
|
||||
}
|
||||
if st.Version == 0 {
|
||||
st.Version = transportStateVersion
|
||||
}
|
||||
if st.Interfaces == nil {
|
||||
st.Interfaces = nil
|
||||
}
|
||||
return st, true
|
||||
}
|
||||
Reference in New Issue
Block a user