Add custom port flag
This commit is contained in:
parent
e2ab8a2110
commit
1337789403
|
@ -6,6 +6,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
)
|
||||
|
||||
// serveCmd represents the serve command
|
||||
|
@ -14,6 +15,11 @@ var serveCmd = &cobra.Command{
|
|||
Short: "Start HTTP server",
|
||||
Long: `Start HTTP server`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := setServerParameters(cmd); err != nil {
|
||||
log.Fatalf("Error happened trying to set parameters: %v\n", err)
|
||||
}
|
||||
|
||||
server()
|
||||
},
|
||||
}
|
||||
|
@ -31,3 +37,14 @@ func init() {
|
|||
// is called directly, e.g.:
|
||||
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
func setServerParameters(cmd *cobra.Command) error {
|
||||
parameterPort, err := cmd.Flags().GetString("port")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if parameterPort != "" {
|
||||
listenPort = parameterPort
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -10,6 +10,10 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
listenPort string = "1302"
|
||||
)
|
||||
|
||||
//go:embed assets
|
||||
var assetsFolder embed.FS
|
||||
|
||||
|
@ -17,6 +21,7 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprintln(w, "Successful upload!")
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent())
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error happened receiving file: %v\n", err)
|
||||
|
@ -34,7 +39,6 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
|
|||
log.Printf("Error happened opening file: %v\n", err)
|
||||
}
|
||||
|
||||
log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent())
|
||||
io.Copy(f, file)
|
||||
}
|
||||
|
||||
|
@ -47,9 +51,9 @@ func server() {
|
|||
http.Handle("/", http.FileServer(http.FS(serverRoot)))
|
||||
http.HandleFunc("/api/upload", uploadFile)
|
||||
|
||||
fmt.Println("Listening on port 8080...")
|
||||
fmt.Printf("Listening on port %v...\n", listenPort)
|
||||
|
||||
err = http.ListenAndServe(":8080", nil)
|
||||
err = http.ListenAndServe(":"+listenPort, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue