-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jiaozifs/feat/init_project
Feat/init project
- Loading branch information
Showing
34 changed files
with
2,517 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
*.dll | ||
*.so | ||
*.dylib | ||
jiaozifs | ||
jzfs | ||
|
||
# goland | ||
*.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
# jiaozifs | ||
version control file system. | ||
|
||
## quick start | ||
|
||
build | ||
```bash | ||
git clone https://github.com/jiaozifs/jiaozifs.git | ||
make build | ||
``` | ||
|
||
init and running | ||
```bash | ||
./jzfs init --db postgres://li:li123@localhost:5432/jiaozifs?sslmode=disable | ||
|
||
./jzfs daemon | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package api_impl | ||
|
||
import ( | ||
"github.com/jiaozifs/jiaozifs/api" | ||
"github.com/jiaozifs/jiaozifs/version" | ||
"go.uber.org/fx" | ||
"net/http" | ||
) | ||
|
||
var _ api.ServerInterface = (*APIController)(nil) | ||
|
||
type APIController struct { | ||
fx.In | ||
} | ||
|
||
func (A APIController) GetVersion(w *api.JiaozifsResponse, r *http.Request) { | ||
swagger, err := api.GetSwagger() | ||
if err != nil { | ||
w.RespError(err) | ||
return | ||
} | ||
|
||
w.RespJSON(api.VersionResult{ | ||
ApiVersion: swagger.Info.Version, | ||
Version: version.UserVersion(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package api_impl | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/getkin/kin-openapi/openapi3filter" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/cors" | ||
logging "github.com/ipfs/go-log/v2" | ||
"github.com/jiaozifs/jiaozifs/api" | ||
"github.com/jiaozifs/jiaozifs/config" | ||
middleware "github.com/oapi-codegen/nethttp-middleware" | ||
"go.uber.org/fx" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
var log = logging.Logger("rpc") | ||
|
||
const APIV1Prefix = "/api/v1" | ||
|
||
func SetupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, controller APIController) error { | ||
swagger, err := api.GetSwagger() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Clear out the servers array in the swagger spec, that skips validating | ||
// that server names match. We don't know how this thing will be run. | ||
swagger.Servers = nil | ||
// This is how you set up a basic chi router | ||
r := chi.NewRouter() | ||
|
||
// Use our validation middleware to check all requests against the | ||
// OpenAPI schema. | ||
r.Use( | ||
cors.Handler(cors.Options{ | ||
// Basic CORS | ||
// for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing | ||
|
||
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts | ||
AllowedOrigins: []string{"https://*", "http://*"}, | ||
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true }, | ||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, | ||
AllowedHeaders: []string{"*"}, | ||
ExposedHeaders: []string{"*"}, | ||
AllowCredentials: false, | ||
MaxAge: 300, // Maximum value not ignored by any of major browsers | ||
}), | ||
|
||
middleware.OapiRequestValidatorWithOptions(swagger, &middleware.Options{ | ||
Options: openapi3filter.Options{ | ||
AuthenticationFunc: func(ctx context.Context, input *openapi3filter.AuthenticationInput) error { | ||
return nil | ||
}, | ||
}, | ||
}), | ||
) | ||
|
||
api.HandlerFromMuxWithBaseURL(controller, r, APIV1Prefix) | ||
|
||
url, err := url.Parse(apiConfig.Listen) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
listener, err := net.Listen("tcp", url.Host) | ||
if err != nil { | ||
return err | ||
} | ||
log.Infof("Start listen api %s", listener.Addr()) | ||
go func() { | ||
err := http.Serve(listener, r) | ||
if err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Errorf("listen address fail %s", err) | ||
} | ||
}() | ||
|
||
lc.Append(fx.Hook{ | ||
OnStop: func(ctx context.Context) error { | ||
return listener.Close() | ||
}, | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package api_impl | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
func writeJson(w http.ResponseWriter, v interface{}) { | ||
data, err := json.Marshal(v) | ||
if err != nil { | ||
writeError(w, err) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(data) | ||
} | ||
|
||
func writeError(w http.ResponseWriter, err error) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(err.Error())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type JiaozifsResponse struct { | ||
http.ResponseWriter | ||
} | ||
|
||
func (response *JiaozifsResponse) RespJSON(v interface{}) { | ||
data, err := json.Marshal(v) | ||
if err != nil { | ||
response.RespError(err) | ||
return | ||
} | ||
response.Header().Set("Content-Type", "application/json") | ||
response.WriteHeader(http.StatusOK) | ||
_, _ = response.Write(data) | ||
} | ||
|
||
func (response *JiaozifsResponse) RespError(err error) { | ||
response.WriteHeader(http.StatusOK) | ||
response.Write([]byte(err.Error())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package apigen provides generated code for our OpenAPI | ||
package api | ||
|
||
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -package api -templates ./tmpls -generate "types,client,chi-server,spec" -o jiaozifs.gen.go ./swagger.yml |
Oops, something went wrong.