Skip to content

Commit 6f84891

Browse files
committed
feat: perform orm and cors function
1 parent 47aa612 commit 6f84891

File tree

16 files changed

+340
-86
lines changed

16 files changed

+340
-86
lines changed

api/api_impl/server.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package api_impl
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/getkin/kin-openapi/openapi3filter"
7+
"github.com/go-chi/chi/v5"
8+
"github.com/go-chi/cors"
9+
logging "github.com/ipfs/go-log/v2"
10+
"github.com/jiaozifs/jiaozifs/api"
11+
"github.com/jiaozifs/jiaozifs/config"
12+
middleware "github.com/oapi-codegen/nethttp-middleware"
13+
"go.uber.org/fx"
14+
"net"
15+
"net/http"
16+
"net/url"
17+
)
18+
19+
var log = logging.Logger("rpc")
20+
21+
const APIV1Prefix = "/api/v1"
22+
23+
func SetupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, controller APIController) error {
24+
swagger, err := api.GetSwagger()
25+
if err != nil {
26+
return err
27+
}
28+
29+
// Clear out the servers array in the swagger spec, that skips validating
30+
// that server names match. We don't know how this thing will be run.
31+
swagger.Servers = nil
32+
// This is how you set up a basic chi router
33+
r := chi.NewRouter()
34+
35+
// Use our validation middleware to check all requests against the
36+
// OpenAPI schema.
37+
r.Use(
38+
cors.Handler(cors.Options{
39+
// Basic CORS
40+
// for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing
41+
42+
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
43+
AllowedOrigins: []string{"https://*", "http://*"},
44+
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
45+
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
46+
AllowedHeaders: []string{"*"},
47+
ExposedHeaders: []string{"*"},
48+
AllowCredentials: false,
49+
MaxAge: 300, // Maximum value not ignored by any of major browsers
50+
}),
51+
52+
middleware.OapiRequestValidatorWithOptions(swagger, &middleware.Options{
53+
Options: openapi3filter.Options{
54+
AuthenticationFunc: func(ctx context.Context, input *openapi3filter.AuthenticationInput) error {
55+
return nil
56+
},
57+
},
58+
}),
59+
)
60+
61+
api.HandlerFromMuxWithBaseURL(controller, r, APIV1Prefix)
62+
63+
url, err := url.Parse(apiConfig.Listen)
64+
if err != nil {
65+
return err
66+
}
67+
68+
listener, err := net.Listen("tcp", url.Host)
69+
if err != nil {
70+
return err
71+
}
72+
log.Infof("Start listen api %s", listener.Addr())
73+
go func() {
74+
err := http.Serve(listener, r)
75+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
76+
log.Errorf("listen address fail %s", err)
77+
}
78+
}()
79+
80+
lc.Append(fx.Hook{
81+
OnStop: func(ctx context.Context) error {
82+
return listener.Close()
83+
},
84+
})
85+
return nil
86+
}
File renamed without changes.

api/jiaozifs.gen.go

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
openapi: "3.0.0"
22

33
info:
4-
description: lakeFS HTTP API
5-
title: lakeFS API
4+
description: jiaozifs HTTP API
5+
title: jiaozifs API
66
license:
77
name: "Apache 2.0"
88
url: https://www.apache.org/licenses/LICENSE-2.0.html
99
version: 1.0.0
1010

1111
servers:
1212
- url: "/api/v1"
13-
description: lakeFS server endpoint
13+
description: jiaozifs server endpoint
14+
15+
security:
16+
- jwt_token: ["aaaa"]
17+
- basic_auth: ["aaaaa"]
1418
components:
19+
securitySchemes:
20+
basic_auth:
21+
type: http
22+
scheme: basic
23+
jwt_token:
24+
type: http
25+
scheme: bearer
26+
bearerFormat: JWT
27+
cookie_auth:
28+
type: apiKey
29+
in: cookie
30+
name: internal_auth_session
31+
oidc_auth:
32+
type: apiKey
33+
in: cookie
34+
name: oidc_auth_session
35+
saml_auth:
36+
type: apiKey
37+
in: cookie
38+
name: saml_auth_session
1539
schemas:
1640
VersionResult:
1741
type: object
@@ -40,6 +64,8 @@ paths:
4064
- common
4165
operationId: getVersion
4266
summary: return program and runtime version
67+
security:
68+
- jwt_token: []
4369
responses:
4470
200:
4571
description: program version

auth/basic_auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package auth

cmd/daemon.go

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ package cmd
55

66
import (
77
"context"
8-
"errors"
9-
"github.com/go-chi/chi/v5"
108
logging "github.com/ipfs/go-log/v2"
11-
"github.com/jiaozifs/jiaozifs/api"
129
"github.com/jiaozifs/jiaozifs/api/api_impl"
1310
"github.com/jiaozifs/jiaozifs/config"
1411
"github.com/jiaozifs/jiaozifs/fx_opt"
12+
"github.com/jiaozifs/jiaozifs/models"
13+
"github.com/jiaozifs/jiaozifs/models/migrations"
1514
"github.com/jiaozifs/jiaozifs/utils"
16-
middleware "github.com/oapi-codegen/nethttp-middleware"
1715
"github.com/spf13/cobra"
18-
"go.uber.org/fx"
19-
"net"
20-
"net/http"
21-
"net/url"
16+
"github.com/spf13/viper"
17+
"github.com/uptrace/bun"
2218
)
2319

2420
var log = logging.Logger("main")
@@ -46,9 +42,13 @@ var daemonCmd = &cobra.Command{
4642
//config
4743
fx_opt.Override(new(*config.Config), cfg),
4844
fx_opt.Override(new(*config.APIConfig), &cfg.API),
49-
//business
45+
fx_opt.Override(new(*config.DatabaseConfig), &cfg.Database),
46+
//database
47+
fx_opt.Override(new(*bun.DB), models.SetupDatabase),
48+
fx_opt.Override(fx_opt.NextInvoke(), migrations.MigrateDatabase),
49+
fx_opt.Override(new(*models.IUserRepo), models.NewUserRepo),
5050
//api
51-
fx_opt.Override(fx_opt.NextInvoke(), setupAPI),
51+
fx_opt.Override(fx_opt.NextInvoke(), api_impl.SetupAPI),
5252
)
5353
if err != nil {
5454
return err
@@ -65,47 +65,9 @@ var daemonCmd = &cobra.Command{
6565

6666
func init() {
6767
rootCmd.AddCommand(daemonCmd)
68-
}
69-
70-
func setupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, controller api_impl.APIController) error {
71-
swagger, err := api.GetSwagger()
72-
if err != nil {
73-
return err
74-
}
75-
76-
// Clear out the servers array in the swagger spec, that skips validating
77-
// that server names match. We don't know how this thing will be run.
78-
swagger.Servers = nil
79-
// This is how you set up a basic chi router
80-
r := chi.NewRouter()
81-
82-
// Use our validation middleware to check all requests against the
83-
// OpenAPI schema.
84-
r.Use(middleware.OapiRequestValidator(swagger))
85-
86-
api.HandlerFromMux(controller, r)
87-
88-
url, err := url.Parse(apiConfig.Listen)
89-
if err != nil {
90-
return err
91-
}
92-
93-
listener, err := net.Listen("tcp", url.Host)
94-
if err != nil {
95-
return err
96-
}
97-
log.Infof("Start listen api %s", listener.Addr())
98-
go func() {
99-
err := http.Serve(listener, r)
100-
if err != nil && !errors.Is(err, http.ErrServerClosed) {
101-
log.Errorf("listen address fail %s", err)
102-
}
103-
}()
68+
daemonCmd.Flags().String("db", "", "pg connection string eg. postgres://user:pass@localhost:5432/jiaozifs?sslmode=disable")
69+
daemonCmd.Flags().String("log-level", "INFO", "set log level eg. DEBUG INFO ERROR")
10470

105-
lc.Append(fx.Hook{
106-
OnStop: func(ctx context.Context) error {
107-
return listener.Close()
108-
},
109-
})
110-
return nil
71+
viper.BindPFlag("database.connection", daemonCmd.Flags().Lookup("db"))
72+
viper.BindPFlag("log.level", daemonCmd.Flags().Lookup("log-level"))
11173
}

cmd/init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ package cmd
66
import (
77
"github.com/jiaozifs/jiaozifs/config"
88
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
910
)
1011

1112
// initCmd represents the init command
1213
var initCmd = &cobra.Command{
1314
Use: "init",
1415
Short: "init jiaozifs ",
1516
Long: `create default config file for jiaoozifs`,
17+
PreRun: func(cmd *cobra.Command, args []string) {
18+
//provent duplicate bind flag with daemon
19+
viper.BindPFlag("database.connection", cmd.Flags().Lookup("db"))
20+
},
1621
RunE: func(cmd *cobra.Command, args []string) error {
1722
return config.InitConfig()
1823
},
1924
}
2025

2126
func init() {
2227
rootCmd.AddCommand(initCmd)
28+
initCmd.Flags().String("db", "", "pg connection string eg. postgres://user:pass@localhost:5432/jiaozifs?sslmode=disable")
2329
}

cmd/root.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
89
"os"
910
)
1011

