src

package
v0.0.0-...-15a2939 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2022 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const EARTHQUAKE_ACTIVITY_TEMPLATE_FILE = "earthquake_activity.gtpl"
View Source
const EARTHQUAKE_COMMENTS_TEMPLATE_FILE = "earthquake_comments.gtpl"
View Source
const EARTHQUAKE_COUNT_CANCEL_TEMPLATE_FILE = "earthquake_count.gtpl"
View Source
const EARTHQUAKE_COUNT_TEMPLATE_FILE = "earthquake_count.gtpl"
View Source
const EARTHQUAKE_EPICENTER_TEMPLATE_FILE = "earthquake_epicenter.gtpl"
View Source
const EARTHQUAKE_INFO_TEMPLATE_FILE = "earthquake_info.gtpl"
View Source
const EARTHQUAKE_REPORT_TEMPLATE_FILE = "earthquake_report.gtpl"
View Source
const EARTHQUAKE_UPDATE_TEMPLATE_FILE = "earthquake_update.gtpl"
View Source
const FILE_PATH = "../templates"

Variables

View Source
var COORDINATE_REGEXP = regexp.MustCompile(`([+-][0-9.]+)([+-][0-9.]+)([+-][0-9]+)?/`)
View Source
var LocalConfig = &Config{
	Mode: "local",

	DatabaseConfig: &mysql.Config{
		DBName:               "earthquake-alert",
		User:                 "docker",
		Passwd:               "docker",
		Addr:                 "localhost:3306",
		Net:                  "tcp",
		ParseTime:            true,
		AllowNativePasswords: true,
	},
	PublishConfigName: "publish_config.yaml",
	PublishConfigPath: ".",
	PublishConfig:     nil,

	JmaXmlUrl: "",

	AuthenticationUser: "user",
	AuthenticationPw:   "password",
}

ローカルの設定 `go run .`をしたときはこの設定が使用されます。

View Source
var ProdConfig = &Config{
	Mode: "prod",

	DatabaseConfig: &mysql.Config{
		DBName:               "earthquake-alert",
		User:                 "docker",
		Passwd:               "docker",
		Addr:                 "localhost:3306",
		Net:                  "tcp",
		ParseTime:            true,
		AllowNativePasswords: true,
	},
	PublishConfigName: "publish_config.yaml",
	PublishConfigPath: ".",
	PublishConfig:     nil,

	JmaXmlUrl: "",

	AuthenticationUser: "",
	AuthenticationPw:   "",
}

本番環境の設定 Docker内部で実行するときなどはこの設定が使用されます。

View Source
var TestConfig = &Config{
	Mode: "test",

	DatabaseConfig: &mysql.Config{
		DBName:               "earthquake-alert-test",
		User:                 "docker",
		Passwd:               "docker",
		Addr:                 "localhost:3306",
		Net:                  "tcp",
		ParseTime:            true,
		AllowNativePasswords: true,
	},
	PublishConfigName: "publish_config_test.yaml",
	PublishConfigPath: ".",
	PublishConfig:     nil,

	JmaXmlUrl: "",

	AuthenticationUser: "",
	AuthenticationPw:   "",
}

テストの設定 `./scripts/test.sh`を使用してGoのテストを走らせたときにこの設定が使用されます。

Functions

func BasicAuth

func BasicAuth(r *gin.Engine) *gin.RouterGroup

func Convert

func Convert(text string, isDeleteReturns bool) string

文字列をイイカンジに整形する

- 全ての改行を消す (isDeleteReturnsがtrueの場合) - 全角英数字を半角英数字に変換する - 全角スペースを半角スペースに変換する

func EarthquakeHandler

func EarthquakeHandler(c gin.Context) error

func FormatDepth

func FormatDepth(d *int) string

震源の深さをいい感じにする

func FormatLatLonDepth

func FormatLatLonDepth(lat float64, lon float64, depth *int) string

日本測地系の緯度経度をいい感じにする lat, lonは度分秒で表す

func FormatMagnitude

func FormatMagnitude(m *jma.Magnitude) string

マグニチュード表記をいい感じにする

func GetIfModifiedSince

func GetIfModifiedSince(url url.URL, d time.Time) (*http.Response, error)

[If-Modified-Since](https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/If-Modified-Since)リクエストヘッダを付与した条件付きGETリクエストをします。 リソースが変更されていない場合、ステータスは304で返されます。

func Init

func Init(mode string)

func InitConfig

func InitConfig(mode string) error

設定初期化

func InitPublishConfig

func InitPublishConfig() error

