aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/service.go
blob: c09ad66ea2dc2476cc22ae9130e306b678126d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package admin

import (
	"log"

	casbin "github.com/casbin/casbin/v2"
)

// Servicer container for dependencies and functions
type Servicer struct {
	*casbin.SyncedEnforcer
	Conf *ServerRepos
}

// InitServer initialize a git server and configure
func (s *Servicer) InitServer() {
	policies := s.Conf.ServerPolicies()
	s.AddPolicies(policies)
	s.SavePolicy()
	s.LoadPolicy()
	s.Conf.ServerPolicies()
}

// NewService create a new admin service, load config, and generate policies
func NewService(modelPath, policyPath, serverConfigPath string) *Servicer {
	enf, err := casbin.NewSyncedEnforcer(modelPath, policyPath)
	if err != nil {
		log.Fatalf("Couldn't load the enforcer encountered the following error: %s", err)
	}
	conf := loadServerConfig(serverConfigPath)
	svc := &Servicer{
		enf,
		conf,
	}
	svc.InitServer()
	return svc
}