56 lines
1 KiB
Go
56 lines
1 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", http.FileServer(http.Dir("./static")))
|
|
|
|
mux.HandleFunc("GET /command/{command}", commandHandler)
|
|
|
|
port := 8080
|
|
log.Printf("Running on port %d\n", port)
|
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), mux); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func commandHandler(w http.ResponseWriter, r *http.Request) {
|
|
command := r.PathValue("command")
|
|
|
|
log.Printf("Handling command %s for %s\n", command, r.RemoteAddr)
|
|
|
|
c, ok := commands[command]
|
|
if !ok {
|
|
fmt.Fprintf(w, "Unknown command: %s", command)
|
|
return
|
|
}
|
|
c.Action(w, []string{})
|
|
}
|
|
|
|
type command interface {
|
|
Usage(w io.Writer)
|
|
Action(w io.Writer, args []string)
|
|
}
|
|
|
|
var commands = map[string]command{
|
|
"help": &helpCommand{},
|
|
}
|
|
|
|
type helpCommand struct {}
|
|
|
|
func (c *helpCommand) Usage(w io.Writer) {
|
|
fmt.Fprintf(w, "Usage: help <command>")
|
|
}
|
|
|
|
func (c *helpCommand) Action(w io.Writer, args []string) {
|
|
fmt.Fprint(w, "Invalid input\r\n")
|
|
c.Usage(w)
|
|
}
|
|
|