公開先設定をyamlファイルから読み取りパースする

func NewConnectMySQL

func NewConnectMySQL(ctx context.Context) (*sql.DB, error)

MySQLに接続する

func ParseCoordinate

func ParseCoordinate(v string) (float64, float64, int, error)

震源要素を解析する。 例: "-17.2+178.6-570000/" --> (-17.2, 178.6, -570000, nil)

returns: (lat, lon, depth, error) depthは不明の場合は1、ごく浅い場合は0

func ParseDate

func ParseDate(d string) (time.Time, error)

ISO8601拡張形式をパースする

func ParseEventID

func ParseEventID(eventId string) ([]uint64, error)

EventIDをパースする

津波関連の電文では、半角スペースで区切られた複数のEventIDが存在することがある

func ParsePosition

func ParsePosition(position float64) float64

度分秒の緯度経度十進度に変換する = 度 + (分 / 60) + (秒 / 60) 引数は、00.00の形式で、[度].[分]となる。

func Routes

func Routes(r *gin.RouterGroup, h *Handler)

func Server

func Server()

func ServerMiddleWare

func ServerMiddleWare(r *gin.Engine)

Ginのミドルウェア

func Template

func Template(fileName string, obj any) (string, error)

func ValidModified

func ValidModified(url url.URL, m string) (bool, error)

HEADリクエストを送り、[Last-Modified](https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Last-Modified)を引数と比較する

Types

type Config

type Config struct {
	Mode string

	// MySQLの設定
	DatabaseConfig *mysql.Config

	// 公開先の設定ファイル名
	PublishConfigName string

	// 公開先のファイルパス
	PublishConfigPath string

	PublishConfig *PublishConfig

	// タイムゾーン
	TimeLocation *time.Location

	// 気象庁XML電文のURL
	JmaXmlUrl string

	// これを設定するとWebサーバーはBasic認証をします
	// Webサーバーを外部に公開する際には必ず設定してください
	AuthenticationUser string
	AuthenticationPw   string
}
var C *Config

設定項目です この変数はデフォルトでnilですが、init()で上書きしています。

type Content

type Content interface {
	// DBに格納したり、DBから関連の地震・津波情報を取得してくるメソッド
	Assembly(ctx context.Context, db *sql.DB) error

	// 震度分布図など様々な画像を生成するためのメソッド
	//
	// 並列で生成したいので`Assembly`と分けている
	SetImages() error

	// タイトルを返す
	GetTitle() string

	// 対象の基点時刻を返す
	GetTargetDate() (time.Time, error)

	// 運用種別を返す
	GetInfoType() jma.InfoType

	// 本文を返す
	GetText() (string, error)

	// 画像を返す
	GetImages() []string

	// EventIDを返す
	GetEventId() ([]uint64, error)
}

type Earthquake

type Earthquake struct {
	Row    string
	Parsed *jma.EarthquakeJmaXml

	Images []string
}

震度速報、震源に関する情報、震源・震度に関する情報

func ParseEarthquake

func ParseEarthquake(row []byte) (*Earthquake, error)

func (*Earthquake) Assembly

func (c *Earthquake) Assembly(ctx context.Context, db *sql.DB) error

func (*Earthquake) GetEventId

func (c *Earthquake) GetEventId() ([]uint64, error)

func (*Earthquake) GetImages

func (c *Earthquake) GetImages() []string

func (*Earthquake) GetInfoType

func (c *Earthquake) GetInfoType() jma.InfoType

func (*Earthquake) GetTargetDate

func (c *Earthquake) GetTargetDate() (time.Time, error)

func (*Earthquake) GetText

func (c *Earthquake) GetText() (string, error)

func (*Earthquake) GetTitle

func (c *Earthquake) GetTitle() string

func (*Earthquake) GetType

func (c *Earthquake) GetType() EarthquakeType

func (*Earthquake) SetImages

func (c *Earthquake) SetImages() error

type EarthquakeActivity

type EarthquakeActivity struct {
	Row    string
	Parsed *jma.EarthquakeActivityJmaXml
}

地震の活動状況等に関する情報

func ParseEarthquakeActivity

func ParseEarthquakeActivity(row []byte) (*EarthquakeActivity, error)

func (*EarthquakeActivity) Assembly

func (e *EarthquakeActivity) Assembly(ctx context.Context, db *sql.DB) error

func (*EarthquakeActivity) GetEventId

func (e *EarthquakeActivity) GetEventId() ([]uint64, error)

func (*EarthquakeActivity) GetImages

func (e *EarthquakeActivity) GetImages() []string

