Skip to content

Commit

Permalink
optimize sail/examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed May 24, 2024
1 parent a7e0fdd commit d5d152e
Show file tree
Hide file tree
Showing 144 changed files with 8,474 additions and 59 deletions.
18 changes: 14 additions & 4 deletions sail/examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
GOFMT ?= gofmt -s -w
GOFILES := $(shell find . -name "*.go" -type f)
TAGS ?= go_json
LDFLAGS = -w -s

BUILD_VERSION := $(shell git describe --tags --always)
BUILD_DATE := $(shell date +'%Y-%m-%d %H:%M:%S %Z')
SHA_SHORT := $(shell git rev-parse --short HEAD)

MOD_NAME = github.com/alimy/mir/sail/mir-example/v4
LDFLAGS = -X "${MOD_NAME}/internal/conf.version=${BUILD_VERSION}" \
-X "${MOD_NAME}/internal/conf.buildDate=${BUILD_DATE}" \
-X "${MOD_NAME}/internal/conf.commitID=${SHA_SHORT}" \
-X "${MOD_NAME}/internal/conf.buildTags=${TAGS}" \
-w -s

.PHONY: default
default: run
default: serve

.PHONY: build
build: fmt
go build -trimpath -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o mir-examples main.go

.PHONY: build
run: fmt
go run -trimpath -tags '$(TAGS)' -ldflags '$(LDFLAGS)' main.go
serve: fmt
go run -trimpath -tags '$(TAGS)' -ldflags '$(LDFLAGS)' main.go serve

.PHONY: mod-tidy
mod-tidy:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions sail/examples/cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Michael Li <[email protected]>. All rights reserved.
// Use of this source code is governed by Apache License 2.0 that
// can be found in the LICENSE file.

package migrate

import (
"fmt"

"github.com/alimy/mir/sail/mir-example/v4/cmd"
"github.com/spf13/cobra"
)

func init() {
migrateCmd := &cobra.Command{
Use: "migrate",
Short: "migrate database data",
Long: "miegrate database data when mir-examples upgrade",
Run: migrateRun,
}
cmd.Register(migrateCmd)
}

func migrateRun(_cmd *cobra.Command, _args []string) {
// TODO: add some logic for migrate cmd feature
fmt.Println("sorry, this feature is not implemented yet.")
}
36 changes: 36 additions & 0 deletions sail/examples/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Michael Li <[email protected]>. All rights reserved.
// Use of this source code is governed by Apache License 2.0 that
// can be found in the LICENSE file.

package cmd

import (
"github.com/spf13/cobra"
)

var (
rootCmd = &cobra.Command{
Use: "mir-example",
Short: `an artistic "twitter like" community`,
Long: `an artistic "twitter like" community`,
}
)

// Setup set root command name,short-describe, long-describe
// return &cobra.Command to custom other options
func Setup(use, short, long string) *cobra.Command {
rootCmd.Use = use
rootCmd.Short = short
rootCmd.Long = long
return rootCmd
}

// Register add sub-command
func Register(cmd *cobra.Command) {
rootCmd.AddCommand(cmd)
}

// Execute start application
func Execute() {
rootCmd.Execute()
}
65 changes: 65 additions & 0 deletions sail/examples/cmd/serve/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 Michael Li <[email protected]>. All rights reserved.
// Use of this source code is governed by Apache License 2.0 that
// can be found in the LICENSE file.

package serve

import (
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/alimy/mir/sail/mir-example/v4/cmd"
"github.com/alimy/mir/sail/mir-example/v4/internal/service"

"github.com/fatih/color"
"github.com/sourcegraph/conc"
"github.com/spf13/cobra"
"go.uber.org/automaxprocs/maxprocs"
)

var (
noDefaultFeatures bool
features []string
)

func init() {
serveCmd := &cobra.Command{
Use: "serve",
Short: "start mir-example server",
Long: "start mir-example server",
Run: serveRun,
}

serveCmd.Flags().BoolVar(&noDefaultFeatures, "no-default-features", false, "whether not use default features")
serveCmd.Flags().StringSliceVarP(&features, "features", "f", []string{}, "use special features")

cmd.Register(serveCmd)
}

func serveRun(_cmd *cobra.Command, _args []string) {
// set maxprocs automatic
maxprocs.Set(maxprocs.Logger(log.Printf))

runtime := service.NewRuntime()

// start services
wg := conc.NewWaitGroup()
fmt.Fprintf(color.Output, "\nstarting run service...\n\n")
runtime.Start(wg)

// graceful stop services
wg.Go(func() {
quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Fprintf(color.Output, "\nshutting down server...\n\n")
runtime.Stop()
})
wg.Wait()
}
26 changes: 26 additions & 0 deletions sail/examples/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 Michael Li <[email protected]>. All rights reserved.
// Use of this source code is governed by Apache License 2.0 that
// can be found in the LICENSE file.

package cmd

import (
"fmt"

"github.com/alimy/mir/sail/mir-example/v4/internal/conf"
"github.com/spf13/cobra"
)

func init() {
versionCmd := &cobra.Command{
Use: "version",
Short: "show version information",
Long: "show version information",
Run: versionRun,
}
Register(versionCmd)
}

func versionRun(_cmd *cobra.Command, _args []string) {
fmt.Println(conf.VersionInfo())
}
4 changes: 4 additions & 0 deletions sail/examples/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 开发文档
本目录包含一些开发者文档。

* [openapi](openapi): api相关文档
3,776 changes: 3,776 additions & 0 deletions sail/examples/docs/openapi/assets/rapidoc-min.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions sail/examples/docs/openapi/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 Michael Li <[email protected]>. All rights reserved.
// Use of this source code is governed by Apache License 2.0 that
// can be found in the LICENSE file.

//go:build docs
// +build docs

package openapi

import (
"embed"
"net/http"
)

//go:embed index.html openapi.json **/*
var files embed.FS

// NewFileSystem get an embed static assets http.FileSystem instance.
func NewFileSystem() http.FileSystem {
return http.FS(files)
}
21 changes: 21 additions & 0 deletions sail/examples/docs/openapi/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<!-- Important: must specify -->
<html>
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<title>paopao-ce develop documents</title>
<!-- Important: rapi-doc uses utf8 characters -->
<script
type="module"
src="/docs/openapi/assets/rapidoc-min.js"
></script>
</head>
<body>
<rapi-doc
spec-url="/docs/openapi/openapi.json"
render-style="read"
>
</rapi-doc>
</body>
</html>
Loading

0 comments on commit d5d152e

Please sign in to comment.