Skip to content
Permalink
Browse files

Refactoring the configuration file

  • Loading branch information...
Depado committed May 21, 2016
1 parent 56a17d4 commit d4ab14a2491f79b4cbad62cb41195936c2452ae7
Showing with 199 additions and 145 deletions.
  1. +0 −75 conf/conf.go
  2. +28 −18 main.go
  3. +157 −0 models/conf.go
  4. +0 −46 models/parser.go
  5. +14 −6 models/service.go

This file was deleted.

46 main.go
@@ -12,27 +12,17 @@ import (
"github.com/Depado/gomonit/views"
)

func main() {
var err error

// Configuration parsing and services initialization
if err = conf.Load("conf.yml"); err != nil {
log.Fatal(err)
}
if err = models.ParseConf(); err != nil {
log.Fatal(err)
}
// Starting monitoring of services
go models.All.Monitor()
// APIVersion is the current API version.
const APIVersion = "1"

// Gin initialization
if !conf.C.Debug {
gin.SetMode(gin.ReleaseMode)
}
// SetupRouter sets up the router and its routes as well as the templates and
// static files routes.
func SetupRouter() *gin.Engine {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/static", "./assets")

// Main view
r.GET("/", views.Index)

// API routes declaration
@@ -55,7 +45,27 @@ func main() {
ar.GET("/hosts/new", admin.NewHost)
ar.POST("/hosts/new", admin.PostNewHost)
}
return r
}

func main() {
var err error

// Configuration parsing and services initialization
if err = conf.Load("conf.yml"); err != nil {
log.Fatal(err)
}
if err = models.ParseConf(); err != nil {
log.Fatal(err)
}
// Starting monitoring of services
go models.All.Monitor()

// Gin initialization
if !conf.C.Debug {
gin.SetMode(gin.ReleaseMode)
}

// Running
r.Run(conf.C.Listen)
router := SetupRouter()
router.Run(conf.C.Listen)
}
@@ -0,0 +1,157 @@
package models

import (
"fmt"
"io/ioutil"
"strings"
"time"

"gopkg.in/yaml.v2"
)

// C is the main configuration that is exported
var C Configuration

// Configuration is the type representing the configuration of the service
type Configuration struct {
Host string
Port string
DefaultServiceInterval time.Duration
DefaultRepoInterval time.Duration
GithubOAuthToken string
Debug bool
Services Services
RawServices []rawService
}

type rawConfiguration struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
DefaultServiceInterval string `yaml:"default_service_interval"`
DefaultRepoInterval string `yaml:"default_repo_interval"`
GithubOAuthToken string `yaml:"github_oauth_token"`
Debug bool `yaml:"debug"`
Services []rawService `yaml:"services"`
}

func (r *rawConfiguration) ParseServices() (Services, error) {
var err error
services := make(Services, len(r.Services))
for i, s := range r.Services {
p, err := s.Parse()
if err != nil {
return services, err
}
services[i] = &p
}
return services, nil
}

type rawService struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Host string `yaml:"host"`
URL string `yaml:"url"`
Icon string `yaml:"icon"`
Own bool `yaml:"own"`

RepoType string `yaml:"repo_type"`
RepoHost string `yaml:"repo_url"`
RepoToken string `yaml:"repo_token"`
Repo string `yaml:"repo"`

CIType string `yaml:"ci_type"`
CIHost string `yaml:"ci_host"`

ServiceInterval string `yaml:"service_interval"`
RepoInterval string `yaml:"repo_interval"`
}

