mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
chore: update profile
This commit is contained in:
parent
316617c396
commit
a0766159f2
10
Dockerfile
10
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"]
|
||||
|
@ -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 {
|
||||
|
@ -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,8 +63,19 @@ func GetProfile() (*Profile, error) {
|
||||
}
|
||||
|
||||
if profile.Mode == "prod" && profile.Data == "" {
|
||||
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)
|
||||
if err != nil {
|
||||
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user