This commit is contained in:
C菌
2022-04-28 03:15:01 +08:00
parent c113e38d82
commit 94cd2ce218
28 changed files with 1059 additions and 14 deletions

35
main.go
View File

@@ -5,12 +5,15 @@ import (
"fmt"
"net/http"
"os"
"runtime"
"tinyurl/pkg/config"
"tinyurl/pkg/db"
"tinyurl/pkg/log"
"tinyurl/route"
"github.com/flamego/cors"
"github.com/flamego/flamego"
lru "github.com/hashicorp/golang-lru"
"github.com/ory/graceful"
// "github.com/justinas/alice"
)
@@ -20,18 +23,29 @@ var (
// Name is the name of the compiled software.
Name string
// Version is the version of the compiled software.
Version string
Version = "unknown"
GitCommitLog = "unknown"
GitStatus = "unknown"
BuildTime = "unknown"
BuildGoVersion = "unknown"
// flagconf is the config flag.
flagconf string
id, _ = os.Hostname()
)
func StringifyMultiLine() string {
return fmt.Sprintf("Version=%s\nGitCommitLog=%s\nBuildTime=%s\nGoVersion=%s\nruntime=%s/%s\n",
Version, GitCommitLog, BuildTime, BuildGoVersion, runtime.GOOS, runtime.GOARCH)
}
func init() {
flag.StringVar(&flagconf, "conf", "./configs/config.yml", "config path, eg: -conf config.yaml")
db.InitDb()
}
func main() {
fmt.Println(StringifyMultiLine())
cfg, err := config.Parse(flagconf)
if err != nil {
panic(err)
@@ -40,8 +54,25 @@ func main() {
if err != nil {
panic(err)
}
l, err := lru.New(10240)
if err != nil {
panic(err)
}
f := flamego.Classic()
f.Map(cfg, logger, db.DB)
f.Use(cors.CORS(
cors.Options{
Methods: []string{"POST", "GET", "OPTIONS", "PUT", "DELETE", "UPDATE"},
AllowDomain: []string{"*"},
AllowCredentials: true,
},
))
f.Use(flamego.Static(flamego.StaticOptions{
Directory: "./dist/",
}))
// f.Use(flamego.Static(flamego.StaticOptions{
// Directory: "./dist/assets/",
// }))
f.Map(cfg, logger, db.DB, l)
// f.Use(auth.Basic("admin", "1111"))
route.Route(f)
server := graceful.WithDefaults(