-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
77 lines (59 loc) · 1.68 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
"os"
"path/filepath"
)
type Server struct {
baseDir string
bookmarksDB *sqlx.DB
isLocal bool
}
func (s *Server) userDataPath() string {
return filepath.Join(s.baseDir, "data")
}
// Server for running plain and locally
func initLocalServer() (s Server, err error) {
// Can't mess with /var if not in sandstorm so we put it somewhere else.
// For now, not randomizing it so that it syncs with the python server.
// And, this will probably be so rarely used that I don't care to randomize it anyway.
// If anything maybe it should be a param that the user can pass in so they
// can save it in their home dir in between runs.
const localBase = "/tmp/desert-atlas-fe66b63c13a042734a5aee2341fa1240"
if err = makeDirExist(localBase); err != nil {
return
}
s = Server{baseDir: localBase, isLocal: true}
if err = s.makeSubDirs(); err != nil {
return
}
err = s.initBookmarksDB()
return
}
// Create dir if not exists yet
func makeDirExist(path string) error {
_, err := os.Stat(path)
// No error means it exists, so nothing to initialize
if err == nil {
return nil
}
if !os.IsNotExist(err) {
// Some error other than os.IsNotExist means all bets are off, don't proceed
return fmt.Errorf("Unknown error checking for dir: %s", path)
}
// os.IsNotExist error means it doesn't exist yet, initialize it
return os.Mkdir(path, 0750)
}
// Server for running inside Sandstorm
func initSandstormServer() (s Server, err error) {
s = Server{baseDir: "/var"}
if err = s.makeSubDirs(); err != nil {
return
}
err = s.initBookmarksDB()
return
}
func (s *Server) makeSubDirs() error {
return makeDirExist(s.userDataPath())
}