Skip to content

Commit

Permalink
Merge branch 'test' of github.com:answerdev/answer into test
Browse files Browse the repository at this point in the history
  • Loading branch information
aichy126 committed May 16, 2023
2 parents 41faf98 + 7f46888 commit 8d421ae
Show file tree
Hide file tree
Showing 223 changed files with 10,793 additions and 2,164 deletions.
11 changes: 8 additions & 3 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ LABEL maintainer="mingcheng<mc@sf.com>"

COPY . /answer
WORKDIR /answer
RUN node -v
RUN make install-ui-packages ui && mv ui/build /tmp

# stage2 build the main binary within static resource
FROM golang:1.19-alpine AS golang-builder
LABEL maintainer="aichy@sf.com"

ARG GOPROXY
# ENV GOPROXY ${GOPROXY:-direct}
ENV GOPROXY=https://goproxy.io,direct

ENV GOPATH /go
ENV GOROOT /usr/local/go
ENV PACKAGE github.com/answerdev/answer
ENV BUILD_DIR ${GOPATH}/src/${PACKAGE}
ENV ANSWER_MODULE ${BUILD_DIR}

ARG TAGS="sqlite sqlite_unlock_notify"
ENV TAGS "bindata timetzdata $TAGS"
Expand All @@ -25,9 +28,11 @@ ARG CGO_EXTRA_CFLAGS
COPY . ${BUILD_DIR}
WORKDIR ${BUILD_DIR}
COPY --from=node-builder /tmp/build ${BUILD_DIR}/ui/build
RUN apk --no-cache add build-base git \
&& make clean build \
&& cp answer /usr/bin/answer
RUN apk --no-cache add build-base git bash \
&& make clean build
RUN chmod 755 answer
RUN ["/bin/bash","-c","script/build_plugin.sh"]
RUN cp answer /usr/bin/answer

