Files
WB-status-api-server/app.go
2025-08-07 10:59:46 +09:00

85 lines
1.7 KiB
Go

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)
}
initdb()
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サイトのみをチェックする
}
}