3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/irc/rest_api.go

154 lines
3.1 KiB
Go
Raw Normal View History

2016-11-06 02:05:29 +01:00
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
// viewing and modifying accounts, registered channels, dlines, rehashing, etc
package irc
import (
"encoding/json"
"net/http"
"time"
2016-11-06 02:05:29 +01:00
"fmt"
"github.com/gorilla/mux"
)
const restErr = "{\"error\":\"An unknown error occurred\"}"
// restAPIServer is used to keep a link to the current running server since this is the best
// way to do it, given how HTTP handlers dispatch and work.
var restAPIServer *Server
2016-11-06 02:59:10 +01:00
type restVersionResp struct {
Version string `json:"version"`
}
2016-11-06 02:05:29 +01:00
type restStatusResp struct {
Clients int `json:"clients"`
Opers int `json:"opers"`
Channels int `json:"channels"`
}
type restDLinesResp struct {
DLines map[string]IPBanInfo `json:"dlines"`
}
type restAcct struct {
Name string
RegisteredAt time.Time `json:"registered-at"`
Clients int
}
type restAccountsResp struct {
2016-11-06 02:59:10 +01:00
Accounts map[string]restAcct `json:"accounts"`
}
type restRehashResp struct {
Successful bool `json:"successful"`
Error string `json:"error"`
Time time.Time `json:"time"`
}
func restVersion(w http.ResponseWriter, r *http.Request) {
rs := restVersionResp{
Version: SemVer,
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
2016-11-06 02:05:29 +01:00
func restStatus(w http.ResponseWriter, r *http.Request) {
rs := restStatusResp{
Clients: restAPIServer.clients.Count(),
Opers: len(restAPIServer.operators),
Channels: len(restAPIServer.channels),
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
2016-11-06 02:59:10 +01:00
func restGetDLines(w http.ResponseWriter, r *http.Request) {
2016-11-06 02:05:29 +01:00
rs := restDLinesResp{
DLines: restAPIServer.dlines.AllBans(),
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
2016-11-06 02:59:10 +01:00
func restGetAccounts(w http.ResponseWriter, r *http.Request) {
rs := restAccountsResp{
Accounts: make(map[string]restAcct),
}
// get accts
for key, info := range restAPIServer.accounts {
rs.Accounts[key] = restAcct{
Name: info.Name,
RegisteredAt: info.RegisteredAt,
Clients: len(info.Clients),
}
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
2016-11-06 02:59:10 +01:00
func restRehash(w http.ResponseWriter, r *http.Request) {
err := restAPIServer.rehash()
rs := restRehashResp{
Successful: err == nil,
Time: time.Now(),
}
if err != nil {
rs.Error = err.Error()
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
2016-11-06 02:05:29 +01:00
func (s *Server) startRestAPI() {
// so handlers can ref it later
restAPIServer = s
// start router
r := mux.NewRouter()
2016-11-06 02:59:10 +01:00
// GET methods
rg := r.Methods("GET").Subrouter()
rg.HandleFunc("/version", restVersion)
rg.HandleFunc("/status", restStatus)
rg.HandleFunc("/dlines", restGetDLines)
rg.HandleFunc("/accounts", restGetAccounts)
// PUT methods
rp := r.Methods("POST").Subrouter()
rp.HandleFunc("/rehash", restRehash)
2016-11-06 02:05:29 +01:00
// start api
go http.ListenAndServe(s.restAPI.Listen, r)
}