chore: update profile

This commit is contained in:
Steven 2023-07-14 13:07:01 +08:00
parent 316617c396
commit a0766159f2
3 changed files with 44 additions and 15 deletions

View File

@ -17,7 +17,7 @@ WORKDIR /backend-build
COPY . . COPY . .
COPY --from=frontend /frontend-build/dist ./server/dist 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. # Make workspace with above generated files.
FROM alpine:3.16 AS monolithic FROM alpine:3.16 AS monolithic
@ -28,7 +28,13 @@ ENV TZ="UTC"
COPY --from=backend /backend-build/slash /usr/local/slash/ COPY --from=backend /backend-build/slash /usr/local/slash/
EXPOSE 5231
# Directory to store the data, which can be referenced as the mounting point. # Directory to store the data, which can be referenced as the mounting point.
RUN mkdir -p /var/opt/slash 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"]

View File

@ -18,6 +18,10 @@ import (
"github.com/boojack/slash/store/db" "github.com/boojack/slash/store/db"
) )
const (
greetingBanner = `Welcome to Slash!`
)
var ( var (
profile *_profile.Profile profile *_profile.Profile
mode string mode string
@ -26,7 +30,7 @@ var (
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "slash", Use: "slash",
Short: "", Short: `A bookmarking and url shortener, save and share your links very easily.`,
Run: func(_cmd *cobra.Command, _args []string) { Run: func(_cmd *cobra.Command, _args []string) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
db := db.NewDB(profile) db := db.NewDB(profile)
@ -56,7 +60,7 @@ var (
cancel() cancel()
}() }()
println("Welcome to Slash!") println(greetingBanner)
fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port) fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
if err := s.Start(ctx); err != nil { if err := s.Start(ctx); err != nil {
if err != http.ErrServerClosed { if err != http.ErrServerClosed {

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"github.com/boojack/slash/server/version" "github.com/boojack/slash/server/version"
@ -12,30 +13,35 @@ import (
// Profile is the configuration to start main server. // Profile is the configuration to start main server.
type Profile struct { 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 can be "prod" or "dev"
Mode string `json:"mode"` Mode string `json:"mode"`
// Port is the binding port for server // 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 is the current version of server
Version string `json:"version"` Version string `json:"version"`
} }
func (p *Profile) IsDev() bool {
return p.Mode != "prod"
}
func checkDSN(dataDir string) (string, error) { func checkDSN(dataDir string) (string, error) {
// Convert to absolute path if relative path is supplied. // Convert to absolute path if relative path is supplied.
if !filepath.IsAbs(dataDir) { 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 { if err != nil {
return "", err return "", err
} }
dataDir = absDir dataDir = absDir
} }
// Trim trailing / in case user supplies // Trim trailing \ or / in case user supplies
dataDir = strings.TrimRight(dataDir, "/") dataDir = strings.TrimRight(dataDir, "\\/")
if _, err := os.Stat(dataDir); err != nil { if _, err := os.Stat(dataDir); err != nil {
return "", fmt.Errorf("unable to access data folder %s, err %w", dataDir, err) 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 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) { func GetProfile() (*Profile, error) {
profile := Profile{} profile := Profile{}
err := viper.Unmarshal(&profile) err := viper.Unmarshal(&profile)
@ -57,7 +63,18 @@ func GetProfile() (*Profile, error) {
} }
if profile.Mode == "prod" && profile.Data == "" { 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) dataDir, err := checkDSN(profile.Data)
@ -67,7 +84,9 @@ func GetProfile() (*Profile, error) {
} }
profile.Data = dataDir 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) profile.Version = version.GetCurrentVersion(profile.Mode)
return &profile, nil return &profile, nil
} }