Files
gin-article/main.go
T
2023-03-09 17:32:56 +08:00

66 lines
941 B
Go

package main
import (
"embed"
"flag"
"gin-article/handler"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/joho/godotenv"
)
//go:embed statics/**/*
var staticFS embed.FS
//go:embed tmpl/*
var tplFS embed.FS
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
os.Exit(1)
}
r := Router()
// 静态文件也打包
tmpl, _ := template.ParseFS(tplFS, "tmpl/*")
r.SetHTMLTemplate(tmpl)
staticFS, _ := fs.Sub(staticFS, "static")
r.StaticFS("/static", http.FS(staticFS))
//r.LoadHTMLGlob("tmpl/*")
//r.Static("/statics", "./statics")
port := flag.Int("p", 88, "the port to listen on")
// 解析命令行参数
flag.Parse()
var wg sync.WaitGroup
wg.Add(1)
go func() {
r.Run(":" + strconv.Itoa(*port))
}()
wg.Add(1)
go func() {
for {
handler.MakeArticleOrder()
time.Sleep(10 * time.Second)
}
}()
wg.Wait()
}