画像は生成しないので空

func (*EarthquakeActivity) GetInfoType

func (e *EarthquakeActivity) GetInfoType() jma.InfoType

func (*EarthquakeActivity) GetTargetDate

func (e *EarthquakeActivity) GetTargetDate() (time.Time, error)

func (*EarthquakeActivity) GetText

func (e *EarthquakeActivity) GetText() (string, error)

func (*EarthquakeActivity) GetTitle

func (e *EarthquakeActivity) GetTitle() string

タイトル 取り消し報の場合`【取消】`を付与する

func (*EarthquakeActivity) SetImages

func (e *EarthquakeActivity) SetImages() error

画像は生成しないのでなにもしない

type EarthquakeCount

type EarthquakeCount struct {
	Row    string
	Parsed *jma.EarthquakeCountInfoJmaXml
}

地震回数に関する情報

func ParseEarthquakeCount

func ParseEarthquakeCount(row []byte) (*EarthquakeCount, error)

func (*EarthquakeCount) Assembly

func (e *EarthquakeCount) Assembly(ctx context.Context, db *sql.DB) error

func (*EarthquakeCount) GetEventId

func (e *EarthquakeCount) GetEventId() ([]uint64, error)

func (*EarthquakeCount) GetImages

func (e *EarthquakeCount) GetImages() []string

画像は生成しない

func (*EarthquakeCount) GetInfoType

func (e *EarthquakeCount) GetInfoType() jma.InfoType

func (*EarthquakeCount) GetTargetDate

func (e *EarthquakeCount) GetTargetDate() (time.Time, error)

func (*EarthquakeCount) GetText

func (e *EarthquakeCount) GetText() (string, error)

func (*EarthquakeCount) GetTitle

func (e *EarthquakeCount) GetTitle() string

func (*EarthquakeCount) SetImages

func (e *EarthquakeCount) SetImages() error

画像は生成しない

func (*EarthquakeCount) TempCountText

func (e *EarthquakeCount) TempCountText() *EarthquakeCountDetail

テンプレートで使うやつ

func (*EarthquakeCount) TempFreeFormComment

func (e *EarthquakeCount) TempFreeFormComment() string

type EarthquakeCountDetail

type EarthquakeCountDetail struct {
	FormattedStartTime string
	FormattedEndTime   string
	Number             int
	FeltNumber         int
}

type EarthquakeType

type EarthquakeType int
const (
	EarthquakeReport    EarthquakeType = 0
	EarthquakeEpicenter EarthquakeType = 1
	EarthquakeInfo      EarthquakeType = 2
	EarthquakeCancel    EarthquakeType = 3
)

type EarthquakeUpdate

type EarthquakeUpdate struct {
	Row    string
	Parsed *jma.EarthquakeUpdateInfoJmaXml

	Images []string

	// 通常のHypocenterの型と違うため独自で定義する
	// 取消報の場合は空になる
	NewName      string
	NewLat       *float64
	NewLon       *float64
	NewDepth     *int
	NewLatLonStr string

	NewMagnitude string

	// 更新前の地震情報
	LatestEarthquake *models.Earthquake
}

顕著な地震の震源要素更新のお知らせ

func ParseEarthquakeUpdate

func ParseEarthquakeUpdate(row []byte) (*EarthquakeUpdate, error)

func (*EarthquakeUpdate) Assembly

func (e *EarthquakeUpdate) Assembly(ctx context.Context, db *sql.DB) error

func (*EarthquakeUpdate) GetEventId

func (e *EarthquakeUpdate) GetEventId() ([]uint64, error)

func (*EarthquakeUpdate) GetImages

func (e *EarthquakeUpdate) GetImages() []string

func (*EarthquakeUpdate) GetInfoType

func (e *EarthquakeUpdate) GetInfoType() jma.InfoType

func (*EarthquakeUpdate) GetOldEarthquakes

func (e *EarthquakeUpdate) GetOldEarthquakes(ctx context.Context, db *sql.DB, eventId uint64) (*models.Earthquake, error)

更新前の地震情報を取得する

func (*EarthquakeUpdate) GetOldEpicenter

func (e *EarthquakeUpdate) GetOldEpicenter() string

前回の震源情報を返す

func (*EarthquakeUpdate) GetOldMagnitude

func (e *EarthquakeUpdate) GetOldMagnitude() string

前回のマグニチュードを返す DBに格納されているマグニチュード表記はフォーマットしたものであるため存在している場合はそのまま返す

func (*EarthquakeUpdate) GetTargetDate

func (e *EarthquakeUpdate) GetTargetDate() (time.Time, error)

func (*EarthquakeUpdate) GetText

func (e *EarthquakeUpdate) GetText() (string, error)

func (*EarthquakeUpdate) GetTitle

func (e *EarthquakeUpdate) GetTitle() string

func (*EarthquakeUpdate) InsertUpdateDB

func (e *EarthquakeUpdate) InsertUpdateDB(ctx context.Context, db *sql.DB, eventId uint64, targetDate time.Time) error

EarthquakeUpdates テーブルにinsertする

func (*EarthquakeUpdate) ParseEpicenter

func (e *EarthquakeUpdate) ParseEpicenter() error

Coordinateを取得してパースする

func (*EarthquakeUpdate) SetImages

func (e *EarthquakeUpdate) SetImages() error

type Epicenter

type Epicenter struct {
	// 緯度経度
	// 震源要素が不明な場合はnilになる
	Lat *float64
	Lon *float64
	// 日本測地系かどうか
	IsDatumJapan bool

	// 深さ
	// 震源要素が不明だったり、深さ不明だった場合はnilになる
	Depth *int

	Name string
	// 遠地地震の場合、ここに震源決定機関が入る
	Source string

	H *jma.Hypocenter
}

func ParseEpicenter

func ParseEpicenter(h *jma.Hypocenter) (*Epicenter, error)

func (*Epicenter) FormatDepth

func (e *Epicenter) FormatDepth() string

深さを返す

type ErrorResponse

type ErrorResponse struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
	File    string `json:"file"`
	Line    int    `json:"line"`
}

type Handler

type Handler struct {
	DB *sql.DB
}

func NewHandler

func NewHandler(db *sql.DB) *Handler

func (*Handler) HandleWrapper

func (h *Handler) HandleWrapper(hand func(ctx *gin.Context) error) func(ctx *gin.Context)

ginでエラー処理を"イイカンジ"にするやつ

func (*Handler) RootHandler

func (h *Handler) RootHandler(ctx *gin.Context) error

`/` のhandler とりあえず接続確認用

type PublishConfig

type PublishConfig struct {
	Twitter []struct {
		// TODO: Twitter API v2のトークンどうなんだろう…?
		Token string `yaml:"token"`

		PublishConfigClientCommon `yaml:",inline"`
	} `yaml:"twitter,omitempty"`

	Discord []struct {
		// DiscordWebhook URL
		WebhookURL string `yaml:"webhook_url"`

		PublishConfigClientCommon `yaml:",inline"`
	} `yaml:"discord,omitempty"`

	Slack []struct {
		// SlackWebhook URL
		WebhookURL string `yaml:"webhook_url"`
		// 送信するSlackチャンネル
		Channel string `yaml:"channel"`

		PublishConfigClientCommon `yaml:",inline"`
	} `yaml:"slack,omitempty"`

	LineNotify []struct {
		// LINE Notify トークン
		Token string `yaml:"token"`

		PublishConfigClientCommon `yaml:",inline"`
	} `yaml:"line_notify,omitempty"`
}

公開先の設定など

type PublishConfigClientCommon

type PublishConfigClientCommon struct {
	ClientName string `yaml:"name"`

	// 送信する最低震度
	// 指定しない場合、全ての地震を送信する
	MinInt jma.EarthquakeIntensity `yaml:"min_int,omitempty"`

	// 送信する対象都道府県
	// これを指定すると、この地域で震度1以上の地震を観測したときのみ送信します。
	// 指定しないと、全ての都道府県の情報を送信します。
	Areas []string `yaml:"areas,omitempty"`

	// 津波情報を送信するか
	// デフォルトfalse
	IsTsunami bool `yaml:"is_tsunami,omitempty"`
}

公開先の共通設定項目

type Tsunami

type Tsunami struct {
	Row    string
	Parsed *jma.TsunamiJmaXml
}

func ParseTsunami

func ParseTsunami(row []byte) (*Tsunami, error)

気象庁の津波XML電文をパースする

func (*Tsunami) GetInfoType

func (c *Tsunami) GetInfoType() jma.InfoType

情報携帯を返す

func (*Tsunami) IsCommon

func (c *Tsunami) IsCommon() bool

伝聞が"通常の運用で発表する情報"であるかどうかを比較する 訓練報やテスト配信などではfalse

func (*Tsunami) Status

func (c *Tsunami) Status() jma.Status

伝聞の形式を返す

Directories

Path Synopsis
気象庁電文のXML定義
気象庁電文のXML定義

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL