feat: updated server implementation
This commit is contained in:
parent
0d27f07a8b
commit
d56a9e452f
5 changed files with 348 additions and 75 deletions
120
pkg/service/plugins.go
Normal file
120
pkg/service/plugins.go
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
configurator "git.towk2.me/towk/configurator/pkg"
|
||||
)
|
||||
|
||||
func (s *Service) ListPlugins() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
plugins map[string]configurator.Plugin
|
||||
names []string
|
||||
body []byte
|
||||
err error
|
||||
)
|
||||
|
||||
plugins, err = LoadPluginsFromDir(s.PathForPlugins())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
for name := range plugins {
|
||||
names = append(names, name)
|
||||
}
|
||||
|
||||
body, err = json.Marshal(names)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(body)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) CreatePlugin() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
plugin configurator.Plugin
|
||||
path string
|
||||
err error
|
||||
)
|
||||
|
||||
plugin, err = getPluginFromRequestBody(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// helper to check for valid plugin name
|
||||
var hasValidName = func(name string) bool {
|
||||
return name != "" && len(name) < 64
|
||||
}
|
||||
|
||||
// check for a valid plugin name
|
||||
if !hasValidName(plugin.Name()) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// save plugin at path using it's name
|
||||
path = s.PathForPluginWithName(plugin.Name())
|
||||
err = SavePluginToFile(path, &plugin)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) DeletePlugin() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
path string
|
||||
plugin configurator.Plugin
|
||||
err error
|
||||
)
|
||||
|
||||
plugin, err = getPluginFromRequestBody(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
path = s.PathForPluginWithName(plugin.Name())
|
||||
err = os.Remove(path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func getPluginFromRequestBody(r *http.Request) (configurator.Plugin, error) {
|
||||
var (
|
||||
plugin configurator.Plugin
|
||||
body []byte
|
||||
err error
|
||||
)
|
||||
body, err = io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &plugin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plugin, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue