overflow_server_app/main.go
2017-09-08 15:05:46 +09:00

91 lines
2.1 KiB
Go

package main
import (
"context"
"encoding/json"
"flag"
"log"
"go.uber.org/zap"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/cors_fasthttp"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_server_app/grpc"
"git.loafle.net/overflow/overflow_server_app/module/member"
"git.loafle.net/overflow/overflow_server_app/server"
"github.com/valyala/fasthttp"
)
var logger *zap.Logger
func main() {
configPath := flag.String("config", ".", "The path of config file")
flag.Parse()
loadConfig(*configPath)
ctx := newContext()
defer logger.Sync()
c := cors_fasthttp.AllowAll(ctx)
grpc.InitializePool(ctx)
s := server.New(ctx)
s.Route("POST", "/account/signin", member.SignIn)
s.Route("POST", "/account/signup", member.SignUp)
s.Route("POST", "/account/forgot_password", member.ForgotPassword)
s.Route("POST", "/account/reset_password", member.ResetPassword)
s.Route("GET", "/account/check_email", member.CheckEmail)
fasthttp.ListenAndServe(config.GetString("server.addr"), c.Handler(s.Handler))
}
func loadConfig(path string) {
config.SetConfigName("config")
config.AddConfigPath(path)
err := config.ReadInConfig()
if nil != err {
log.Fatalf("config error: %v", err)
}
}
func newContext() context.Context {
var err error
ctx := context.Background()
logConfig := config.Sub("logging")
buf, err := logConfig.Marshal("json")
if err != nil {
panic(err)
}
var cfg zap.Config
if err = json.Unmarshal(buf, &cfg); err != nil {
panic(err)
}
logger, err = cfg.Build()
if err != nil {
panic(err)
}
ctx = logging.NewContext(ctx, logger)
return ctx
}
//
//func CORS(next server.RequestHandler) server.RequestHandler {
// return server.RequestHandler(func(sctx *server.ServerContext, ctx * fasthttp.RequestCtx) {
//
// ctx.Response.Header.Set("Access-Control-Allow-Credentials", "true" )
// ctx.Response.Header.Set("Access-Control-Allow-Headers", "authorization")
// ctx.Response.Header.Set("Access-Control-Allow-Methods", "HEAD,GET,POST,PUT,DELETE,OPTIONS" )
// ctx.Response.Header.Set("Access-Control-Allow-Origin", "*")
//
// next(sctx, ctx)
// })
//}