Skip to content

Commit 9955eeb

Browse files
committed
Merge pull request #9 from nak3/cleanup-initial-process
[fix] clean up startup process
2 parents 282ce8e + 205330c commit 9955eeb

File tree

5 files changed

+45
-88
lines changed

5 files changed

+45
-88
lines changed

app/app.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ var (
1414
App *golf.Application
1515
)
1616

17-
func Init() {
18-
App = golf.New()
19-
err := model.Initialize("dingo.db")
20-
if err != nil {
21-
panic(err)
17+
func fileExists(filename string) bool {
18+
_, err := os.Stat(filename)
19+
return err == nil
20+
}
21+
22+
func Init(dbPath string) {
23+
if err := model.Initialize(dbPath, fileExists(dbPath)); err != nil {
24+
fmt.Errorf("failed to intialize db: %v", err)
25+
os.Exit(1)
2226
}
27+
fmt.Printf("Database is used at %s\n", dbPath)
28+
29+
App = golf.New()
2330

2431
App.Config.Set("app/static_dir", "static")
2532
App.Config.Set("app.log_dir", "tmp/log")
@@ -131,6 +138,6 @@ func registerHomeHandler() {
131138
func Run(portNumber string) {
132139
registerAdminURLHandlers()
133140
registerHomeHandler()
134-
fmt.Println("Application Started on port " + portNumber)
141+
fmt.Printf("Application Started on port %s\n", portNumber)
135142
App.Run(":" + portNumber)
136143
}

app/model/init.go

+28-14
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package model
22

33
import (
44
"database/sql"
5+
"time"
6+
57
"github.com/dinever/dingo/app/utils"
68
_ "github.com/mattn/go-sqlite3"
7-
"os"
8-
"time"
99
)
1010

1111
var db *sql.DB
1212

13-
1413
const samplePostContent = `
1514
Welcome to Dingo! This is your first post. You can find it in the [admin panel](/admin/).
1615
@@ -81,25 +80,40 @@ type Row interface {
8180
Scan(dest ...interface{}) error
8281
}
8382

84-
func Initialize(dbPath string) error {
83+
func Initialize(dbPath string, dbExists bool) error {
8584
tokens = make(map[string]*Token)
8685

86+
if err := initConnection(dbPath); err != nil {
87+
return err
88+
}
89+
90+
if err := createTableIfNotExist(); err != nil {
91+
return err
92+
}
93+
94+
if !dbExists {
95+
if err := createWelcomeData(); err != nil {
96+
return err
97+
}
98+
}
99+
100+
return nil
101+
}
102+
103+
func initConnection(dbPath string) error {
87104
var err error
88-
_, errDB := os.Stat(dbPath)
89105
db, err = sql.Open("sqlite3", dbPath)
90106
if err != nil {
91107
return err
92108
}
93-
_, err = db.Exec(schema)
94-
if err != nil {
109+
return nil
110+
}
111+
112+
func createTableIfNotExist() error {
113+
if _, err := db.Exec(schema); err != nil {
95114
return err
96115
}
97-
if errDB != nil {
98-
err = CreateWelcomeData()
99-
if err != nil {
100-
panic(err)
101-
}
102-
}
116+
103117
checkBlogSettings()
104118
return nil
105119
}
@@ -110,7 +124,7 @@ func checkBlogSettings() {
110124
SetSettingIfNotExists("description", "Awesome blog created by Dingo.", "blog")
111125
}
112126

113-
func CreateWelcomeData() error {
127+
func createWelcomeData() error {
114128
var err error
115129
p := NewPost()
116130
p.Title = "Welcome to Dingo!"

cmd/install.go

-60
This file was deleted.

cmd/site.go

-3
This file was deleted.

main.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package main
22

33
import (
44
"flag"
5+
56
"github.com/dinever/dingo/app"
6-
"github.com/dinever/dingo/cmd"
77
)
88

99
func main() {
10-
if !cmd.CheckInstall() {
11-
cmd.Install()
12-
}
1310
portPtr := flag.String("port", "8000", "The port number for Dingo to listen to.")
11+
dbFilePathPtr := flag.String("database", "dingo.db", "The database file path for Djingo to use.")
1412
flag.Parse()
15-
Dingo.Init()
13+
14+
Dingo.Init(*dbFilePathPtr)
1615
Dingo.Run(*portPtr)
1716
}

0 commit comments

Comments
 (0)