Skip to content

Commit 04b75c4

Browse files
authored
Merge pull request #31 from HilkopterBob/devel
Devel
2 parents 2f2bff3 + a2768ad commit 04b75c4

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ RUN \
2828
EXPOSE 8080
2929

3030
# Run
31-
CMD ["/app/packagelock"]
31+
CMD ["/app/packagelock start"]

config/conf-init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ConfigProvider interface {
2424
GetString(string string) string
2525
SetDefault(key string, value any)
2626
Get(key string) any
27+
GetBool(key string) bool
2728
}
2829

2930
// TODO: How to test?

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
2121
github.com/google/uuid v1.6.0 // indirect
2222
github.com/hashicorp/hcl v1.0.0 // indirect
23+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2324
github.com/klauspost/compress v1.17.9 // indirect
2425
github.com/magiconair/properties v1.8.7 // indirect
2526
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -33,6 +34,7 @@ require (
3334
github.com/sourcegraph/conc v0.3.0 // indirect
3435
github.com/spf13/afero v1.11.0 // indirect
3536
github.com/spf13/cast v1.6.0 // indirect
37+
github.com/spf13/cobra v1.8.1 // indirect
3638
github.com/spf13/pflag v1.0.5 // indirect
3739
github.com/subosito/gotenv v1.6.0 // indirect
3840
github.com/valyala/bytebufferpool v1.0.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/
44
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
55
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
66
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
7+
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
78
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
89
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
910
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -38,6 +39,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
3839
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3940
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
4041
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
42+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
43+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4144
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
4245
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
4346
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
@@ -78,6 +81,7 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
7881
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
7982
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
8083
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
84+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8185
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
8286
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
8387
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
@@ -91,6 +95,8 @@ github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
9195
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
9296
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
9397
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
98+
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
99+
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
94100
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
95101
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
96102
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=

main.go

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,85 @@ import (
1212
"time"
1313

1414
"github.com/fsnotify/fsnotify"
15+
"github.com/spf13/cobra"
1516
"github.com/spf13/viper"
1617
)
1718

18-
// Linker Injections
19-
// Version injection with Docker Build & ldflags
20-
// Do not modify, init or change in code!
21-
var AppVersion string
19+
var (
20+
restartChan = make(chan struct{})
21+
quitChan = make(chan os.Signal, 1)
22+
AppVersion string // Version injected with ldflags
23+
)
2224

23-
// TODO: support for multiple network adapters.
25+
// Root command using Cobra
26+
var rootCmd = &cobra.Command{
27+
Use: "packagelock",
28+
Short: "Packagelock CLI tool",
29+
Long: `Packagelock CLI manages the server and other operations.`,
30+
}
2431

25-
func main() {
26-
// Start Viper for config management
32+
// Start command to run the server
33+
var startCmd = &cobra.Command{
34+
Use: "start",
35+
Short: "Start the server",
36+
Run: func(cmd *cobra.Command, args []string) {
37+
startServer()
38+
},
39+
}
40+
41+
func init() {
42+
// Add commands to rootCmd
43+
rootCmd.AddCommand(startCmd)
44+
45+
// Initialize Viper config
46+
cobra.OnInitialize(initConfig)
47+
}
48+
49+
// initConfig initializes Viper and configures the application
50+
func initConfig() {
2751
config.Config = config.StartViper(viper.New())
2852

2953
// If AppVersion is injected, set it in the configuration
3054
if AppVersion != "" {
3155
config.Config.SetDefault("general.app-version", AppVersion)
3256
}
3357

58+
// Check and create self-signed certificates if missing
3459
if _, err := os.Stat(config.Config.GetString("network.ssl-config.certificatepath")); os.IsNotExist(err) {
3560
fmt.Println("Certificate files missing, creating new self-signed.")
36-
err := certs.CreateSelfSignedCert(config.Config.GetString("network.ssl-config.certificatepath"), config.Config.GetString("network.ssl-config.privatekeypath"))
61+
err := certs.CreateSelfSignedCert(
62+
config.Config.GetString("network.ssl-config.certificatepath"),
63+
config.Config.GetString("network.ssl-config.privatekeypath"))
3764
if err != nil {
3865
fmt.Printf("Error creating self-signed certificate: %v\n", err)
39-
return
66+
os.Exit(1)
4067
}
4168
}
69+
}
4270

71+
// startServer starts the Fiber server with appropriate configuration
72+
func startServer() {
4373
fmt.Println(config.Config.AllSettings())
4474

45-
// Channel to signal the restart
46-
restartChan := make(chan struct{})
47-
quitChan := make(chan os.Signal, 1)
4875
signal.Notify(quitChan, os.Interrupt, syscall.SIGTERM)
4976

5077
// Start the server in a goroutine
5178
go func() {
5279
for {
53-
// Add Fiber routes
5480
router := server.AddRoutes(config.Config)
5581

56-
// Fiber does not use the standard http.Server
5782
// Setup server address from config
5883
serverAddr := config.Config.GetString("network.fqdn") + ":" + config.Config.GetString("network.port")
5984

60-
// Fiber specific server start
85+
// Start server based on SSL config
6186
go func() {
62-
fmt.Printf("Starting Fiber HTTPS server at https://%s...\n", serverAddr)
63-
64-
// start ssl session if ssl:true is set in config file, else start http
65-
if config.Config.Get("network.ssl") == true {
66-
if err := server.ListenAndServeTLS(router.Router, config.Config.GetString("network.ssl-config.certificatepath"), config.Config.GetString("network.ssl-config.privatekeypath"), serverAddr); err != nil {
87+
if config.Config.GetBool("network.ssl") {
88+
fmt.Printf("Starting Fiber HTTPS server at https://%s...\n", serverAddr)
89+
if err := server.ListenAndServeTLS(
90+
router.Router,
91+
config.Config.GetString("network.ssl-config.certificatepath"),
92+
config.Config.GetString("network.ssl-config.privatekeypath"),
93+
serverAddr); err != nil {
6794
fmt.Printf("Server error: %s\n", err)
6895
}
6996
} else {
@@ -74,12 +101,10 @@ func main() {
74101
}
75102
}()
76103

77-
// Wait for either a restart signal or termination signal
104+
// Handle restart or quit signals
78105
select {
79106
case <-restartChan:
80107
fmt.Println("Restarting Fiber server...")
81-
82-
// Gracefully shutdown the Fiber server
83108
_, cancel := context.WithTimeout(context.Background(), 5*time.Second)
84109
defer cancel()
85110
if err := router.Router.Shutdown(); err != nil {
@@ -90,8 +115,6 @@ func main() {
90115

91116
case <-quitChan:
92117
fmt.Println("Shutting down Fiber server...")
93-
94-
// Gracefully shutdown on quit signal
95118
_, cancel := context.WithTimeout(context.Background(), 5*time.Second)
96119
defer cancel()
97120
if err := router.Router.Shutdown(); err != nil {
@@ -104,15 +127,23 @@ func main() {
104127
}
105128
}()
106129

107-
// Watch for configuration changes
130+
// Watch for config changes
108131
config.Config.OnConfigChange(func(e fsnotify.Event) {
109132
fmt.Println("Config file changed:", e.Name)
110133
fmt.Println("Restarting to apply changes...")
111-
restartChan <- struct{}{} // Send signal to restart the server
134+
restartChan <- struct{}{}
112135
})
113136
config.Config.WatchConfig()
114137

115138
// Block until quit signal is received
116139
<-quitChan
117140
fmt.Println("Main process exiting.")
118141
}
142+
143+
func main() {
144+
// Execute the Cobra root command
145+
if err := rootCmd.Execute(); err != nil {
146+
fmt.Println(err)
147+
os.Exit(1)
148+
}
149+
}

templates/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ <h1>404 - Page not found 😢</h1>
157157
let distance = Math.sqrt(dx * dx + dy * dy);
158158

159159
if (distance < maxDistance) {
160-
let opacity = 1 - distance / maxDistance;
160+
let opacity = 1 - (distance / maxDistance);
161161
ctx.strokeStyle = `rgba(0, 0, 0, ${opacity})`;
162162
ctx.lineWidth = 1;
163163
ctx.beginPath();

0 commit comments

Comments
 (0)