rename api to cmd

This commit is contained in:
Samuel Lorch 2023-03-05 21:01:42 +01:00
parent 4b246f6c51
commit c3d5e3edc1
3 changed files with 0 additions and 0 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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")
}