mirror of
https://github.com/ChuckNorrison/LightningTipBot.git
synced 2026-08-13 12:33:14 +02:00
* add admin server + service * string fixes * ban and unban user * add return * ban using admin key * ban reason * remove redundant func call * add http status * add log * anon_id_sha256 (#262) * anon_id_sha256 * refactor sha256 hash to strings package * oops Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> * add admin server + service * ban and unban user * string fixes * add return * ban using admin key * ban reason * remove redundant func call * add http status * add log Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> Co-authored-by: LightningTipBot <88730856+LightningTipBot@users.noreply.github.com>
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/LightningTipBot/LightningTipBot/internal"
|
|
"github.com/gorilla/mux"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Server struct {
|
|
httpServer *http.Server
|
|
router *mux.Router
|
|
}
|
|
|
|
const (
|
|
StatusError = "ERROR"
|
|
StatusOk = "OK"
|
|
)
|
|
|
|
func NewServer(address string) *Server {
|
|
srv := &http.Server{
|
|
Addr: address,
|
|
// Good practice: enforce timeouts for servers you create!
|
|
WriteTimeout: 15 * time.Second,
|
|
ReadTimeout: 15 * time.Second,
|
|
}
|
|
apiServer := &Server{
|
|
httpServer: srv,
|
|
}
|
|
apiServer.router = mux.NewRouter()
|
|
apiServer.httpServer.Handler = apiServer.router
|
|
go apiServer.httpServer.ListenAndServe()
|
|
log.Infof("[LNURL] Server started at %s", internal.Configuration.Bot.LNURLServerUrl.Host)
|
|
return apiServer
|
|
}
|
|
|
|
func (w *Server) ListenAndServe() {
|
|
go w.httpServer.ListenAndServe()
|
|
}
|
|
func (w *Server) PathPrefix(path string, handler http.Handler) {
|
|
w.router.PathPrefix(path).Handler(handler)
|
|
}
|
|
func (w *Server) AppendRoute(path string, handler func(http.ResponseWriter, *http.Request), methods ...string) {
|
|
r := w.router.HandleFunc(path, LoggingMiddleware("API", handler))
|
|
if len(methods) > 0 {
|
|
r.Methods(methods...)
|
|
}
|
|
}
|
|
|
|
func NotFoundHandler(writer http.ResponseWriter, err error) {
|
|
log.Errorln(err)
|
|
// return 404 on any error
|
|
http.Error(writer, "404 page not found", http.StatusNotFound)
|
|
}
|
|
|
|
func WriteResponse(writer http.ResponseWriter, response interface{}) error {
|
|
jsonResponse, err := json.Marshal(response)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = writer.Write(jsonResponse)
|
|
return err
|
|
}
|