This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (60 loc) · 1.35 KB
/
Copy pathmain.go
File metadata and controls
77 lines (60 loc) · 1.35 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
package main
import (
"fmt"
"html/template"
"net/http"
"database/sql"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
_ "github.com/mattn/go-sqlite3"
)
var router *chi.Mux
var db *sql.DB
type BlogPost struct {
ID int `json:"id"`
Slug string `json:"slug"`
Title string `json:"title"`
Content template.HTML `json:"content"`
}
func NewPost(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/base.go.html", "templates/new.go.html")
err := t.Execute(w, nil)
catch(err)
}
func init() {
router = chi.NewRouter()
router.Use(middleware.Recoverer)
var err error
db, err = connect()
catch(err)
}
func main() {
// AL_BLOG_PASS := os.Getenv("AL_BLOG_PASS")
// fmt.Println("Blog pass:" + AL_BLOG_PASS)
router = chi.NewRouter()
router.Use(middleware.Recoverer)
var err error
_, err = connect()
catch(err)
router.Use(ChangeMethod)
router.Get("/", GetAllPosts)
router.Route("/new", func(r chi.Router) {
r.Get("/", NewPost)
r.Post("/", CreatePost)
})
router.Route("/{postSlug}", func(r chi.Router) {
r.Use(PostCtx)
r.Get("/", GetPost)
// r.Put("/", UpdatePost)
// r.Delete("/", DeletePost)
// r.Get("/edit", EditPost)
})
err = http.ListenAndServe(":8080", router)
catch(err)
}
func catch(err error) {
if err != nil {
fmt.Println(err)
panic(err)
}
}