refactor: initial commit for major rewrite
This commit is contained in:
parent
3253cb8bbb
commit
bfd83f35a3
45 changed files with 439 additions and 1733 deletions
122
pkg/service/service.go
Normal file
122
pkg/service/service.go
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
configurator "git.towk2.me/towk/configurator/pkg"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
const (
|
||||
PLUGINS_RELPATH = "/plugins"
|
||||
TEMPLATES_RELPATH = "/templates"
|
||||
PROFILES_RELPATH = "/profiles"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
RootPath string `yaml:"root,omitempty"`
|
||||
Environment map[string]string
|
||||
|
||||
// max counts
|
||||
PluginsMaxCount int
|
||||
ProfilesMaxCount int
|
||||
}
|
||||
|
||||
// New creates the directories at specified path
|
||||
func New() *Service {
|
||||
return &Service{
|
||||
RootPath: ".",
|
||||
Environment: map[string]string{
|
||||
"CONFIGURATOR_HOST_URI": "",
|
||||
"ACCESS_TOKEN": "",
|
||||
},
|
||||
PluginsMaxCount: 64,
|
||||
ProfilesMaxCount: 256,
|
||||
}
|
||||
}
|
||||
|
||||
// Serve() starts the configurator service and waits for requests.
|
||||
func (s *Service) Serve() error {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.RequestID)
|
||||
router.Use(middleware.RealIP)
|
||||
router.Use(middleware.Logger)
|
||||
router.Use(middleware.Recoverer)
|
||||
router.Use(middleware.StripSlashes)
|
||||
router.Use(middleware.Timeout(60 * time.Second))
|
||||
|
||||
if s.requireAuth() {
|
||||
|
||||
} else {
|
||||
// general
|
||||
router.Get("/download", s.Download())
|
||||
router.Post("/upload", s.Upload())
|
||||
router.Get("/list", s.List())
|
||||
|
||||
// profiles
|
||||
router.Get("/profiles", s.GetProfiles())
|
||||
// router.Post("/profiles", s.CreateProfiles())
|
||||
router.Get("/profile/{id}", s.GetProfile())
|
||||
router.Post("/profile/{id}", s.CreateProfile())
|
||||
router.Post("/profile/{id}/data/{varname}", s.CreateProfileVar())
|
||||
router.Delete("/profile/{id}/data/{varname}", s.DeleteProfileVar())
|
||||
router.Get("/profile/{id}/data", s.GetProfileData())
|
||||
router.Get("/profile/{id}/data/{varname}", s.GetProfileVar())
|
||||
router.Post("/profile/{id}/paths/{path}", s.CreateProfilePath())
|
||||
router.Delete("/profile/{id}/paths/{path}", s.DeleteProfilePath())
|
||||
router.Get("/profile/{id}/paths/{path}", s.GetProfilePath())
|
||||
|
||||
// plugins
|
||||
router.Get("/plugins", s.GetPlugins())
|
||||
router.Post("/plugins", s.CreatePlugins())
|
||||
router.Delete("/plugins/{id}", s.DeletePlugins())
|
||||
}
|
||||
|
||||
// always available public routes go here
|
||||
router.HandleFunc("/status", s.GetStatus)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) requireAuth() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Service) FetchJwks(uri string) {
|
||||
|
||||
}
|
||||
|
||||
func LoadProfile(path string) (*Profile, error) {
|
||||
return LoadFromJSONFile[Profile](path)
|
||||
}
|
||||
|
||||
func LoadPlugin(path string) (*configurator.Plugin, error) {
|
||||
return LoadFromJSONFile[configurator.Plugin](path)
|
||||
}
|
||||
|
||||
func LoadFromJSONFile[T any](path string) (*T, error) {
|
||||
var (
|
||||
res *T
|
||||
contents []byte
|
||||
err error
|
||||
)
|
||||
|
||||
contents, err = os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read plugin file: %v", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(contents, &res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal plugin: %v", err)
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (s *Service) BuildProfilePath(id string) string {
|
||||
return s.RootPath + PLUGINS_RELPATH + "/" + id
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue