-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (112 loc) · 3.07 KB
/
main.go
File metadata and controls
126 lines (112 loc) · 3.07 KB
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
package main
import (
"blog/database"
"blog/generator"
"blog/post"
"blog/web"
"blog/web/components"
"blog/web/pages"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
func main() {
db := database.GetDatabaseConnection()
defer db.Close()
generator.SaveTemplComponent("index", pages.LandingPage())
r := gin.Default()
r.HTMLRender = &web.TemplRender{}
username := os.Getenv("GOTH_USER")
password := os.Getenv("GOTH_PASSWORD")
if username == "" {
log.Println("Environment Variable GOTH_USER not set.")
log.Println("Using default user admin:admin")
username = "admin"
password = "admin"
}
r.Use(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.Next()
return
}
if len(c.Request.URL.Path) == 1 {
http.ServeFile(c.Writer, c.Request, "static/index.html")
c.Abort()
return
}
requestedFile := "static" + c.Request.URL.Path
if _, err := os.Stat(requestedFile); err == nil {
http.ServeFile(c.Writer, c.Request, requestedFile)
c.Abort()
return
}
c.Next()
})
api := r.Group("/api")
authorizedApi := r.Group("/api", gin.BasicAuth(gin.Accounts{
username: password,
}))
authorizedApi.POST("/posts", func(c *gin.Context) {
content, _ := io.ReadAll(c.Request.Body)
filename, err := post.GeneratePost(string(content), db)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Can't generate post! %v", err))
return
}
message := fmt.Sprintf("Created new blog post %s", filename)
log.Println(message)
c.String(http.StatusOK, message)
})
authorizedApi.DELETE("/posts", func(c *gin.Context) {
id := c.Query("id")
err := post.DeletePost(id, db)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Can't delete post! %v", err))
return
}
message := fmt.Sprintf("Deleted post %s", id)
log.Println(message)
c.String(http.StatusOK, message)
})
api.GET("/posts", func(c *gin.Context) {
posts, err := post.GetPosts(db)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("Can't load posts %v", err))
}
c.HTML(http.StatusOK, "", components.PostGrid(posts))
})
authorizedApi.POST("/asset", func(c *gin.Context) {
form, _ := c.MultipartForm()
files := form.File["files"]
paths := []string{}
for _, file := range files {
path := fmt.Sprintf("assets/%s", file.Filename)
c.SaveUploadedFile(file, "static/"+path)
paths = append(paths, path)
}
message := fmt.Sprintf("Files uploaded: %v", paths)
log.Println(message)
c.String(http.StatusOK, message)
})
authorizedApi.DELETE("/asset", func(c *gin.Context) {
filename := c.Query("filename")
if filename == "" {
c.String(http.StatusBadRequest, "Can't delete asset without filename given!")
return
}
path := fmt.Sprintf("static/assets/%s", filename)
err := os.Remove(path)
if err != nil {
c.String(http.StatusNotFound, fmt.Sprintf("Asset %s not found on server!", path))
return
}
message := fmt.Sprintf("Removed asset %s from the server!", path)
log.Println(message)
c.String(http.StatusNotFound, message)
})
r.Run(":8080")
}