feat: updated server implementation
This commit is contained in:
parent
0d27f07a8b
commit
d56a9e452f
5 changed files with 348 additions and 75 deletions
|
|
@ -15,36 +15,69 @@ import (
|
|||
"git.towk2.me/towk/configurator/pkg/util"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
const (
|
||||
PLUGINS_RELPATH = "/plugins"
|
||||
TEMPLATES_RELPATH = "/templates"
|
||||
PROFILES_RELPATH = "/profiles"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Addr string
|
||||
RootPath string `yaml:"root,omitempty"`
|
||||
Environment map[string]string
|
||||
|
||||
// max counts
|
||||
PluginsMaxCount int
|
||||
ProfilesMaxCount int
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// New creates the directories at specified path
|
||||
// New creates a new Service instance with default values
|
||||
func New() *Service {
|
||||
return &Service{
|
||||
RootPath: ".",
|
||||
Addr: ":5050",
|
||||
RootPath: "./",
|
||||
Environment: map[string]string{
|
||||
"CONFIGURATOR_HOST_URI": "",
|
||||
"ACCESS_TOKEN": "",
|
||||
"CONFIGURATOR_HOST": "",
|
||||
"CONFIGURATOR_ROOT": "",
|
||||
"ACCESS_TOKEN": "",
|
||||
},
|
||||
PluginsMaxCount: 64,
|
||||
ProfilesMaxCount: 256,
|
||||
PluginsMaxCount: DEFAULT_PLUGINS_MAX_COUNT,
|
||||
ProfilesMaxCount: DEFAULT_PROFILES_MAX_COUNT,
|
||||
Timeout: DEFAULT_TIMEOUT_IN_SECS,
|
||||
}
|
||||
}
|
||||
|
||||
// Init() sets up the default files and directories for the service
|
||||
func (s *Service) Init() error {
|
||||
// create the default directories
|
||||
var err error
|
||||
err = os.MkdirAll(s.RootPath, 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service root path: %v", err)
|
||||
}
|
||||
err = os.MkdirAll(s.PathForPlugins(), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service plugin path: %v", err)
|
||||
}
|
||||
err = os.MkdirAll(s.PathForProfiles(), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service profile path: %v", err)
|
||||
}
|
||||
err = os.MkdirAll(s.PathForData(), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service profile path: %v", err)
|
||||
}
|
||||
|
||||
// create the default files
|
||||
err = os.WriteFile(s.PathForMetadata(), []byte(DEFAULT_METADATA), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service metadata file: %v", err)
|
||||
}
|
||||
err = os.WriteFile(s.PathForHome(), []byte(DEFAULT_HOME), 0o777)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make service metadata file: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Serve() starts the configurator service and waits for requests.
|
||||
func (s *Service) Serve() error {
|
||||
router := chi.NewRouter()
|
||||
|
|
@ -53,15 +86,16 @@ func (s *Service) Serve() error {
|
|||
router.Use(middleware.Logger)
|
||||
router.Use(middleware.Recoverer)
|
||||
router.Use(middleware.StripSlashes)
|
||||
router.Use(middleware.Timeout(60 * time.Second))
|
||||
router.Use(middleware.Timeout(s.Timeout * time.Second))
|
||||
|
||||
if s.requireAuth() {
|
||||
|
||||
} else {
|
||||
// general
|
||||
// router.Handle("/download/*", http.StripPrefix("/download/", http.FileServer(http.Dir(s.PathForData()))))
|
||||
router.Get("/download/*", s.Download())
|
||||
router.Post("/upload", s.Upload())
|
||||
router.Get("/list", s.List())
|
||||
router.Get("/list/*", s.List())
|
||||
|
||||
// profiles
|
||||
router.Get("/profiles", s.GetProfiles())
|
||||
|
|
@ -76,14 +110,14 @@ func (s *Service) Serve() error {
|
|||
// 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())
|
||||
router.Get("/plugins", s.ListPlugins())
|
||||
router.Post("/plugins", s.CreatePlugin())
|
||||
router.Delete("/plugins/{id}", s.DeletePlugin())
|
||||
}
|
||||
|
||||
// always available public routes go here
|
||||
router.HandleFunc("/status", s.GetStatus)
|
||||
return http.ListenAndServe(":8080", router)
|
||||
return http.ListenAndServe(s.Addr, router)
|
||||
}
|
||||
|
||||
func (s *Service) requireAuth() bool {
|
||||
|
|
@ -225,9 +259,34 @@ func saveToJSONFile[T any](path string, data T) error {
|
|||
}
|
||||
|
||||
func (s *Service) PathForProfileWithID(id string) string {
|
||||
return s.RootPath + PROFILES_RELPATH + "/" + id
|
||||
return s.RootPath + RELPATH_PROFILES + "/" + id
|
||||
}
|
||||
|
||||
func (s *Service) PathForPluginWithName(name string) string {
|
||||
return s.RootPath + RELPATH_PLUGINS + "/" + name
|
||||
}
|
||||
|
||||
func (s *Service) PathForProfiles() string {
|
||||
return s.RootPath + RELPATH_PROFILES + "/"
|
||||
}
|
||||
|
||||
func (s *Service) PathForPlugins() string {
|
||||
return s.RootPath + PLUGINS_RELPATH
|
||||
return s.RootPath + RELPATH_PLUGINS + "/"
|
||||
}
|
||||
|
||||
func (s *Service) PathForData() string {
|
||||
return s.RootPath + RELPATH_DATA
|
||||
}
|
||||
|
||||
func (s *Service) PathForMetadata() string {
|
||||
return s.RootPath + RELPATH_METADATA
|
||||
}
|
||||
|
||||
func (s *Service) PathForHome() string {
|
||||
return s.RootPath + RELPATH_HELP
|
||||
}
|
||||
|
||||
func (s *Service) writeErrorResponse(w http.ResponseWriter, message string, code int) {
|
||||
http.Error(w, message, code)
|
||||
log.Error().Msg(message)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue