-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (69 loc) · 1.64 KB
/
main.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
package main
import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"paperfinderv3/paperloader"
"paperfinderv3/server"
"path"
"strconv"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func init() {
err := godotenv.Load()
if err != nil {
fmt.Println("Cannot find .env file")
os.Exit(1)
}
_, err = strconv.Atoi(os.Getenv("SEARCH_THRESHOLD"))
if err != nil {
fmt.Println("Invalid search threshold")
os.Exit(1)
}
}
func main() {
papers := flag.Bool("papers", false, "If set will redownload papers")
index := flag.Bool("index", false, "If set will regenerate index")
flag.Parse()
if *papers {
paperloader.LoadPapers()
}
if *index {
paperloader.GenerateIndex()
}
gin.SetMode(gin.ReleaseMode)
fmt.Println("Starting server...")
r := gin.New()
r.SetFuncMap(template.FuncMap{
"title": cases.Title(language.English).String,
"len": func(a []server.SearchResult) int {
return len(a)
},
"mangle": server.MangleFilename,
"img": func(in string) string {
ext := path.Ext(in)
return in[0:len(in)-len(ext)] + ".jpg"
},
"trim": func(in string) string {
if len(in) <= 3 {
return in
}
return in[0:3]
},
})
r.LoadHTMLGlob("web/*_template.html")
r.GET("/", server.GenerateIndex)
r.GET("/search", server.GenerateSearchResults)
r.GET("/file/:filename", server.GetPaper)
r.GET("/image/:filename", server.GetImage)
r.NoRoute(server.NotFound)
r.StaticFile("/css/index.css", "web/css/index.css")
r.StaticFile("/css/search.css", "web/css/search.css")
r.StaticFS("/assets", http.Dir("web/assets"))
r.Run("0.0.0.0:" + os.Getenv("HTTP_PORT"))
}