65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package action
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/akikareha/himewiki/internal/config"
|
|
)
|
|
|
|
type Params struct {
|
|
Name string
|
|
Action string
|
|
ID *int
|
|
}
|
|
|
|
func parse(cfg *config.Config, r *http.Request) Params {
|
|
name := strings.TrimPrefix(r.URL.Path, "/")
|
|
if name == "" {
|
|
name = cfg.Wiki.Front
|
|
}
|
|
|
|
action := r.URL.Query().Get("a")
|
|
|
|
idStr := r.URL.Query().Get("i")
|
|
id, err := strconv.Atoi(idStr)
|
|
var idRef = &id
|
|
if err != nil || id <= 0 {
|
|
idRef = nil
|
|
}
|
|
|
|
return Params{Name: name, Action: action, ID: idRef}
|
|
}
|
|
|
|
func handle(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
|
|
params := parse(cfg, r)
|
|
|
|
switch params.Action {
|
|
case "static":
|
|
Static(cfg, w, r, ¶ms)
|
|
case "", "view":
|
|
View(cfg, w, r, ¶ms)
|
|
case "edit":
|
|
Edit(cfg, w, r, ¶ms)
|
|
case "all":
|
|
All(cfg, w, r, ¶ms)
|
|
case "revs":
|
|
Revisions(cfg, w, r, ¶ms)
|
|
case "revert":
|
|
Revert(cfg, w, r, ¶ms)
|
|
case "rev":
|
|
ViewRevision(cfg, w, r, ¶ms)
|
|
case "search":
|
|
Search(cfg, w, r, ¶ms)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}
|
|
|
|
func Handler(cfg *config.Config) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
handle(cfg, w, r)
|
|
}
|
|
}
|