pull/429/head
Hunter Long 2020-02-29 22:31:55 -08:00
parent c6a965e140
commit 11d4d1852b
6 changed files with 125 additions and 31 deletions

View File

@ -55,6 +55,10 @@ docker-latest: docker-base
docker-vue:
docker build -t hunterlong/statping:vue --build-arg VERSION=${VERSION} .
push-vue: clean frontend-build compile docker-base docker-vue
docker push hunterlong/statping:base
docker push hunterlong/statping:vue
modd:
modd -f ./dev/modd.conf

View File

@ -150,7 +150,7 @@ func mainProcess() error {
return errors.Wrap(err, "database migration")
}
if err := core.CoreApp.CreateServicesFromEnvs(); err != nil {
if err := core.CoreApp.ServicesFromEnvFile(); err != nil {
errStr := "error 'SERVICE' environment variable"
log.Errorln(errStr)
return errors.Wrap(err, errStr)

View File

@ -16,6 +16,7 @@
package core
import (
"bufio"
"fmt"
"github.com/go-yaml/yaml"
"github.com/hunterlong/statping/core/notifier"
@ -352,25 +353,39 @@ func findServiceByHash(hash string) *Service {
return nil
}
func (c *Core) CreateServicesFromEnvs() error {
servicesEnv := utils.Getenv("SERVICES", []*types.Service{}).([]*types.Service)
func (c *Core) ServicesFromEnvFile() error {
servicesEnv := utils.Getenv("SERVICES_FILE", "").(string)
if servicesEnv == "" {
return nil
}
for k, service := range servicesEnv {
file, err := os.Open(servicesEnv)
if err != nil {
return errors.Wrapf(err, "error opening 'SERVICES_FILE' at: %s", servicesEnv)
}
defer file.Close()
if err := service.Valid(); err != nil {
return errors.Wrapf(err, "invalid service at index %d in SERVICES environment variable", k)
var serviceLines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
serviceLines = append(serviceLines, scanner.Text())
}
if len(serviceLines) == 0 {
return nil
}
for k, service := range serviceLines {
svr, err := utils.ValidateService(service)
if err != nil {
return errors.Wrapf(err, "invalid service at index %d in SERVICES_FILE environment variable", k)
}
if findServiceByHash(service.String()) == nil {
newService := &types.Service{
Name: service.Name,
Domain: service.Domain,
Method: service.Method,
Type: service.Type,
if findServiceByHash(svr.String()) == nil {
if _, err := database.Create(svr); err != nil {
return errors.Wrapf(err, "could not create service %s", svr.Name)
}
if _, err := database.Create(newService); err != nil {
return errors.Wrapf(err, "could not create service %s", newService.Name)
}
log.Infof("Created new service '%s'", newService.Name)
log.Infof("Created new service '%s'", svr.Name)
}
}

37
dev/services_in_text.txt vendored Normal file
View File

@ -0,0 +1,37 @@
https://demo.statping.com/
https://github.com/hunterlong/statping
https://statping.com
https://hub.docker.com/r/hunterlong/statping/
https://godoc.org/github.com/hunterlong/statping
https://hub.docker.com/r/hunterlong/statping/
https://goreportcard.com/report/github.com/hunterlong/statping
http://slack.statping.com
https://www.google.com/search?q=do+a+barrel+roll
https://travis-ci.com/hunterlong/statping
https://vue.statping.com
http://gorm.io
https://www.youtube.com/watch?v=mLOdar3jshI
https://www.youtube.com/watch?v=WwoGhpYdebQ&t=1m11s
https://www.youtube.com/watch?v=RPS-Cq4uMFs
https://www.youtube.com/watch?v=5Ao5mg11xIk&t=1m9s
https://www.youtube.com/watch?v=3tJ_SZDbPwQ
https://www.youtube.com/watch?v=6IgaDHRiujA
https://www.youtube.com/watch?v=x8eDyCRa0mY
https://www.youtube.com/watch?v=N3kkNfH4yco
tcp://8.8.8.8:53
tcp://8.8.4.4:53
https://mysql.statping.com
https://postgres.statping.com
tcp://hosted3.nfoservers.com:21
tcp://hosted3.nfoservers.com:22
tcp://hosted3.nfoservers.com:443
tcp://hosted3.nfoservers.com:3306
tcp://hosted15.nfoservers.com:3306
tcp://hosted15.nfoservers.com:21
tcp://hosted15.nfoservers.com:22
tcp://hosted15.nfoservers.com:80
tcp://hosted15.nfoservers.com:443
tcp://demo.statping.com:80
tcp://demo.statping.com:443
tcp://facebook.com:80
tcp://facebook.com:443

View File

@ -19,7 +19,6 @@ import (
"crypto/sha1"
"encoding/hex"
"fmt"
"github.com/pkg/errors"
"time"
)
@ -121,22 +120,9 @@ func (s *Service) IsRunning() bool {
}
}
func (s *Service) String() string {
func (s Service) String() string {
format := fmt.Sprintf("name:%sdomain:%sport:%dtype:%smethod:%s", s.Name, s.Domain, s.Port, s.Type, s.Method)
h := sha1.New()
h.Write([]byte(format))
return hex.EncodeToString(h.Sum(nil))
}
func (s *Service) Valid() error {
if s.Name == "" {
return errors.New("invalid - missing service name")
}
if s.Domain == "" {
return errors.New("invalid - missing service domain")
}
if s.Type == "" {
return errors.New("invalid - missing service type")
}
return nil
}

View File

@ -29,6 +29,7 @@ import (
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"reflect"
@ -441,6 +442,57 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
return contents, resp, err
}
func ValidateService(line string) (*types.Service, error) {
p, err := url.Parse(line)
if err != nil {
return nil, err
}
newService := new(types.Service)
domain := p.Host
newService.Name = niceDomainName(domain, p.Path)
if p.Port() != "" {
newService.Port = int(ToInt(p.Port()))
if p.Scheme != "http" && p.Scheme != "https" {
domain = strings.ReplaceAll(domain, ":"+p.Port(), "")
}
}
newService.Domain = domain
switch p.Scheme {
case "http", "https":
newService.Type = "http"
newService.Method = "get"
if p.Scheme == "https" {
newService.VerifySSL = types.NewNullBool(true)
}
default:
newService.Type = p.Scheme
}
return newService, nil
}
func niceDomainName(domain string, paths string) string {
domain = strings.ReplaceAll(domain, "www.", "")
splitPath := strings.Split(paths, "/")
if len(splitPath) == 1 {
return domain
}
var addedName []string
for k, p := range splitPath {
if k > 2 {
break
}
if len(p) > 16 {
addedName = append(addedName, p+"...")
break
} else {
addedName = append(addedName, p)
}
}
return domain + strings.Join(addedName, "/")
}
const (
B = 0x100
N = 0x1000