mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
rename api to cmd
This commit is contained in:
parent
4b246f6c51
commit
c3d5e3edc1
3 changed files with 0 additions and 0 deletions
23
api/apply.go
23
api/apply.go
|
@ -1,23 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/pkg/nftables"
|
||||
)
|
||||
|
||||
func apply(conf *definitions.Config) error {
|
||||
fileContent, err := nftables.GenerateNfTablesFile(*conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Generating nftables file %w", err)
|
||||
}
|
||||
|
||||
err = nftables.ApplyNfTablesFile(fileContent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Applying nftables %w", err)
|
||||
}
|
||||
slog.Info("Wrote nftables File!")
|
||||
return nil
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
)
|
||||
|
||||
func LoadConfiguration(file string) (*definitions.Config, error) {
|
||||
var config definitions.Config
|
||||
configFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening Config File %w", err)
|
||||
}
|
||||
defer configFile.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
jsonParser := json.NewDecoder(configFile)
|
||||
jsonParser.DisallowUnknownFields()
|
||||
err = jsonParser.Decode(&config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding Config File %w", err)
|
||||
}
|
||||
return &config, nil
|
||||
}
|
57
api/main.go
57
api/main.go
|
@ -1,57 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
applyPtr := flag.Bool("apply", false, "apply config and stop")
|
||||
flag.Parse()
|
||||
|
||||
slog.Info("Starting...")
|
||||
|
||||
conf, err := LoadConfiguration("config.json")
|
||||
if err != nil {
|
||||
slog.Error("Loading Config", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Info("Config Loaded", "config", conf)
|
||||
|
||||
if *applyPtr {
|
||||
slog.Info("Applying Config...")
|
||||
err := apply(conf)
|
||||
if err != nil {
|
||||
slog.Error("Applying Config", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Info("Config Applied, Exiting...")
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("Starting Webserver...")
|
||||
server.StartWebserver(conf)
|
||||
|
||||
slog.Info("Ready")
|
||||
|
||||
// Handle Exit Signal
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
slog.Info("Got Signal, Exiting...")
|
||||
|
||||
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer shutdownRelease()
|
||||
|
||||
server.ShutdownWebserver(shutdownCtx)
|
||||
|
||||
slog.Info("Done")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue