74 行
1.6 KiB
Go
74 行
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"routersys.com/internal/site"
|
|
"routersys.com/web"
|
|
)
|
|
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
configPath := flag.String("config", "/etc/website/website.conf", "設定ファイル")
|
|
check := flag.Bool("check", false, "設定を読んで終わる")
|
|
showVersion := flag.Bool("version", false, "版を表示して終わる")
|
|
flag.Parse()
|
|
if *showVersion {
|
|
fmt.Println(version)
|
|
return
|
|
}
|
|
cfg, err := site.ParseConfig(*configPath)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(3)
|
|
}
|
|
if *check {
|
|
fmt.Println("設定は正しいです。")
|
|
return
|
|
}
|
|
static, err := fs.Sub(web.Static, "static")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
srv, err := site.New(cfg, version, static)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
cleanup := make(chan struct{})
|
|
go srv.RunCleanup(cleanup)
|
|
|
|
hs := &http.Server{
|
|
Addr: cfg.Listen,
|
|
Handler: srv.Handler(),
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 20 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
MaxHeaderBytes: 16 << 10,
|
|
}
|
|
go func() {
|
|
<-ctx.Done()
|
|
close(cleanup)
|
|
sctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
hs.Shutdown(sctx)
|
|
}()
|
|
log.Printf("待ち受け: %s(版 %s)", cfg.Listen, version)
|
|
if err := hs.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatal(err)
|
|
}
|
|
}
|