feat: 读配置,起服务
This commit is contained in:
parent
70d3731302
commit
cd820c187a
1
go.mod
1
go.mod
|
|
@ -7,6 +7,7 @@ replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
|
||||||
require (
|
require (
|
||||||
github.com/apache/thrift v0.0.0-00010101000000-000000000000
|
github.com/apache/thrift v0.0.0-00010101000000-000000000000
|
||||||
github.com/cloudwego/hertz v0.10.3
|
github.com/cloudwego/hertz v0.10.3
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
|
||||||
3
go.sum
3
go.sum
|
|
@ -30,8 +30,10 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/nyaruka/phonenumbers v1.0.55 h1:bj0nTO88Y68KeUQ/n3Lo2KgK7lM1hF7L9NFuwcCl3yg=
|
github.com/nyaruka/phonenumbers v1.0.55 h1:bj0nTO88Y68KeUQ/n3Lo2KgK7lM1hF7L9NFuwcCl3yg=
|
||||||
github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
|
github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
|
||||||
|
|
@ -112,6 +114,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
|
||||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package biz
|
||||||
|
|
||||||
|
import "weather_and_earthquake/internal/config"
|
||||||
|
|
||||||
|
var AccuWeather *config.AccuWeather
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
import "weather_and_earthquake/internal/config"
|
||||||
|
|
||||||
|
var DataConf *config.Data
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import "github.com/cloudwego/hertz/pkg/app"
|
import (
|
||||||
|
"github.com/cloudwego/hertz/pkg/app"
|
||||||
|
"weather_and_earthquake/internal/config"
|
||||||
|
)
|
||||||
import "context"
|
import "context"
|
||||||
|
|
||||||
|
var Apps *config.Apps
|
||||||
|
|
||||||
// 跨域
|
// 跨域
|
||||||
func CorsMiddleware() app.HandlerFunc {
|
func CorsMiddleware() app.HandlerFunc {
|
||||||
return func(ctx context.Context, c *app.RequestContext) {
|
return func(ctx context.Context, c *app.RequestContext) {
|
||||||
|
|
|
||||||
66
main.go
66
main.go
|
|
@ -3,12 +3,70 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/cloudwego/hertz/pkg/app/server"
|
"github.com/cloudwego/hertz/pkg/app/server"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"weather_and_earthquake/internal/biz"
|
||||||
|
"weather_and_earthquake/internal/config"
|
||||||
|
"weather_and_earthquake/internal/data"
|
||||||
|
"weather_and_earthquake/internal/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
var (
|
||||||
h := server.Default()
|
_configPath string
|
||||||
|
_appsPath string
|
||||||
|
_server *server.Hertz
|
||||||
|
)
|
||||||
|
|
||||||
register(h)
|
func init() {
|
||||||
h.Spin()
|
flag.StringVar(&_configPath, "config", "configs/config.yaml", "config path, eg: -conf=config.yaml")
|
||||||
|
flag.StringVar(&_appsPath, "apps", "configs/apps.yaml", "apps path, eg: -apps=apps.yaml")
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialize() {
|
||||||
|
configFile, err := os.Open(_configPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err.Error())
|
||||||
|
}
|
||||||
|
defer configFile.Close()
|
||||||
|
|
||||||
|
conf := &config.Config{}
|
||||||
|
decoder := yaml.NewDecoder(configFile)
|
||||||
|
err = decoder.Decode(conf)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
appsFile, err := os.Open(_appsPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err.Error())
|
||||||
|
}
|
||||||
|
defer appsFile.Close()
|
||||||
|
|
||||||
|
apps := &config.Apps{}
|
||||||
|
decoder = yaml.NewDecoder(appsFile)
|
||||||
|
err = decoder.Decode(apps)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(apps.AppSecrets) == 0 {
|
||||||
|
log.Fatalln("no app secrets")
|
||||||
|
}
|
||||||
|
|
||||||
|
biz.AccuWeather = conf.AccuWeather
|
||||||
|
middleware.Apps = apps
|
||||||
|
data.DataConf = conf.Data
|
||||||
|
|
||||||
|
hostPort := fmt.Sprintf("%s:%d", conf.Server.Addr, conf.Server.Port)
|
||||||
|
_server = server.Default(server.WithHostPorts(hostPort))
|
||||||
|
register(_server)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
initialize()
|
||||||
|
_server.Spin()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue