member-console/cmd/start.go

69 lines
2.0 KiB
Go

/*
Copyright © 2025 Wiki Cafe <mail@wiki.cafe>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"log"
"net/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var port string
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "Start serving the member-console web application",
Long: `The start command starts an HTTP server that serves the member-console web
application.
The server listens on port 8080 by default, unless a different port is specified using the --port flag.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("start called")
// Define the directory containing the static HTML files
dir := http.Dir("./components")
// Create a file server to serve files from the specified directory
fileServer := http.FileServer(dir)
// Use a ServeMux to handle requests
httpRequestRouter := http.NewServeMux()
// Serve files for the root path ("/")
httpRequestRouter.Handle("/", fileServer)
// Start the HTTP server on the specified port
log.Printf("Starting server on :%s...\n", port)
err := http.ListenAndServe(":"+port, httpRequestRouter)
if err != nil {
log.Fatal(err)
}
},
}
func init() {
startCmd.Flags().StringVar(&port, "port", "8080", "port to listen on")
viper.BindPFlag("port", startCmd.Flags().Lookup("port"))
rootCmd.AddCommand(startCmd)
}