initial commit
This commit is contained in:
10
README.md
10
README.md
@@ -1,3 +1,11 @@
|
||||
# WB-status-api-server
|
||||
|
||||
a lightweight web api for Watanabebashi's statuspage
|
||||
WB-status-api-server は軽量なステータスページ用APIサーバです。
|
||||
|
||||
機能はシンプルでJSON形式で指定したURL、もしくは設定したURL全ての死活状況・HTTPステータスコードをJSON形式で返します。
|
||||
|
||||
デフォルトで5分間死活監視結果をキャッシュするため、ステータスページにアクセスが集中しても監視先に負荷をかける心配がありません。
|
||||
|
||||
また、大きなメリットとして本サーバは少し改良するだけでサーバーレスで運用することができます。フロントエンドのステータスページを静的サイトにし、JSで本サーバのAPIを叩かせることで運用コストを下げることができますし、ステータスページ用にサーバを用意する必要がなくなります。
|
||||
|
||||
本サーバは現在開発中のため動作しません。
|
84
app.go
Normal file
84
app.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-yaml/yaml"
|
||||
"os"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
General GeneralConfig `yaml:"general"`
|
||||
Sites []SiteConfig `yaml:"sites"`
|
||||
}
|
||||
|
||||
type GeneralConfig struct {
|
||||
CacheTime int `yaml:"cachetime"`
|
||||
DBType string `yaml:"dbtype"`
|
||||
SQLAddr string `yaml:"sqladdr"`
|
||||
}
|
||||
|
||||
type SiteConfig struct {
|
||||
Name string `yaml:"name"`
|
||||
URL string `yaml:"url"`
|
||||
Type string `yaml:"type"`
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func main() {
|
||||
// YAMLファイルを読み込む
|
||||
data, err := os.Open("config.yaml")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer data.Close()
|
||||
err = yaml.Unmarshal(data, &config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.GET("/api/getStatus", func(c *gin.Context) {
|
||||
// 全てのサイトのステータス情報をJSON形式で返す
|
||||
|
||||
})
|
||||
r.GET("/api/getStatus/:URL", func(c *gin.Context) {
|
||||
// 指定したサイトのステータス情報をJSON形式で返す
|
||||
url := c.Param("URL")
|
||||
|
||||
})
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
func initdb(){
|
||||
if config.General.DBType == "sqlite"{
|
||||
db, err := sql.Open("sqlite3", config.General.SQLAddr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer db.Close()
|
||||
// テーブルの作成
|
||||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS sites (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
lastChecked DATETIME NOT NULL,
|
||||
lastDowned DATETIME NOT NULL,
|
||||
status TEXT NOT NULL
|
||||
)`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getstatus(url string) string {
|
||||
if url == "" {
|
||||
// 全てのWebサイトをチェックする
|
||||
} else{
|
||||
// 指定したWebサイトのみをチェックする
|
||||
}
|
||||
}
|
13
config.yaml.sample
Normal file
13
config.yaml.sample
Normal file
@@ -0,0 +1,13 @@
|
||||
general:
|
||||
cachetime: 300
|
||||
# cachetimeは秒単位
|
||||
dbtype: sqlite
|
||||
# 現時点ではsqlite以外非対応
|
||||
sqladdr: "./wb-status-api.db"
|
||||
|
||||
sites:
|
||||
example_local:
|
||||
displayname: "example site"
|
||||
url: "http://example.local"
|
||||
type: "general"
|
||||
# このように書く。キー(ここではexample_local)は自由に決めてよい
|
Reference in New Issue
Block a user