@@ -15,9 +16,6 @@ var rootCmd = &cobra.Command{
1516
Use: "jiaozifs",
1617
Short: "version file for manage datasets",
1718
Long: ``,
18-
// Uncomment the following line if your bare application
19-
// has an action associated with it:
20-
// Run: func(cmd *cobra.Command, args []string) { },
2119
}
2220

2321
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -31,5 +29,5 @@ func Execute() {
3129

3230
func init() {
3331
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.jiaozifs/config.yaml)")
34-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
32+
viper.BindPFlag("config", rootCmd.Flags().Lookup("config"))
3533
}

cmd/version.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cmd
66
import (
77
"fmt"
88
"github.com/jiaozifs/jiaozifs/api"
9+
"github.com/jiaozifs/jiaozifs/api/api_impl"
910
"github.com/jiaozifs/jiaozifs/config"
1011
"github.com/jiaozifs/jiaozifs/version"
1112
"github.com/spf13/cobra"
@@ -17,16 +18,19 @@ var versionCmd = &cobra.Command{
1718
Short: "version of jiaozifs",
1819
Long: `jiaozifs version`,
1920
RunE: func(cmd *cobra.Command, args []string) error {
20-
cfg, err := config.LoadConfig(cfgFile)
21+
swagger, err := api.GetSwagger()
2122
if err != nil {
2223
return err
2324
}
24-
client, err := api.NewClient(cfg.API.Listen)
25+
fmt.Println("Version ", version.UserVersion())
26+
fmt.Println("API Version ", swagger.Info.Version)
27+
28+
//get runtime version
29+
cfg, err := config.LoadConfig(cfgFile)
2530
if err != nil {
2631
return err
2732
}
28-
29-
swagger, err := api.GetSwagger()
33+
client, err := api.NewClient(cfg.API.Listen + api_impl.APIV1Prefix)
3034
if err != nil {
3135
return err
3236
}
@@ -42,8 +46,6 @@ var versionCmd = &cobra.Command{
4246
if okResp.JSON200 == nil {
4347
return fmt.Errorf("request version fail %d %s", okResp.HTTPResponse.StatusCode, okResp.HTTPResponse.Body)
4448
}
45-
fmt.Println("Version ", version.UserVersion())
46-
fmt.Println("API Version ", swagger.Info.Version)
4749
fmt.Println("Runtime Version ", okResp.JSON200.Version)
4850
fmt.Println("Runtime API Version ", okResp.JSON200.ApiVersion)
4951
return nil
@@ -52,14 +54,4 @@ var versionCmd = &cobra.Command{
5254

5355
func init() {
5456
rootCmd.AddCommand(versionCmd)
55-
56-
// Here you will define your flags and configuration settings.
57-
58-
// Cobra supports Persistent Flags which will work for this command
59-
// and all subcommands, e.g.:
60-
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")
61-
62-
// Cobra supports local flags which will only run when this command
63-
// is called directly, e.g.:
64-
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
6557
}

0 commit comments

Comments
 (0)