forked from truck-assistant/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoyread.go
87 lines (73 loc) · 2.4 KB
/
joyread.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
78
79
80
81
82
83
84
85
86
87
package joyread
import (
// built-in packages
"database/sql"
"fmt"
"os"
"path"
"runtime"
"strconv" // vendor packages
"github.com/gin-gonic/gin" // custom packages
"github.com/joyread/server/books"
cError "github.com/joyread/server/error"
"github.com/joyread/server/home"
"github.com/joyread/server/middleware"
"github.com/joyread/server/models"
"github.com/joyread/server/onboard"
"github.com/joyread/server/settings"
)
// StartServer handles the URL routes and starts the server
func StartServer() {
// Gin initiate
r := gin.Default()
conf := settings.GetConf()
// Serve static files
r.Static("/assets", path.Join(conf.BaseValues.AssetPath, "assets"))
// HTML rendering
r.LoadHTMLGlob(path.Join(conf.BaseValues.AssetPath, "assets/templates/*"))
// Create uploads/img directory
os.MkdirAll(path.Join(conf.BaseValues.AssetPath, "uploads/img"), os.ModePerm)
// Open postgres database
connStr := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", conf.BaseValues.DBValues.DBUsername, conf.BaseValues.DBValues.DBPassword, conf.BaseValues.DBValues.DBHostname, conf.BaseValues.DBValues.DBPort, conf.BaseValues.DBValues.DBName, conf.BaseValues.DBValues.DBSSLMode)
db, err := sql.Open("postgres", connStr)
cError.CheckError(err)
defer db.Close()
fmt.Println(runtime.GOOS)
fmt.Println(runtime.GOARCH)
if runtime.GOOS == "windows" {
fmt.Println("Hello from Windows")
}
// models.CreateLegend(db)
models.CreateAccount(db)
models.CreateBooks(db)
// models.CreateSMTP(db)
// models.CreateNextcloud(db)
r.Use(
middleware.CORSMiddleware(),
middleware.APIMiddleware(db),
middleware.UserMiddleware(db),
)
// Gin handlers
r.GET("/", home.Home)
r.GET("/uploads/:bookName", home.ServeBook)
r.GET("/cover/:coverName", home.ServeCover)
r.GET("/signin", home.Home)
r.GET("/send-file", home.SendFile)
r.POST("/signin", onboard.PostSignIn)
r.GET("/signup", onboard.GetSignUp)
r.POST("/signup", onboard.PostSignUp)
r.GET("/signout", onboard.SignOut)
r.GET("/storage", onboard.GetStorage)
r.POST("/nextcloud", onboard.PostNextcloud)
r.GET("/nextcloud-auth/:user_id", onboard.NextcloudAuthCode)
r.POST("/upload-books", books.UploadBooks)
r.GET("/book/:bookName", books.GetBook)
r.GET("/viewer/:bookName", books.Viewer)
// Listen and serve
port, err := strconv.Atoi(conf.BaseValues.ServerPort)
if err != nil {
fmt.Println("Invalid port specified")
os.Exit(1)
}
r.Run(fmt.Sprintf(":%d", port))
}