mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat: add backend
This commit is contained in:
parent
bc418ff248
commit
a838d994a6
68 changed files with 9561 additions and 0 deletions
113
internal/http/action.go
Normal file
113
internal/http/action.go
Normal file
|
@ -0,0 +1,113 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
type actionService interface {
|
||||
Fetch() ([]domain.Action, error)
|
||||
Store(action domain.Action) (*domain.Action, error)
|
||||
Delete(actionID int) error
|
||||
ToggleEnabled(actionID int) error
|
||||
}
|
||||
|
||||
type actionHandler struct {
|
||||
encoder encoder
|
||||
actionService actionService
|
||||
}
|
||||
|
||||
func (h actionHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.getActions)
|
||||
r.Post("/", h.storeAction)
|
||||
r.Delete("/{actionID}", h.deleteAction)
|
||||
r.Put("/{actionID}", h.updateAction)
|
||||
r.Patch("/{actionID}/toggleEnabled", h.toggleActionEnabled)
|
||||
}
|
||||
|
||||
func (h actionHandler) getActions(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
actions, err := h.actionService.Fetch()
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, actions, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h actionHandler) storeAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Action
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
action, err := h.actionService.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, action, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h actionHandler) updateAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Action
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
action, err := h.actionService.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, action, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h actionHandler) deleteAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
actionID = chi.URLParam(r, "actionID")
|
||||
)
|
||||
|
||||
// if !actionID return error
|
||||
|
||||
id, _ := strconv.Atoi(actionID)
|
||||
|
||||
if err := h.actionService.Delete(id); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h actionHandler) toggleActionEnabled(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
actionID = chi.URLParam(r, "actionID")
|
||||
)
|
||||
|
||||
// if !actionID return error
|
||||
|
||||
id, _ := strconv.Atoi(actionID)
|
||||
|
||||
if err := h.actionService.ToggleEnabled(id); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
}
|
41
internal/http/config.go
Normal file
41
internal/http/config.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/config"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
type configJson struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
LogLevel string `json:"log_level"`
|
||||
LogPath string `json:"log_path"`
|
||||
BaseURL string `json:"base_url"`
|
||||
}
|
||||
|
||||
type configHandler struct {
|
||||
encoder encoder
|
||||
}
|
||||
|
||||
func (h configHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.getConfig)
|
||||
}
|
||||
|
||||
func (h configHandler) getConfig(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
c := config.Config
|
||||
|
||||
conf := configJson{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
LogLevel: c.LogLevel,
|
||||
LogPath: c.LogPath,
|
||||
BaseURL: c.BaseURL,
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, conf, http.StatusOK)
|
||||
}
|
119
internal/http/download_client.go
Normal file
119
internal/http/download_client.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
type downloadClientService interface {
|
||||
List() ([]domain.DownloadClient, error)
|
||||
Store(client domain.DownloadClient) (*domain.DownloadClient, error)
|
||||
Delete(clientID int) error
|
||||
Test(client domain.DownloadClient) error
|
||||
}
|
||||
|
||||
type downloadClientHandler struct {
|
||||
encoder encoder
|
||||
downloadClientService downloadClientService
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.listDownloadClients)
|
||||
r.Post("/", h.store)
|
||||
r.Put("/", h.update)
|
||||
r.Post("/test", h.test)
|
||||
r.Delete("/{clientID}", h.delete)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) listDownloadClients(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
clients, err := h.downloadClientService.List()
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, clients, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.DownloadClient
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
client, err := h.downloadClientService.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, client, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) test(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.DownloadClient
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.downloadClientService.Test(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.DownloadClient
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
client, err := h.downloadClientService.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, client, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h downloadClientHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
clientID = chi.URLParam(r, "clientID")
|
||||
)
|
||||
|
||||
// if !clientID return error
|
||||
|
||||
id, _ := strconv.Atoi(clientID)
|
||||
|
||||
if err := h.downloadClientService.Delete(id); err != nil {
|
||||
// encode error
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
26
internal/http/encoder.go
Normal file
26
internal/http/encoder.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type encoder struct {
|
||||
}
|
||||
|
||||
func (e encoder) StatusResponse(ctx context.Context, w http.ResponseWriter, response interface{}, status int) {
|
||||
if response != nil {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf=8")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
// log err
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(status)
|
||||
}
|
||||
}
|
||||
|
||||
func (e encoder) StatusNotFound(ctx context.Context, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
132
internal/http/filter.go
Normal file
132
internal/http/filter.go
Normal file
|
@ -0,0 +1,132 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
type filterService interface {
|
||||
ListFilters() ([]domain.Filter, error)
|
||||
FindByID(filterID int) (*domain.Filter, error)
|
||||
Store(filter domain.Filter) (*domain.Filter, error)
|
||||
Delete(filterID int) error
|
||||
Update(filter domain.Filter) (*domain.Filter, error)
|
||||
//StoreFilterAction(action domain.Action) error
|
||||
}
|
||||
|
||||
type filterHandler struct {
|
||||
encoder encoder
|
||||
filterService filterService
|
||||
}
|
||||
|
||||
func (h filterHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.getFilters)
|
||||
r.Get("/{filterID}", h.getByID)
|
||||
r.Post("/", h.store)
|
||||
r.Put("/{filterID}", h.update)
|
||||
r.Delete("/{filterID}", h.delete)
|
||||
}
|
||||
|
||||
func (h filterHandler) getFilters(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
trackers, err := h.filterService.ListFilters()
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, trackers, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h filterHandler) getByID(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
filterID = chi.URLParam(r, "filterID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(filterID)
|
||||
|
||||
filter, err := h.filterService.FindByID(id)
|
||||
if err != nil {
|
||||
h.encoder.StatusNotFound(ctx, w)
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, filter, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h filterHandler) storeFilterAction(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
filterID = chi.URLParam(r, "filterID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(filterID)
|
||||
|
||||
filter, err := h.filterService.FindByID(id)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, filter, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h filterHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Filter
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
filter, err := h.filterService.Store(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, filter, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h filterHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Filter
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
filter, err := h.filterService.Update(data)
|
||||
if err != nil {
|
||||
// encode error
|
||||
return
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, filter, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h filterHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
filterID = chi.URLParam(r, "filterID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(filterID)
|
||||
|
||||
if err := h.filterService.Delete(id); err != nil {
|
||||
// return err
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
118
internal/http/indexer.go
Normal file
118
internal/http/indexer.go
Normal file
|
@ -0,0 +1,118 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
type indexerService interface {
|
||||
Store(indexer domain.Indexer) (*domain.Indexer, error)
|
||||
Update(indexer domain.Indexer) (*domain.Indexer, error)
|
||||
List() ([]domain.Indexer, error)
|
||||
GetAll() ([]*domain.IndexerDefinition, error)
|
||||
GetTemplates() ([]domain.IndexerDefinition, error)
|
||||
Delete(id int) error
|
||||
}
|
||||
|
||||
type indexerHandler struct {
|
||||
encoder encoder
|
||||
indexerService indexerService
|
||||
}
|
||||
|
||||
func (h indexerHandler) Routes(r chi.Router) {
|
||||
r.Get("/schema", h.getSchema)
|
||||
r.Post("/", h.store)
|
||||
r.Put("/", h.update)
|
||||
r.Get("/", h.getAll)
|
||||
r.Get("/options", h.list)
|
||||
r.Delete("/{indexerID}", h.delete)
|
||||
}
|
||||
|
||||
func (h indexerHandler) getSchema(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
indexers, err := h.indexerService.GetTemplates()
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, indexers, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h indexerHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Indexer
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
indexer, err := h.indexerService.Store(data)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, indexer, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h indexerHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.Indexer
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
indexer, err := h.indexerService.Update(data)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, indexer, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h indexerHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
idParam = chi.URLParam(r, "indexerID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(idParam)
|
||||
|
||||
if err := h.indexerService.Delete(id); err != nil {
|
||||
// return err
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h indexerHandler) getAll(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
indexers, err := h.indexerService.GetAll()
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, indexers, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h indexerHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
indexers, err := h.indexerService.List()
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, indexers, http.StatusOK)
|
||||
}
|
132
internal/http/irc.go
Normal file
132
internal/http/irc.go
Normal file
|
@ -0,0 +1,132 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
type ircService interface {
|
||||
ListNetworks(ctx context.Context) ([]domain.IrcNetwork, error)
|
||||
DeleteNetwork(ctx context.Context, id int64) error
|
||||
GetNetworkByID(id int64) (*domain.IrcNetwork, error)
|
||||
StoreNetwork(network *domain.IrcNetwork) error
|
||||
StoreChannel(networkID int64, channel *domain.IrcChannel) error
|
||||
StopNetwork(name string) error
|
||||
}
|
||||
|
||||
type ircHandler struct {
|
||||
encoder encoder
|
||||
ircService ircService
|
||||
}
|
||||
|
||||
func (h ircHandler) Routes(r chi.Router) {
|
||||
r.Get("/", h.listNetworks)
|
||||
r.Post("/", h.storeNetwork)
|
||||
r.Put("/network/{networkID}", h.storeNetwork)
|
||||
r.Post("/network/{networkID}/channel", h.storeChannel)
|
||||
r.Get("/network/{networkID}/stop", h.stopNetwork)
|
||||
r.Get("/network/{networkID}", h.getNetworkByID)
|
||||
r.Delete("/network/{networkID}", h.deleteNetwork)
|
||||
}
|
||||
|
||||
func (h ircHandler) listNetworks(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
networks, err := h.ircService.ListNetworks(ctx)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, networks, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h ircHandler) getNetworkByID(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(networkID)
|
||||
|
||||
network, err := h.ircService.GetNetworkByID(int64(id))
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, network, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h ircHandler) storeNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.IrcNetwork
|
||||
)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err := h.ircService.StoreNetwork(&data)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h ircHandler) storeChannel(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.IrcChannel
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(networkID)
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err := h.ircService.StoreChannel(int64(id), &data)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h ircHandler) stopNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
err := h.ircService.StopNetwork(networkID)
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h ircHandler) deleteNetwork(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
ctx = r.Context()
|
||||
networkID = chi.URLParam(r, "networkID")
|
||||
)
|
||||
|
||||
id, _ := strconv.Atoi(networkID)
|
||||
|
||||
err := h.ircService.DeleteNetwork(ctx, int64(id))
|
||||
if err != nil {
|
||||
//
|
||||
}
|
||||
|
||||
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
|
||||
}
|
123
internal/http/service.go
Normal file
123
internal/http/service.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/config"
|
||||
"github.com/autobrr/autobrr/web"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
address string
|
||||
baseUrl string
|
||||
actionService actionService
|
||||
downloadClientService downloadClientService
|
||||
filterService filterService
|
||||
indexerService indexerService
|
||||
ircService ircService
|
||||
}
|
||||
|
||||
func NewServer(address string, baseUrl string, actionService actionService, downloadClientSvc downloadClientService, filterSvc filterService, indexerSvc indexerService, ircSvc ircService) Server {
|
||||
return Server{
|
||||
address: address,
|
||||
baseUrl: baseUrl,
|
||||
actionService: actionService,
|
||||
downloadClientService: downloadClientSvc,
|
||||
filterService: filterSvc,
|
||||
indexerService: indexerSvc,
|
||||
ircService: ircSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s Server) Open() error {
|
||||
listener, err := net.Listen("tcp", s.address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server := http.Server{
|
||||
Handler: s.Handler(),
|
||||
}
|
||||
|
||||
return server.Serve(listener)
|
||||
}
|
||||
|
||||
func (s Server) Handler() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
//r.Get("/", index)
|
||||
//r.Get("/dashboard", dashboard)
|
||||
|
||||
//handler := web.AssetHandler("/", "build")
|
||||
|
||||
encoder := encoder{}
|
||||
|
||||
assets, _ := fs.Sub(web.Assets, "build/static")
|
||||
r.HandleFunc("/static/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
fileSystem := http.StripPrefix("/static/", http.FileServer(http.FS(assets)))
|
||||
fileSystem.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
|
||||
actionHandler := actionHandler{
|
||||
encoder: encoder,
|
||||
actionService: s.actionService,
|
||||
}
|
||||
|
||||
r.Route("/api/actions", actionHandler.Routes)
|
||||
|
||||
downloadClientHandler := downloadClientHandler{
|
||||
encoder: encoder,
|
||||
downloadClientService: s.downloadClientService,
|
||||
}
|
||||
|
||||
r.Route("/api/download_clients", downloadClientHandler.Routes)
|
||||
|
||||
filterHandler := filterHandler{
|
||||
encoder: encoder,
|
||||
filterService: s.filterService,
|
||||
}
|
||||
|
||||
r.Route("/api/filters", filterHandler.Routes)
|
||||
|
||||
ircHandler := ircHandler{
|
||||
encoder: encoder,
|
||||
ircService: s.ircService,
|
||||
}
|
||||
|
||||
r.Route("/api/irc", ircHandler.Routes)
|
||||
|
||||
indexerHandler := indexerHandler{
|
||||
encoder: encoder,
|
||||
indexerService: s.indexerService,
|
||||
}
|
||||
|
||||
r.Route("/api/indexer", indexerHandler.Routes)
|
||||
|
||||
configHandler := configHandler{
|
||||
encoder: encoder,
|
||||
}
|
||||
|
||||
r.Route("/api/config", configHandler.Routes)
|
||||
})
|
||||
|
||||
//r.HandleFunc("/*", handler.ServeHTTP)
|
||||
r.Get("/", index)
|
||||
r.Get("/*", index)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
p := web.IndexParams{
|
||||
Title: "Dashboard",
|
||||
Version: "thisistheversion",
|
||||
BaseUrl: config.Config.BaseURL,
|
||||
}
|
||||
web.Index(w, p)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue