From a0766159f20ce5cb33f20536d8997335b445a1cb Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 14 Jul 2023 13:07:01 +0800 Subject: [PATCH] chore: update profile --- Dockerfile | 10 ++++++++-- cmd/slash/main.go | 8 ++++++-- server/profile/profile.go | 41 ++++++++++++++++++++++++++++----------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 46b7ef0..7287fc3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ WORKDIR /backend-build COPY . . COPY --from=frontend /frontend-build/dist ./server/dist -RUN go build -o slash ./cmd/slash/main.go +RUN CGO_ENABLED=0 go build -o slash ./cmd/slash/main.go # Make workspace with above generated files. FROM alpine:3.16 AS monolithic @@ -28,7 +28,13 @@ ENV TZ="UTC" COPY --from=backend /backend-build/slash /usr/local/slash/ +EXPOSE 5231 + # Directory to store the data, which can be referenced as the mounting point. RUN mkdir -p /var/opt/slash +VOLUME /var/opt/slash -ENTRYPOINT ["./slash", "--mode", "prod", "--port", "5231"] +ENV SLASH_MODE="prod" +ENV SLASH_PORT="5231" + +ENTRYPOINT ["./slash"] diff --git a/cmd/slash/main.go b/cmd/slash/main.go index bd36ed2..93dd0ef 100644 --- a/cmd/slash/main.go +++ b/cmd/slash/main.go @@ -18,6 +18,10 @@ import ( "github.com/boojack/slash/store/db" ) +const ( + greetingBanner = `Welcome to Slash!` +) + var ( profile *_profile.Profile mode string @@ -26,7 +30,7 @@ var ( rootCmd = &cobra.Command{ Use: "slash", - Short: "", + Short: `A bookmarking and url shortener, save and share your links very easily.`, Run: func(_cmd *cobra.Command, _args []string) { ctx, cancel := context.WithCancel(context.Background()) db := db.NewDB(profile) @@ -56,7 +60,7 @@ var ( cancel() }() - println("Welcome to Slash!") + println(greetingBanner) fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port) if err := s.Start(ctx); err != nil { if err != http.ErrServerClosed { diff --git a/server/profile/profile.go b/server/profile/profile.go index 8109214..0bc6eaa 100644 --- a/server/profile/profile.go +++ b/server/profile/profile.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "strings" "github.com/boojack/slash/server/version" @@ -12,30 +13,35 @@ import ( // Profile is the configuration to start main server. type Profile struct { - // Data is the data directory - Data string `json:"-"` - // DSN points to store data - DSN string `json:"-"` // Mode can be "prod" or "dev" Mode string `json:"mode"` // Port is the binding port for server - Port int `json:"port"` + Port int `json:"-"` + // Data is the data directory + Data string `json:"-"` + // DSN points to where slash stores its own data + DSN string `json:"-"` // Version is the current version of server Version string `json:"version"` } +func (p *Profile) IsDev() bool { + return p.Mode != "prod" +} + func checkDSN(dataDir string) (string, error) { // Convert to absolute path if relative path is supplied. if !filepath.IsAbs(dataDir) { - absDir, err := filepath.Abs(filepath.Dir(os.Args[0]) + "/" + dataDir) + relativeDir := filepath.Join(filepath.Dir(os.Args[0]), dataDir) + absDir, err := filepath.Abs(relativeDir) if err != nil { return "", err } dataDir = absDir } - // Trim trailing / in case user supplies - dataDir = strings.TrimRight(dataDir, "/") + // Trim trailing \ or / in case user supplies + dataDir = strings.TrimRight(dataDir, "\\/") if _, err := os.Stat(dataDir); err != nil { return "", fmt.Errorf("unable to access data folder %s, err %w", dataDir, err) @@ -44,7 +50,7 @@ func checkDSN(dataDir string) (string, error) { return dataDir, nil } -// GetDevProfile will return a profile for dev or prod. +// GetProfile will return a profile for dev or prod. func GetProfile() (*Profile, error) { profile := Profile{} err := viper.Unmarshal(&profile) @@ -57,7 +63,18 @@ func GetProfile() (*Profile, error) { } if profile.Mode == "prod" && profile.Data == "" { - profile.Data = "/var/opt/slash" + if runtime.GOOS == "windows" { + profile.Data = filepath.Join(os.Getenv("ProgramData"), "slash") + + if _, err := os.Stat(profile.Data); os.IsNotExist(err) { + if err := os.MkdirAll(profile.Data, 0770); err != nil { + fmt.Printf("Failed to create data directory: %s, err: %+v\n", profile.Data, err) + return nil, err + } + } + } else { + profile.Data = "/var/opt/slash" + } } dataDir, err := checkDSN(profile.Data) @@ -67,7 +84,9 @@ func GetProfile() (*Profile, error) { } profile.Data = dataDir - profile.DSN = fmt.Sprintf("%s/slash_%s.db", dataDir, profile.Mode) + dbFile := fmt.Sprintf("slash_%s.db", profile.Mode) + profile.DSN = filepath.Join(dataDir, dbFile) profile.Version = version.GetCurrentVersion(profile.Mode) + return &profile, nil }