RUN mkdir -p /data/uploads && chmod 777 /data/uploads \
&& mkdir -p /data/i18n && cp -r i18n/*.yaml /data/i18n
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ tmp
vendor/
/answer-data/
/answer
/new_answer

dist/
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ FROM golang:1.19-alpine AS golang-builder
LABEL maintainer="aichy@sf.com"

ARG GOPROXY
ENV GOPROXY ${GOPROXY:-direct}
# ENV GOPROXY ${GOPROXY:-direct}
ENV GOPROXY=https://goproxy.io,direct

ENV GOPATH /go
ENV GOROOT /usr/local/go
ENV PACKAGE github.com/answerdev/answer
ENV BUILD_DIR ${GOPATH}/src/${PACKAGE}
ENV ANSWER_MODULE ${BUILD_DIR}

ARG TAGS="sqlite sqlite_unlock_notify"
ENV TAGS "bindata timetzdata $TAGS"
Expand All @@ -27,9 +29,11 @@ ARG CGO_EXTRA_CFLAGS
COPY . ${BUILD_DIR}
WORKDIR ${BUILD_DIR}
COPY --from=node-builder /tmp/build ${BUILD_DIR}/ui/build
RUN apk --no-cache add build-base git \
&& make clean build \
&& cp answer /usr/bin/answer
RUN apk --no-cache add build-base git bash \
&& make clean build
RUN chmod 755 answer
RUN ["/bin/bash","-c","script/build_plugin.sh"]
RUN cp answer /usr/bin/answer

RUN mkdir -p /data/uploads && chmod 777 /data/uploads \
&& mkdir -p /data/i18n && cp -r i18n/*.yaml /data/i18n
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.PHONY: build clean ui

VERSION=1.0.9
VERSION=1.1.0
BIN=answer
DIR_SRC=./cmd/answer
DOCKER_CMD=docker

GO_ENV=CGO_ENABLED=0 GO111MODULE=on
Revision=$(shell git rev-parse --short HEAD)
GO_FLAGS=-ldflags="-X main.Version=$(VERSION) -X 'main.Revision=$(Revision)' -X 'main.Time=`date`' -extldflags -static"
GO_FLAGS=-ldflags="-X github.com/answerdev/answer/cmd.Version=$(VERSION) -X 'github.com/answerdev/answer/cmd.Revision=$(Revision)' -X 'github.com/answerdev/answer/cmd.Time=`date +%s`' -extldflags -static"
GO=$(GO_ENV) $(shell which go)

build: generate
Expand Down
64 changes: 2 additions & 62 deletions cmd/answer/main.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,12 @@
package main

import (
"os"
"time"

"github.com/answerdev/answer/internal/base/conf"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/cron"
"github.com/answerdev/answer/internal/cli"
"github.com/answerdev/answer/internal/schema"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman"
"github.com/segmentfault/pacman/contrib/log/zap"
"github.com/segmentfault/pacman/contrib/server/http"
"github.com/segmentfault/pacman/log"
)

// go build -ldflags "-X main.Version=x.y.z"
var (
// Name is the name of the project
Name = "answer"
// Version is the version of the project
Version = "0.0.0"
// Revision is the git short commit revision number
Revision = ""
// Time is the build time of the project
Time = ""
// log level
logLevel = os.Getenv("LOG_LEVEL")
// log path
logPath = os.Getenv("LOG_PATH")
answercmd "github.com/answerdev/answer/cmd"
)

// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
log.SetLogger(zap.NewLogger(
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
Execute()
}

func runApp() {
c, err := conf.ReadConfig(cli.GetConfigFilePath())
if err != nil {
panic(err)
}
conf.GetPathIgnoreList()
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
if err != nil {
panic(err)
}
constant.Version = Version
constant.Revision = Revision
schema.AppStartTime = time.Now()

defer cleanup()
if err := app.Run(); err != nil {
panic(err)
}
}

func newApplication(serverConf *conf.Server, server *gin.Engine, manager *cron.ScheduledTaskManager) *pacman.Application {
manager.Run()
return pacman.NewApp(
pacman.WithName(Name),
pacman.WithVersion(Version),
pacman.WithServer(http.NewServer(server, serverConf.HTTP.Addr)),
)
answercmd.Main()
}
59 changes: 55 additions & 4 deletions cmd/answer/command.go → cmd/command.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main
package answercmd

import (
"fmt"
"os"
"strings"

"github.com/answerdev/answer/internal/base/conf"
"github.com/answerdev/answer/internal/cli"
"github.com/answerdev/answer/internal/install"
"github.com/answerdev/answer/internal/migrations"
"github.com/answerdev/answer/plugin"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +18,14 @@ var (
dataDirPath string
// dumpDataPath dump data path
dumpDataPath string
// plugins needed to build in answer application
buildWithPlugins []string
// build output path
buildOutput string
// This config is used to upgrade the database from a specific version number manually.
// The version number is calculated with `migrations.go` migrations length.
// If you want to upgrade the database from version 1 to version 2, you can use `answer upgrade -f 1`.
upgradeFromVersionNumber int
)

func init() {
Expand All @@ -25,7 +35,13 @@ func init() {

dumpCmd.Flags().StringVarP(&dumpDataPath, "path", "p", "./", "dump data path, eg: -p ./dump/data/")

for _, cmd := range []*cobra.Command{initCmd, checkCmd, runCmd, dumpCmd, upgradeCmd} {
buildCmd.Flags().StringSliceVarP(&buildWithPlugins, "with", "w", []string{}, "plugins needed to build")

buildCmd.Flags().StringVarP(&buildOutput, "output", "o", "", "build output path")

upgradeCmd.Flags().IntVarP(&upgradeFromVersionNumber, "from", "f", 0, "upgrade from specific version number that in database version table")

for _, cmd := range []*cobra.Command{initCmd, checkCmd, runCmd, dumpCmd, upgradeCmd, buildCmd, pluginCmd} {
rootCmd.AddCommand(cmd)
}
}
Expand Down Expand Up @@ -91,12 +107,13 @@ To run answer, use:
Long: `upgrade Answer version`,
Run: func(_ *cobra.Command, _ []string) {
cli.FormatAllPath(dataDirPath)
cli.InstallI18nBundle(true)
c, err := conf.ReadConfig(cli.GetConfigFilePath())
if err != nil {
fmt.Println("read config failed: ", err.Error())
return
}
if err = migrations.Migrate(c.Data.Database, c.Data.Cache); err != nil {
if err = migrations.Migrate(c.Data.Database, c.Data.Cache, upgradeFromVersionNumber); err != nil {
fmt.Println("migrate failed: ", err.Error())
return
}
Expand Down Expand Up @@ -160,10 +177,44 @@ To run answer, use:
fmt.Println("check environment all done")
},
}

// buildCmd used to build another answer with plugins
buildCmd = &cobra.Command{
Use: "build",
Short: "used to build answer with plugins",
Long: `Build a new Answer with plugins that you need`,
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("try to build a new answer with plugins:\n%s\n", strings.Join(buildWithPlugins, "\n"))
err := cli.BuildNewAnswer(buildOutput, buildWithPlugins, cli.OriginalAnswerInfo{
Version: Version,
Revision: Revision,
Time: Time,
})
if err != nil {
fmt.Printf("build failed %v", err)
} else {
fmt.Printf("build new answer successfully %s\n", buildOutput)
}
},
}

// pluginCmd prints all plugins packed in the binary
pluginCmd = &cobra.Command{
Use: "plugin",
Short: "prints all plugins packed in the binary",
Long: `prints all plugins packed in the binary`,
Run: func(_ *cobra.Command, _ []string) {
_ = plugin.CallBase(func(base plugin.Base) error {
info := base.Info()
fmt.Printf("%s[%s] made by %s\n", info.SlugName, info.Version, info.Author)
return nil
})
},
}
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// This is called by main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand Down
75 changes: 75 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package answercmd

import (
"fmt"
"os"
"time"

"github.com/answerdev/answer/internal/base/conf"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/cron"
"github.com/answerdev/answer/internal/cli"
"github.com/answerdev/answer/internal/schema"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman"
"github.com/segmentfault/pacman/contrib/log/zap"
"github.com/segmentfault/pacman/contrib/server/http"
"github.com/segmentfault/pacman/log"
)

// go build -ldflags "-X github.com/answerdev/answer/cmd.Version=x.y.z"
var (
// Name is the name of the project
Name = "answer"
// Version is the version of the project
Version = "0.0.0"
// Revision is the git short commit revision number
Revision = "-"
// Time is the build time of the project
Time = "-"
// log level
logLevel = os.Getenv("LOG_LEVEL")
// log path
logPath = os.Getenv("LOG_PATH")
)

// Main
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func Main() {
log.SetLogger(zap.NewLogger(
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
Execute()
}

func runApp() {
c, err := conf.ReadConfig(cli.GetConfigFilePath())
if err != nil {
panic(err)
}
conf.GetPathIgnoreList()
app, cleanup, err := initApplication(
c.Debug, c.Server, c.Data.Database, c.Data.Cache, c.I18n, c.Swaggerui, c.ServiceConfig, log.GetLogger())
if err != nil {
panic(err)
}
constant.Version = Version
constant.Revision = Revision
schema.AppStartTime = time.Now()
fmt.Println("answer Version:", constant.Version, " Revision:", constant.Revision)

defer cleanup()
if err := app.Run(); err != nil {
panic(err)
}
}

func newApplication(serverConf *conf.Server, server *gin.Engine, manager *cron.ScheduledTaskManager) *pacman.Application {
manager.Run()
return pacman.NewApp(
pacman.WithName(Name),
pacman.WithVersion(Version),
pacman.WithServer(http.NewServer(server, serverConf.HTTP.Addr)),
)
}
2 changes: 1 addition & 1 deletion cmd/answer/wire.go → cmd/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// The build tag makes sure the stub is not built in the final build.

package main
package answercmd

import (
"github.com/answerdev/answer/internal/base/conf"
Expand Down
Loading

0 comments on commit 8d421ae

Please sign in to comment.