-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"github.com/webishdev/stopnik/internal/config" | ||
"github.com/webishdev/stopnik/internal/crypto" | ||
"github.com/webishdev/stopnik/internal/server" | ||
logger "github.com/webishdev/stopnik/log" | ||
"gopkg.in/yaml.v3" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func printVersion(version string, gitHash string) { | ||
fmt.Printf("STOPnik %s - %s\n", version, gitHash) | ||
} | ||
|
||
func printHelp(version string, gitHash string) { | ||
fmt.Printf("STOPnik %s - %s\n\n", version, gitHash) | ||
flag.Usage() | ||
} | ||
|
||
func readPassword() { | ||
fmt.Printf("Password: ") | ||
passwordScanner := bufio.NewScanner(os.Stdin) | ||
passwordScanner.Scan() | ||
password := passwordScanner.Text() | ||
fmt.Printf("Salt: ") | ||
saltScanner := bufio.NewScanner(os.Stdin) | ||
saltScanner.Scan() | ||
salt := saltScanner.Text() | ||
result := crypto.Sha512SaltedHash(password, salt) | ||
fmt.Printf("Hashed value is: %s\n\n", result) | ||
} | ||
|
||
func start(configurationFile *string) error { | ||
configLoader := config.NewConfigLoader(os.ReadFile, yaml.Unmarshal) | ||
|
||
currentConfig, configError := readConfiguration(configurationFile, configLoader) | ||
if configError != nil { | ||
return configError | ||
} | ||
|
||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
stopnikServer := server.NewStopnikServer(currentConfig) | ||
|
||
go func() { | ||
sig := <-sigs | ||
logger.Debug("Received signal %s", sig) | ||
stopnikServer.Shutdown() | ||
}() | ||
|
||
stopnikServer.Start() | ||
|
||
return nil | ||
} | ||
|
||
func readConfiguration(configurationFile *string, configLoader *config.Loader) (*config.Config, error) { | ||
currentConfig, configError := configLoader.LoadConfig(*configurationFile) | ||
if configError != nil { | ||
fmt.Printf("STOPnik %s - %s\n\n", Version, GitHash) | ||
fmt.Printf("%v", configError) | ||
return nil, configError | ||
} | ||
logger.SetLogLevel(currentConfig.Server.LogLevel) | ||
logger.Info("Config loaded from %s", *configurationFile) | ||
|
||
return currentConfig, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
) | ||
|
||
var Version = "development" | ||
var GitHash = "none" | ||
|
||
func main() { | ||
isHelp := flag.Bool("help", false, "Show help message") | ||
showVersion := flag.Bool("version", false, "Show version information") | ||
askPassword := flag.Bool("password", false, "Ask for password and salt to create hash") | ||
configurationFile := flag.String("file", "config.yml", "Configuration file to use") | ||
flag.Parse() | ||
|
||
if *isHelp { | ||
printHelp(Version, GitHash) | ||
os.Exit(0) | ||
} else if *showVersion { | ||
printVersion(Version, GitHash) | ||
os.Exit(0) | ||
} else if *askPassword { | ||
readPassword() | ||
os.Exit(0) | ||
} | ||
|
||
startError := start(configurationFile) | ||
if startError != nil { | ||
os.Exit(1) | ||
} | ||
} |