i2bill.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net/http"
  6. "git.i2edu.net/i2/i2-bill-api/internal/config"
  7. "git.i2edu.net/i2/i2-bill-api/internal/handler"
  8. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  9. "git.i2edu.net/i2/i2-bill-api/internal/utils"
  10. "git.i2edu.net/i2/go-zero/core/conf"
  11. "git.i2edu.net/i2/go-zero/rest"
  12. "git.i2edu.net/i2/go-zero/rest/httpx"
  13. )
  14. var configFile = flag.String("f", "etc/i2bill-api.yaml", "the config file")
  15. var withUnauthorizedCallback = rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) {
  16. httpx.OkJson(w, utils.ReturnHTTPUnauthorized(err))
  17. })
  18. func main() {
  19. flag.Parse()
  20. var c config.Config
  21. conf.MustLoad(*configFile, &c)
  22. ctx := svc.NewServiceContext(c)
  23. server := rest.MustNewServer(c.RestConf, withUnauthorizedCallback)
  24. defer server.Stop()
  25. handler.RegisterHandlers(server, ctx)
  26. server.Use(handler.Cors(ctx))
  27. server.AddRoutes(handler.CorsCheck())
  28. server.AddRoutes(handler.Static("/asserts/", handler.LocalFile("asserts", false)))
  29. fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
  30. server.Start()
  31. }