Skip to content

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 9 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*.dll
*.so
*.dylib
jiaozifs
jzfs

# goland
*.idea
Expand Down
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

16 changes: 16 additions & 0 deletions README.md
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
```

27 changes: 27 additions & 0 deletions api/api_impl/common.go
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(),
})
}
86 changes: 86 additions & 0 deletions api/api_impl/server.go
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
}
22 changes: 22 additions & 0 deletions api/api_impl/utils.go
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()))
}
26 changes: 26 additions & 0 deletions api/custom_response.go
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()))
}
4 changes: 4 additions & 0 deletions api/docs.go
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
Loading