-
Notifications
You must be signed in to change notification settings - Fork 11
Feat/init project #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc02ce6
feat:init project
hunjixin 42c3237
feat: add cobra flag and config
hunjixin 6a016e1
feat: add cofig operation
hunjixin 4f2f746
feat: make version command work
hunjixin d8c6bca
feat: use custom response
hunjixin 47aa612
更新jiaozifs_mind.drawio
hunjixin 6f84891
feat: perform orm and cors function
hunjixin 6e9dafd
feat: add simple quick start
hunjixin 1011513
chore: rename executable file name
hunjixin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.