func (r *rawService) Parse() (Service, error) {
var err error
var s Service
var ri time.Duration
var si time.Duration

if r.ID == "" || r.Name == "" {
return s, fmt.Errorf("Each service needs a 'name' and an 'id' fields.")
}
s.Name = r.Name
s.ID = r.ID
if r.RepoType != "" && r.RepoType != "github" { // TODO Modify when other types of repo are added
return s, fmt.Errorf("Service %s : %s repo type isn't supported yet", r.Name, r.RepoType)
}
if r.Repo != "" {
s.Repo = r.Repo
s.RepoType = "github" // TODO Modify when other types of repo are added
r.RepoHost = "https://github.com"
s.RepoURL = fmt.Sprintf("%s/%s", strings.TrimSuffix(r.RepoHost, "/"), r.Repo)
if r.RepoInterval != "" {
if ri, err = time.ParseDuration(r.RepoInterval); err != nil {
return s, fmt.Errorf("Service %s : Can't parse 'repo_interval' : %s", r.Name, err)
}
s.RepoInterval = ri
}
if r.CIType != "" {
if r.CIType != "drone" {
return s, fmt.Errorf("Service %s : %s ci type isn't supported yet", r.Name, r.CIType)
}
if r.CIHost == "" {
return s, fmt.Errorf("Service %s : Missing 'ci_host' field", r.Name)
}
s.BuildAPI = fmt.Sprintf("%s/api/repos/%s/builds", strings.TrimSuffix(r.CIHost, "/"), r.Repo)
s.BuildURL = fmt.Sprintf("%s/%s", strings.TrimSuffix(r.CIHost, "/"), r.Repo)
}
}
if r.URL != "" {
s.URL = r.URL
short := strings.TrimPrefix(s.URL, "http://")
s.ShortURL = strings.TrimPrefix(short, "https://")
if r.ServiceInterval != "" {
if si, err = time.ParseDuration(r.ServiceInterval); err != nil {
return s, fmt.Errorf("Service %s : Can't parse 'service_interval' : %s", r.Name, err)
}
s.ServiceInterval = si
}
}
s.Icon = "/static/custom/" + r.Icon
s.Own = r.Own
s.Host = r.Host
return s, nil
}

// Load loads the given fp (file path) to the C global configuration variable.
func Load(fp string) error {
var err error
var raw rawConfiguration
var conf []byte
var sd time.Duration
var rd time.Duration

if conf, err = ioutil.ReadFile(fp); err != nil {
return err
}
if err = yaml.Unmarshal(conf, &raw); err != nil {
return err
}
if sd, err = time.ParseDuration(raw.DefaultServiceInterval); err != nil {
return err
}
if rd, err = time.ParseDuration(raw.DefaultRepoInterval); err != nil {
return err
}
ss, err := raw.ParseServices()
if err != nil {
return err
}
C = Configuration{
Host: raw.Host,
Port: raw.Port,
DefaultServiceInterval: sd,
DefaultRepoInterval: rd,
GithubOAuthToken: raw.GithubOAuthToken,
Debug: raw.Debug,
Services: ss,
}
return nil
}

This file was deleted.

@@ -10,23 +10,31 @@ import (
"sync"
"time"

"github.com/Depado/gomonit/conf"
"github.com/Depado/govue/conf"
)

// All represents all the services
var All Services

// Service is a single service
type Service struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
ShortURL string `json:"short_url"`
Description string `json:"description"`
RepoURL string `json:"repo_url"`
RepoStars int `json:"repo_stars"`
RepoForks int `json:"repo_forks"`
RepoWatchers int `json:"repo_watchers"`
Host string `json:"host"`
ServiceInterval time.Duration `json:"service_interval"`

Description string `json:"description"`
RepoURL string `json:"repo_url"`
RepoStars int `json:"repo_stars"`
RepoForks int `json:"repo_forks"`
RepoWatchers int `json:"repo_watchers"`
RepoType string `json:"repo_type"`
RepoHost string `json:"repo_host"`
Repo string `json:"repo"`
RepoInterval time.Duration `json:"repo_interval"`

BuildAPI string `json:"build_api"`
BuildURL string `json:"build_url"`
CurrentBuildURL string `json:"current_build_url"`

0 comments on commit d4ab14a

Please sign in to comment.
You can’t perform that action at this time.