Base for blog

This commit is contained in:
Amarpreet Minhas 2019-04-15 18:40:50 -04:00
parent b68dba10cb
commit 04fdad2bfb
2 changed files with 65 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import (
"os"
"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/users"
"github.com/go-chi/chi"
@ -57,6 +58,7 @@ func Routes() *chi.Mux {
router.Route("/v1", func(r chi.Router) {
r.Mount("/api/auth", auth.Routes())
r.Mount("/api/blog", blog.Routes())
r.Mount("/api/users", users.Routes())
})

63
packages/blog/blog.go Normal file
View 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
}