-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (119 loc) · 3.12 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:generate npm run build
package main
import (
"embed"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var (
//go:embed static
StaticFS embed.FS
//go:embed tmpl
TmplFS embed.FS
DBFile = orEnv("DB_FILE", "memos.db")
Port = orEnv("PORT", "3000")
DB = initDB(DBFile)
)
type Memo struct {
ID int `json:"id" gorm:"primaryKey"`
Hero string `json:"hero" gorm:"type:blob"`
Tag string `json:"tag" form:"tag" gorm:"type:text"`
Title string `json:"title" form:"title" gorm:"type:text"`
Content string `json:"content" form:"content" gorm:"type:text"`
UpdatedAt int64 `json:"updatedAt" gorm:"autoUpdateTime"`
CreatedAt int64 `json:"createdAt" gorm:"autoCreateTime"`
Archived bool `json:"archived" gorm:"default:false"`
}
func initDB(filepath string) *gorm.DB {
db, err := gorm.Open(sqlite.Open(filepath), &gorm.Config{
DisableAutomaticPing: true,
})
if err != nil {
log.Fatal(err)
}
err = db.AutoMigrate(&Memo{})
if err != nil {
log.Fatal(err)
}
return db
}
func getMemos() ([]Memo, error) {
var memos []Memo
if err := DB.Find(&memos, "archived = ?", false).Error; err != nil {
return nil, err
}
return memos, nil
}
func getMemosFilterByTag(tag string) ([]Memo, error) {
var memos []Memo
if err := DB.Find(&memos, "tag = ?", tag).Error; err != nil {
return nil, err
}
return memos, nil
}
func getMemo(id int) (*Memo, error) {
var memo Memo
if err := DB.First(&memo, "id = ?", id).Error; err != nil {
return nil, err
}
return &memo, nil
}
func main() {
r := gin.Default()
r.SetHTMLTemplate(template.Must(template.ParseFS(TmplFS, "tmpl/*")))
staticFS, _ := fs.Sub(StaticFS, "static")
r.StaticFS("/static/", http.FS(staticFS))
r.GET("/", func(c *gin.Context) {
memos, err := getMemos()
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{"Message": err.Error()})
return
}
c.HTML(http.StatusOK, "list.html", memos)
})
r.GET("/:tag", func(c *gin.Context) {
memos, err := getMemosFilterByTag(c.Param("tag"))
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{"Message": err.Error()})
return
}
c.HTML(http.StatusOK, "list.html", memos)
})
r.POST("/memo/create", func(c *gin.Context) {
var memo Memo
if err := c.ShouldBind(&memo); err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{"Message": err.Error()})
return
}
if err := DB.Create(&memo).Error; err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{"Message": err.Error()})
return
}
c.Redirect(http.StatusFound, "/")
})
r.GET("/create.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "create.html", gin.H{})
})
r.GET("/timeline.html", func(c *gin.Context) {
memos, err := getMemos()
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{"Message": err.Error()})
return
}
c.HTML(http.StatusOK, "timeline.html", memos)
})
r.Run(":" + Port)
}
func orEnv(key, def string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return def
}