Skip to content

Commit

Permalink
✨ feat: modified asterisk config #54 #9
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 14, 2024
1 parent 809a127 commit bc42839
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 29 deletions.
66 changes: 52 additions & 14 deletions asterisk/asterisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"time"

"github.com/sivaosorg/govm/timex"
"github.com/sivaosorg/govm/utils"
)

Expand Down Expand Up @@ -52,23 +53,43 @@ func (t *TelephonyConfig) SetRegion(value string) *TelephonyConfig {
return t
}

func (t *TelephonyConfig) SetPhonePrefix(values []string) *TelephonyConfig {
t.PhonePrefix = values
func (t *TelephonyConfig) SetPhonePrefixes(values []string) *TelephonyConfig {
t.PhonePrefixes = values
return t
}

func (t *TelephonyConfig) AppendPhonePrefix(values ...string) *TelephonyConfig {
t.PhonePrefix = append(t.PhonePrefix, values...)
func (t *TelephonyConfig) AppendPhonePrefixes(values ...string) *TelephonyConfig {
t.PhonePrefixes = append(t.PhonePrefixes, values...)
return t
}

func (t *TelephonyConfig) SetDigitExtensions(values []interface{}) *TelephonyConfig {
t.DigitExtensions = values
func (t *TelephonyConfig) SetApplyMaxExtension(values []interface{}) *TelephonyConfig {
t.ApplyMaxExtension = values
return t
}

func (t *TelephonyConfig) AppendDigitExtensions(values ...interface{}) *TelephonyConfig {
t.DigitExtensions = append(t.DigitExtensions, values...)
func (t *TelephonyConfig) AppendApplyMaxExtension(values ...interface{}) *TelephonyConfig {
t.ApplyMaxExtension = append(t.ApplyMaxExtension, values...)
return t
}

func (t *TelephonyConfig) SetExceptionalExtension(values []string) *TelephonyConfig {
t.ExceptionalExtension = values
return t
}

func (t *TelephonyConfig) AppendExceptionalExtension(values ...string) *TelephonyConfig {
t.ExceptionalExtension = append(t.ExceptionalExtension, values...)
return t
}

func (t *TelephonyConfig) SetTimezone(value string) *TelephonyConfig {
t.Timezone = value
return t
}

func (t *TelephonyConfig) SetTimeFormat(value string) *TelephonyConfig {
t.TimeFormat = value
return t
}

Expand Down Expand Up @@ -99,18 +120,25 @@ func AsteriskConfigValidator(a *AsteriskConfig) {
a.SetPort(a.Port)
}

func GetAsteriskConfigSample() *AsteriskConfig {
a := NewAsteriskConfig()
func GetTelephonyConfigSample() *TelephonyConfig {
t := NewTelephonyConfig()
t.SetRegion("VN")
t.AppendDigitExtensions(4, 5, 6)
t.AppendPhonePrefix("9", "6064")
t.AppendApplyMaxExtension(4, 5)
t.AppendPhonePrefixes("9")
t.SetTimezone(timex.DefaultTimezoneVietnam)
t.SetTimeFormat(timex.TimeFormat20060102150405)
t.AppendExceptionalExtension("022123456XX..XX")
return t
}

func GetAsteriskConfigSample() *AsteriskConfig {
a := NewAsteriskConfig()
a.SetEnabled(true)
a.SetHost("http://127.0.0.1")
a.SetHost("127.0.0.1")
a.SetPort(5038)
a.SetUsername("u@root")
a.SetPassword("pwd")
a.SetTelephony(*t)
a.SetTelephony(*GetTelephonyConfigSample())
return a
}

Expand Down Expand Up @@ -202,3 +230,13 @@ func (c *ClusterMultiTenantAsteriskConfig) FindClusterBy(key string) (MultiTenan
}
return *NewMultiTenantAsteriskConfig(), fmt.Errorf("The asterisk cluster not found")
}

func NewSettingConfig() *SettingConfig {
s := &SettingConfig{}
return s
}

func NewCacheConfig() *CacheConfig {
c := &CacheConfig{}
return c
}
17 changes: 14 additions & 3 deletions asterisk/asterisk_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ package asterisk
import "time"

type TelephonyConfig struct {
Region string `json:"region" yaml:"region"`
PhonePrefix []string `json:"phone_prefix" yaml:"phone_prefix"`
DigitExtensions []interface{} `json:"digit_extensions" yaml:"digit_extensions"`
Region string `json:"region" yaml:"region"`
Timezone string `json:"timezone" yaml:"timezone"`
TimeFormat string `json:"time_format" yaml:"time_format"`
PhonePrefixes []string `json:"phone_prefixes" yaml:"phone_prefixes"` // phone prefixes which removed from phone. number
ApplyMaxExtension []interface{} `json:"apply_max_extension" yaml:"apply_max_extension"`
ExceptionalExtension []string `json:"exceptional_extension" yaml:"exceptional_extension"`
}

type SettingConfig struct {
}

type CacheConfig struct {
}

type AsteriskConfig struct {
Expand All @@ -16,6 +25,8 @@ type AsteriskConfig struct {
Username string `json:"username" binding:"required" yaml:"username"`
Password string `json:"-" yaml:"password"`
Telephony TelephonyConfig `json:"telephony" yaml:"telephony"`
Setting SettingConfig `json:"setting" yaml:"setting"`
Caches CacheConfig `json:"cache" yaml:"cache"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
}

Expand Down
8 changes: 8 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"time"

"github.com/sivaosorg/govm/logger"
"github.com/sivaosorg/govm/timex"
"github.com/sivaosorg/govm/utils"
)

func NewServer() *Server {
s := &Server{}
s.SetTimezone(timex.DefaultTimezoneVietnam)
s.SetAttr(*NewAttribute())
s.SetTimeout(*NewTimeout().SetRead(15 * time.Second).SetWrite(15 * time.Second))
s.SetSSL(*GetSSLSample())
Expand All @@ -20,6 +22,11 @@ func NewServer() *Server {
return s
}

func (s *Server) SetTimezone(value string) *Server {
s.Timezone = value
return s
}

func (s *Server) SetHost(value string) *Server {
s.Host = value
return s
Expand Down Expand Up @@ -68,6 +75,7 @@ func ServerValidator(s *Server) {

func GetServerSample() *Server {
s := NewServer().
SetTimezone(timex.DefaultTimezoneVietnam).
SetHost("127.0.0.1").
SetPort(8083).
SetTimeout(*GetTimeoutSample())
Expand Down
15 changes: 8 additions & 7 deletions server/server_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package server
import "time"

type Server struct {
Host string `json:"host" yaml:"host"`
Port int `json:"port" binding:"required" yaml:"port"`
Mode string `json:"mode" yaml:"mode"`
Timeout Timeout `json:"timeout" yaml:"timeout"`
Attr Attr `json:"attr" yaml:"attr"`
SSL SSL `json:"ssl" yaml:"ssl"`
SP Pprof `json:"serve_proxy" yaml:"serve_proxy"`
Timezone string `json:"timezone" yaml:"timezone"`
Host string `json:"host" yaml:"host"`
Port int `json:"port" binding:"required" yaml:"port"`
Mode string `json:"mode" yaml:"mode"`
Timeout Timeout `json:"timeout" yaml:"timeout"`
Attr Attr `json:"attr" yaml:"attr"`
SSL SSL `json:"ssl" yaml:"ssl"`
SP Pprof `json:"serve_proxy" yaml:"serve_proxy"`
}

type Timeout struct {
Expand Down
2 changes: 1 addition & 1 deletion timex/timex.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func (now *Timex) Parse(s ...string) (t time.Time, err error) {
)

for _, str := range s {
hasTimeInStr := HasTimeRegexp.MatchString(str) // match 15:04:05, 15
hasTimeInStr := ApplyTimeRegexp.MatchString(str) // match 15:04:05, 15
onlyTimeInStr = hasTimeInStr && onlyTimeInStr && OnlyTimeRegexp.MatchString(str)
if t, err = now.parseWithFormat(str, currentLocation); err == nil {
location := t.Location()
Expand Down
162 changes: 158 additions & 4 deletions timex/timex_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package timex

import "regexp"

// Deprecated: unsupported
const (
DateTimeFormatYearMonthDTimezoneHourMinuteSecond = "2006-01-02T15:04:05"
DateTimeFormatYearMonthDay = "2006-01-02"
Expand All @@ -12,10 +13,163 @@ const (
DateTimeForm20060102150405999999 = "2006-01-02 15:04:05.999999"
)

// Recommended
const (
FormHourMinuteSecond = "15:04:05"
DotFormHourMinuteSecond = "15.04.05"
TimeRFC01T150405 = "15:04:05"
TimeRFC02D150405 = "15.04.05"
)

var HasTimeRegexp = regexp.MustCompile(`(\s+|^\s*|T)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))(\s*$|[Z+-])`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, 2021-07-20T00:59:10Z, 2021-07-20T00:59:10+08:00, 2021-07-20T00:00:10-07:00 etc
var OnlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc
// Recommended
const (
TimeFormat20060102T150405999999 = "2006-01-02T15:04:05.999999"
TimeFormat20060102T150405 = "2006-01-02T15:04:05"
TimeFormat20060102150405 = "2006-01-02 15:04:05"
TimeFormat02012006150405 = "02-01-2006 15:04:05"
TimeFormatRFC0102012006150405 = "02/01/2006 15:04:05"
TimeFormat20060102 = "2006-01-02"
TimeFormatRFC0102012006 = "02/01/2006"
TimeFormat20060102150405999999 = "2006-01-02 15:04:05.999999"
TimeFormat20060102150405999999RFC3339 = "2006-01-02 15:04:05.999999999 -07:00"
)

var ApplyTimeRegexp = regexp.MustCompile(`(\s+|^\s*|T)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))(\s*$|[Z+-])`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, 2021-07-20T00:59:10Z, 2021-07-20T00:59:10+08:00, 2021-07-20T00:00:10-07:00 etc
var OnlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc

// Timezone constants representing default timezones for specific regions.
const (
// DefaultTimezoneVietnam is a constant that holds the IANA Time Zone identifier
// for the default timezone in Vietnam, which is "Asia/Ho_Chi_Minh".
DefaultTimezoneVietnam = "Asia/Ho_Chi_Minh"

// DefaultTimezoneNewYork is a constant that holds the IANA Time Zone identifier
// for the default timezone in New York, USA, which is "America/New_York".
DefaultTimezoneNewYork = "America/New_York"

// DefaultTimezoneLondon is a constant that holds the IANA Time Zone identifier
// for the default timezone in London, United Kingdom, which is "Europe/London".
DefaultTimezoneLondon = "Europe/London"

// DefaultTimezoneTokyo is a constant that holds the IANA Time Zone identifier
// for the default timezone in Tokyo, Japan, which is "Asia/Tokyo".
DefaultTimezoneTokyo = "Asia/Tokyo"

// DefaultTimezoneSydney is a constant that holds the IANA Time Zone identifier
// for the default timezone in Sydney, Australia, which is "Australia/Sydney".
DefaultTimezoneSydney = "Australia/Sydney"

// DefaultTimezoneParis is a constant that holds the IANA Time Zone identifier
// for the default timezone in Paris, France, which is "Europe/Paris".
DefaultTimezoneParis = "Europe/Paris"

// DefaultTimezoneMoscow is a constant that holds the IANA Time Zone identifier
// for the default timezone in Moscow, Russia, which is "Europe/Moscow".
DefaultTimezoneMoscow = "Europe/Moscow"

// DefaultTimezoneLosAngeles is a constant that holds the IANA Time Zone identifier
// for the default timezone in Los Angeles, USA, which is "America/Los_Angeles".
DefaultTimezoneLosAngeles = "America/Los_Angeles"

// DefaultTimezoneManila is a constant that holds the IANA Time Zone identifier
// for the default timezone in Manila, Philippines, which is "Asia/Manila".
DefaultTimezoneManila = "Asia/Manila"

// DefaultTimezoneKualaLumpur is a constant that holds the IANA Time Zone identifier
// for the default timezone in Kuala Lumpur, Malaysia, which is "Asia/Kuala_Lumpur".
DefaultTimezoneKualaLumpur = "Asia/Kuala_Lumpur"

// DefaultTimezoneJakarta is a constant that holds the IANA Time Zone identifier
// for the default timezone in Jakarta, Indonesia, which is "Asia/Jakarta".
DefaultTimezoneJakarta = "Asia/Jakarta"

// DefaultTimezoneYangon is a constant that holds the IANA Time Zone identifier
// for the default timezone in Yangon, Myanmar, which is "Asia/Yangon".
DefaultTimezoneYangon = "Asia/Yangon"

// DefaultTimezoneAuckland is a constant that holds the IANA Time Zone identifier
// for the default timezone in Auckland, New Zealand, which is "Pacific/Auckland".
DefaultTimezoneAuckland = "Pacific/Auckland"

// DefaultTimezoneBangkok is a constant that holds the IANA Time Zone identifier
// for the default timezone in Bangkok, Thailand, which is "Asia/Bangkok".
DefaultTimezoneBangkok = "Asia/Bangkok"

// DefaultTimezoneDelhi is a constant that holds the IANA Time Zone identifier
// for the default timezone in Delhi, India, which is "Asia/Kolkata".
DefaultTimezoneDelhi = "Asia/Kolkata"

// DefaultTimezoneDubai is a constant that holds the IANA Time Zone identifier
// for the default timezone in Dubai, United Arab Emirates, which is "Asia/Dubai".
DefaultTimezoneDubai = "Asia/Dubai"

// DefaultTimezoneCairo is a constant that holds the IANA Time Zone identifier
// for the default timezone in Cairo, Egypt, which is "Africa/Cairo".
DefaultTimezoneCairo = "Africa/Cairo"

// DefaultTimezoneAthens is a constant that holds the IANA Time Zone identifier
// for the default timezone in Athens, Greece, which is "Europe/Athens".
DefaultTimezoneAthens = "Europe/Athens"

// DefaultTimezoneRome is a constant that holds the IANA Time Zone identifier
// for the default timezone in Rome, Italy, which is "Europe/Rome".
DefaultTimezoneRome = "Europe/Rome"

// DefaultTimezoneJohannesburg is a constant that holds the IANA Time Zone identifier
// for the default timezone in Johannesburg, South Africa, which is "Africa/Johannesburg".
DefaultTimezoneJohannesburg = "Africa/Johannesburg"

// DefaultTimezoneStockholm is a constant that holds the IANA Time Zone identifier
// for the default timezone in Stockholm, Sweden, which is "Europe/Stockholm".
DefaultTimezoneStockholm = "Europe/Stockholm"

// DefaultTimezoneOslo is a constant that holds the IANA Time Zone identifier
// for the default timezone in Oslo, Norway, which is "Europe/Oslo".
DefaultTimezoneOslo = "Europe/Oslo"

// DefaultTimezoneHelsinki is a constant that holds the IANA Time Zone identifier
// for the default timezone in Helsinki, Finland, which is "Europe/Helsinki".
DefaultTimezoneHelsinki = "Europe/Helsinki"

// DefaultTimezoneKiev is a constant that holds the IANA Time Zone identifier
// for the default timezone in Kiev, Ukraine, which is "Europe/Kiev".
DefaultTimezoneKiev = "Europe/Kiev"

// DefaultTimezoneBeijing is a constant that holds the IANA Time Zone identifier
// for the default timezone in Beijing, China, which is "Asia/Shanghai".
DefaultTimezoneBeijing = "Asia/Shanghai"

// DefaultTimezoneSingapore is a constant that holds the IANA Time Zone identifier
// for the default timezone in Singapore, which is "Asia/Singapore".
DefaultTimezoneSingapore = "Asia/Singapore"

// DefaultTimezoneIslamabad is a constant that holds the IANA Time Zone identifier
// for the default timezone in Islamabad, Pakistan, which is "Asia/Karachi".
DefaultTimezoneIslamabad = "Asia/Karachi"

// DefaultTimezoneColombo is a constant that holds the IANA Time Zone identifier
// for the default timezone in Colombo, Sri Lanka, which is "Asia/Colombo".
DefaultTimezoneColombo = "Asia/Colombo"

// DefaultTimezoneDhaka is a constant that holds the IANA Time Zone identifier
// for the default timezone in Dhaka, Bangladesh, which is "Asia/Dhaka".
DefaultTimezoneDhaka = "Asia/Dhaka"

// DefaultTimezoneKathmandu is a constant that holds the IANA Time Zone identifier
// for the default timezone in Kathmandu, Nepal, which is "Asia/Kathmandu".
DefaultTimezoneKathmandu = "Asia/Kathmandu"

// DefaultTimezoneBrisbane is a constant that holds the IANA Time Zone identifier
// for the default timezone in Brisbane, Australia, which is "Australia/Brisbane".
DefaultTimezoneBrisbane = "Australia/Brisbane"

// DefaultTimezoneWellington is a constant that holds the IANA Time Zone identifier
// for the default timezone in Wellington, New Zealand, which is "Pacific/Auckland".
DefaultTimezoneWellington = "Pacific/Auckland"

// DefaultTimezonePortMoresby is a constant that holds the IANA Time Zone identifier
// for the default timezone in Port Moresby, Papua New Guinea, which is "Pacific/Port_Moresby".
DefaultTimezonePortMoresby = "Pacific/Port_Moresby"

// DefaultTimezoneSuva is a constant that holds the IANA Time Zone identifier
// for the default timezone in Suva, Fiji, which is "Pacific/Fiji".
DefaultTimezoneSuva = "Pacific/Fiji"
)

0 comments on commit bc42839

Please sign in to comment.