Go binary/server for helping run/build sessions for the Maze Rats TTRPG
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

31 lines
706 B

package handlers
import (
"github.com/gorilla/mux"
"mazeratsgen/internal/dice"
"net/http"
"time"
)
// @Summary Roll some dice
// @Tags dice
// @Produce application/json, text/plain
// @Method GET
// @Router /api/roll/{xdy} [get]
// @Param xdy path string true "Roll in xdy format (e.g. 1d6)"
// @Success 200
func Roll(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
roller := dice.NewRoller(time.Now().UnixNano())
response, err := roller.Roll(vars["xdy"])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
// TODO: Return json error
w.Write([]byte(err.Error()))
return
}
template := "{{ range . }}{{ . }}\n{{end}}"
WriteResponse(w, r, response, template)
}