-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (57 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"os"
"strings"
"github.com/akamensky/argparse"
"github.com/happer64bit/froop/server"
)
func main() {
// Create a new parser object
parser := argparse.NewParser("myserver", "A simple HTTP server")
// Create the 'serve' command
serveCmd := parser.NewCommand("serve", "Start the HTTP server")
// Add command-line options for port and address
port := serveCmd.String("p", "port", &argparse.Options{
Help: "Port to run the server on",
Default: "8080",
})
address := serveCmd.String("a", "address", &argparse.Options{
Help: "Address to bind the server to",
Default: "localhost",
})
// Add an optional authentication flag
auth := serveCmd.String("", "auth", &argparse.Options{
Help: "Enable authentication with the format username:password",
Required: false,
})
// Add a verbose flag for logging
verbose := serveCmd.Flag("v", "verbose", &argparse.Options{
Help: "Enable verbose logging",
})
// Parse the command-line arguments
err := parser.Parse(os.Args)
if err != nil {
fmt.Println(parser.Usage(err))
os.Exit(1)
}
// Check if the 'serve' command was provided
if len(os.Args) < 2 || os.Args[1] != "serve" {
fmt.Println("Usage: <program> serve [--port=PORT] [--address=ADDRESS] [--auth=username:password] [--verbose]")
os.Exit(1)
}
// Handle authentication parsing
var username, password string
if *auth != "" {
parts := strings.SplitN(*auth, ":", 2)
if len(parts) != 2 {
fmt.Println("Error: --auth should be in the format username:password.")
fmt.Println(parser.Usage(nil))
os.Exit(1)
}
username = parts[0]
password = parts[1]
}
// Start the server with or without authentication based on the presence of the --auth flag
server.StartServer(*address, *port, ".", username, password, *verbose)
}