Initial commit
This commit is contained in:
commit
82d714d8a3
9 changed files with 251 additions and 0 deletions
56
main.go
Normal file
56
main.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
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)
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue