Base for blog
This commit is contained in:
parent
b68dba10cb
commit
04fdad2bfb
2 changed files with 65 additions and 0 deletions
2
main.go
2
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.minhas.io/asara/sudoscientist/packages/auth"
|
"git.minhas.io/asara/sudoscientist/packages/auth"
|
||||||
|
"git.minhas.io/asara/sudoscientist/packages/blog"
|
||||||
"git.minhas.io/asara/sudoscientist/packages/database"
|
"git.minhas.io/asara/sudoscientist/packages/database"
|
||||||
"git.minhas.io/asara/sudoscientist/packages/users"
|
"git.minhas.io/asara/sudoscientist/packages/users"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
|
@ -57,6 +58,7 @@ func Routes() *chi.Mux {
|
||||||
|
|
||||||
router.Route("/v1", func(r chi.Router) {
|
router.Route("/v1", func(r chi.Router) {
|
||||||
r.Mount("/api/auth", auth.Routes())
|
r.Mount("/api/auth", auth.Routes())
|
||||||
|
r.Mount("/api/blog", blog.Routes())
|
||||||
r.Mount("/api/users", users.Routes())
|
r.Mount("/api/users", users.Routes())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
63
packages/blog/blog.go
Normal file
63
packages/blog/blog.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
package blog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
_ "fmt"
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/jwtauth"
|
||||||
|
_ "github.com/go-chi/render"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DB *sql.DB
|
||||||
|
TokenAuth *jwtauth.JWTAuth
|
||||||
|
)
|
||||||
|
|
||||||
|
type BlogPost struct {
|
||||||
|
Title string `json:"title",db:"title"`
|
||||||
|
Slug string `json:"slug",db:"slug"`
|
||||||
|
Author string `json:"author",db:"author"`
|
||||||
|
DatePublished time.Time `json:"date", db:"date"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
dbCreateStatement := `
|
||||||
|
CREATE TABLE IF NOT EXISTS blog
|
||||||
|
(id SERIAL PRIMARY KEY,
|
||||||
|
title text,
|
||||||
|
slug text,
|
||||||
|
author text REFERENCES users (username),
|
||||||
|
date timestamp)`
|
||||||
|
DB.Exec(dbCreateStatement)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Routes() *chi.Mux {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Group(func(r chi.Router) {
|
||||||
|
r.Use(jwtauth.Verifier(TokenAuth))
|
||||||
|
r.Use(jwtauth.Authenticator)
|
||||||
|
r.Post("/", createBlogPost)
|
||||||
|
r.Patch("/{slug}", updateBlogPost)
|
||||||
|
})
|
||||||
|
r.Get("/", getAllBlogPosts)
|
||||||
|
r.Get("/{slug}", getBlogPost)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAllBlogPosts(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return
|
||||||
|
}
|
Reference in a new issue