Skip to content
Permalink
Browse files

Improving architecture

  • Loading branch information...
Depado committed Apr 1, 2016
1 parent 676291e commit 54c104d4901bb8ce2607aba2bef4a35545fb5c3d
Showing with 82 additions and 51 deletions.
  1. +7 −7 configuration/configuration.go → conf/conf.go
  2. +22 −42 main.go
  3. +7 −2 models/service.go
  4. +31 −0 views/api.go
  5. +15 −0 views/views.go
@@ -1,4 +1,4 @@
package configuration
package conf

import (
"fmt"
@@ -68,15 +68,15 @@ func Load(fp string) error {
}

// Parse parses the configuration and returns the appropriate Services
func (c Configuration) Parse() (models.Services, error) {
services := make(models.Services, len(c.Services))
func (c Configuration) Parse() error {
models.All = make(models.Services, len(c.Services))
c.CIURL = strings.TrimSuffix(c.CIURL, "/")
for i, s := range c.Services {
if s.CIType != "" && s.CIType != "drone" {
return nil, fmt.Errorf("Unable to use %s as CI, currently only drone is supported.", s.CIType)
return fmt.Errorf("Unable to use %s as CI, currently only drone is supported.", s.CIType)
}
if s.RepoType != "" && s.RepoType != "github" {
return nil, fmt.Errorf("Unable to use %s as repository, currently only github is supported.", s.RepoType)
return fmt.Errorf("Unable to use %s as repository, currently only github is supported.", s.RepoType)
}
repoURL := ""
if s.Repo != "" {
@@ -90,7 +90,7 @@ func (c Configuration) Parse() (models.Services, error) {
}
short := strings.TrimPrefix(s.URL, "http://")
short = strings.TrimPrefix(short, "https://")
services[i] = &models.Service{
models.All[i] = &models.Service{
Name: s.Name,
URL: s.URL,
ShortURL: short,
@@ -102,5 +102,5 @@ func (c Configuration) Parse() (models.Services, error) {
Own: s.Own,
}
}
return services, nil
return nil
}
64 main.go
@@ -2,80 +2,60 @@ package main

import (
"log"
"net/http"

"github.com/gin-gonic/gin"

"github.com/Depado/gomonit/admin"
"github.com/Depado/gomonit/auth"
"github.com/Depado/gomonit/configuration"
"github.com/Depado/gomonit/conf"
"github.com/Depado/gomonit/models"
"github.com/Depado/gomonit/views"
)

var all models.Services

func index(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"all": all,
})
}

func status(c *gin.Context) {
resp := gin.H{}
for _, s := range all {
resp[s.Name] = s.Status
}
c.JSON(200, resp)
}

func dump(c *gin.Context) {
c.JSON(200, all)
}

func own(c *gin.Context) {
resp := models.Services{}
for _, s := range all {
if s.Own {
resp = append(resp, s)
}
}
c.JSON(http.StatusOK, resp)
}

func main() {
var err error
if err = configuration.Load("conf.yml"); err != nil {

// Configuration parsing and services initialization
if err = conf.Load("conf.yml"); err != nil {
log.Fatal(err)
}
cnf := configuration.C
if all, err = cnf.Parse(); err != nil {
if err = conf.C.Parse(); err != nil {
log.Fatal(err)
}
go all.Monitor(cnf.UpdateInterval)
if !cnf.Debug {
// Starting monitoring of services
go models.All.Monitor()

// Gin initialization
if !conf.C.Debug {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/static", "./assets")

r.GET("/", index)
r.GET("/", views.Index)

// API routes declaration
api := r.Group("/api")
{
api.GET("/status", status)
api.GET("/dump/all", dump)
api.GET("/dump/own", own)
api.GET("/status", views.Status)
api.GET("/dump/all", views.DumpAll)
api.GET("/dump/own", views.DumpOwn)
}

// Authentication routes declaration
r.GET("/login", auth.Login)
r.POST("/login", auth.PostLogin)

// Admin routes declaration
ar := r.Group("/admin")
{
ar.GET("/", admin.Root)
ar.GET("/hosts", admin.Hosts)
ar.GET("/hosts/new", admin.NewHost)
ar.POST("/hosts/new", admin.PostNewHost)
}
r.Run(cnf.Listen)

// Running
r.Run(conf.C.Listen)
}
@@ -8,8 +8,13 @@ import (
"net/http"
"strings"
"time"

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

// All represents all the services
var All Services

// Service is a single service
type Service struct {
Name string `json:"name"`
@@ -135,8 +140,8 @@ func (s *Service) Check() {
type Services []*Service

// Monitor allows to monitor Services every interval delay
func (ss Services) Monitor(interval time.Duration) {
tc := time.NewTicker(interval)
func (ss Services) Monitor() {
tc := time.NewTicker(conf.C.UpdateInterval)
for {
for _, s := range ss {
go s.Check()
@@ -0,0 +1,31 @@
package views

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/Depado/gomonit/models"
)

func Status(c *gin.Context) {
resp := gin.H{}
for _, s := range models.All {
resp[s.Name] = s.Status
}
c.JSON(200, resp)
}

func DumpAll(c *gin.Context) {
c.JSON(200, models.All)
}

func DumpOwn(c *gin.Context) {
resp := models.Services{}
for _, s := range models.All {
if s.Own {
resp = append(resp, s)
}
}
c.JSON(http.StatusOK, resp)
}
@@ -0,0 +1,15 @@
package views

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/Depado/gomonit/models"
)

func index(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"all": models.All,
})
}

0 comments on commit 54c104d

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