Unverified Commit 15176880 authored by boojack's avatar boojack Committed by GitHub

chore: update code structure (#1139)

* chore: update code structure

* chore: update
parent 29124f56
...@@ -15,7 +15,7 @@ RUN apk update && apk add --no-cache gcc musl-dev ...@@ -15,7 +15,7 @@ RUN apk update && apk add --no-cache gcc musl-dev
COPY . . COPY . .
COPY --from=frontend /frontend-build/dist ./server/dist COPY --from=frontend /frontend-build/dist ./server/dist
RUN go build -o memos ./bin/server/main.go RUN go build -o memos ./main.go
# Make workspace with above generated files. # Make workspace with above generated files.
FROM alpine:3.16 AS monolithic FROM alpine:3.16 AS monolithic
......
package main package cmd
import ( import (
"context"
"fmt"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
_ "github.com/mattn/go-sqlite3" "github.com/spf13/cobra"
"github.com/spf13/viper"
"context"
"fmt"
"github.com/usememos/memos/server" "github.com/usememos/memos/server"
"github.com/usememos/memos/server/profile" _profile "github.com/usememos/memos/server/profile"
) )
const ( const (
...@@ -26,20 +26,16 @@ const ( ...@@ -26,20 +26,16 @@ const (
` `
) )
func main() { var (
profile, err := profile.GetProfile() profile *_profile.Profile
if err != nil { mode string
fmt.Printf("failed to get profile, error: %+v\n", err) port int
return data string
}
println("---")
println("profile")
println("mode:", profile.Mode)
println("port:", profile.Port)
println("dsn:", profile.DSN)
println("version:", profile.Version)
println("---")
rootCmd = &cobra.Command{
Use: "memos",
Short: `An open-source, self-hosted memo hub with knowledge management and social networking.`,
Run: func(_cmd *cobra.Command, _args []string) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
s, err := server.NewServer(ctx, profile) s, err := server.NewServer(ctx, profile)
if err != nil { if err != nil {
...@@ -71,4 +67,53 @@ func main() { ...@@ -71,4 +67,53 @@ func main() {
// Wait for CTRL-C. // Wait for CTRL-C.
<-ctx.Done() <-ctx.Done()
},
}
)
func Execute() error {
return rootCmd.Execute()
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&mode, "mode", "m", "demo", `mode of server, can be "prod" or "dev" or "demo"`)
rootCmd.PersistentFlags().IntVarP(&port, "port", "p", 8081, "port of server")
rootCmd.PersistentFlags().StringVarP(&data, "data", "d", "", "data directory")
err := viper.BindPFlag("mode", rootCmd.PersistentFlags().Lookup("mode"))
if err != nil {
panic(err)
}
err = viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
if err != nil {
panic(err)
}
err = viper.BindPFlag("data", rootCmd.PersistentFlags().Lookup("data"))
if err != nil {
panic(err)
}
viper.SetDefault("mode", "demo")
viper.SetDefault("port", 8081)
viper.SetEnvPrefix("memos")
}
func initConfig() {
viper.AutomaticEnv()
var err error
profile, err = _profile.GetProfile()
if err != nil {
fmt.Printf("failed to get profile, error: %+v\n", err)
return
}
println("---")
println("Server profile")
println("dsn:", profile.DSN)
println("port:", profile.Port)
println("mode:", profile.Mode)
println("version:", profile.Version)
println("---")
} }
...@@ -36,28 +36,40 @@ require ( ...@@ -36,28 +36,40 @@ require (
github.com/aws/smithy-go v1.13.5 // indirect github.com/aws/smithy-go v1.13.5 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect
github.com/labstack/gommon v0.3.1 // indirect github.com/labstack/gommon v0.3.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/backo-go v1.0.1 // indirect github.com/segmentio/backo-go v1.0.1 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.6.0 // indirect go.uber.org/multierr v1.8.0 // indirect
golang.org/x/sys v0.5.0 // indirect golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect golang.org/x/time v0.1.0 // indirect
google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
...@@ -69,7 +81,9 @@ require ( ...@@ -69,7 +81,9 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.30.3 github.com/aws/aws-sdk-go-v2/service/s3 v1.30.3
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/segmentio/analytics-go v3.1.0+incompatible github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/stretchr/testify v1.8.0 github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.24.0 go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230111222715-75897c7a292a golang.org/x/exp v0.0.0-20230111222715-75897c7a292a
golang.org/x/mod v0.6.0 golang.org/x/mod v0.6.0
......
This diff is collapsed.
package main
import (
_ "github.com/mattn/go-sqlite3"
"github.com/usememos/memos/cmd"
)
func main() {
err := cmd.Execute()
if err != nil {
panic(err)
}
}
...@@ -3,7 +3,7 @@ tmp_dir = ".air" ...@@ -3,7 +3,7 @@ tmp_dir = ".air"
[build] [build]
bin = "./.air/memos" bin = "./.air/memos"
cmd = "go build -o ./.air/memos ./bin/server/main.go" cmd = "go build -o ./.air/memos ./main.go"
delay = 1000 delay = 1000
exclude_dir = [".air", "web", "build"] exclude_dir = [".air", "web", "build"]
exclude_file = [] exclude_file = []
......
...@@ -8,6 +8,6 @@ cd "$(dirname "$0")/../" ...@@ -8,6 +8,6 @@ cd "$(dirname "$0")/../"
echo "Start building backend..." echo "Start building backend..."
go build -o ./build/memos ./bin/server/main.go go build -o ./build/memos ./main.go
echo "Backend built!" echo "Backend built!"
package profile package profile
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/spf13/viper"
"github.com/usememos/memos/server/version" "github.com/usememos/memos/server/version"
) )
...@@ -47,10 +47,10 @@ func checkDSN(dataDir string) (string, error) { ...@@ -47,10 +47,10 @@ func checkDSN(dataDir string) (string, error) {
// GetDevProfile will return a profile for dev or prod. // GetDevProfile will return a profile for dev or prod.
func GetProfile() (*Profile, error) { func GetProfile() (*Profile, error) {
profile := Profile{} profile := Profile{}
flag.StringVar(&profile.Mode, "mode", "demo", "mode of server") err := viper.Unmarshal(&profile)
flag.IntVar(&profile.Port, "port", 8081, "port of server") if err != nil {
flag.StringVar(&profile.Data, "data", "", "data directory") return nil, err
flag.Parse() }
if profile.Mode != "demo" && profile.Mode != "dev" && profile.Mode != "prod" { if profile.Mode != "demo" && profile.Mode != "dev" && profile.Mode != "prod" {
profile.Mode = "demo" profile.Mode = "demo"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment