161 lines
3.9 KiB
Go
161 lines
3.9 KiB
Go
//go:generate go run assets/generate.go
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
|
|
"git.wicak.co/arif/photlog/assets/static"
|
|
"git.wicak.co/arif/photlog/assets/template"
|
|
"git.wicak.co/arif/photlog/models"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/shurcooL/httpfs/html/vfstemplate"
|
|
)
|
|
|
|
// Env contains the app configuration
|
|
type Env struct {
|
|
dataSource string
|
|
db *sql.DB
|
|
}
|
|
|
|
func init() {
|
|
env := Env{"/data/data.sqlite3", nil}
|
|
|
|
_, err := os.Stat("./data")
|
|
if err != nil {
|
|
os.MkdirAll("./data", os.ModePerm)
|
|
|
|
env.db, err = models.InitDB("/data/data.sqlite3")
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
defer env.db.Close()
|
|
|
|
if _, err = env.db.Exec("CREATE TABLE IF NOT EXISTS data (uuid text not null primary key, title text not null, subtitle text not null, description text not null, media text not null);"); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
uuid, title, subtitle, description, media := "df1852e4-4eb2-4227-acac-e1e743bb6189", "This is title", "This is subtitle", "This is description", "test.jpg"
|
|
if _, err = env.db.Exec("CREATE TABLE IF NOT EXISTS data (uuid text not null primary key, title text not null, subtitle text not null, description text not null, media text not null);"); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
_, err = env.db.Exec("INSERT INTO data VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING;", uuid, title, subtitle, description, media)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (env *Env) indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
tmpl, err := vfstemplate.ParseFiles(template.Template, nil, "/index.html")
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
|
|
uuid := "df1852e4-4eb2-4227-acac-e1e743bb6189"
|
|
post, err := models.GetPost(env.db, uuid)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
|
|
err = tmpl.Execute(w, post)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (env *Env) postHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" {
|
|
tmpl, err := vfstemplate.ParseFiles(template.Template, nil, "/post.html")
|
|
if err != nil {
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
|
|
uuid := "df1852e4-4eb2-4227-acac-e1e743bb6189"
|
|
post, err := models.GetPost(env.db, uuid)
|
|
if err != nil {
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
|
|
err = tmpl.Execute(w, post)
|
|
if err != nil {
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
} else {
|
|
|
|
err := r.ParseMultipartForm(4 * 1024 * 1024)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
|
|
file, handler, err := r.FormFile("media")
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
fp, err := os.Create(path.Join("data", "media", handler.Filename))
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
defer fp.Close()
|
|
|
|
io.Copy(fp, file)
|
|
|
|
uuid := "df1852e4-4eb2-4227-acac-e1e743bb6189"
|
|
post := models.Post{
|
|
Title: r.FormValue("title"),
|
|
Subtitle: r.FormValue("subtitle"),
|
|
Description: r.FormValue("description"),
|
|
Media: handler.Filename,
|
|
}
|
|
|
|
err = models.UpdatePost(env.db, post, uuid)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, http.StatusText(500), 500)
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, "/", 302)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
dataSource := "data/data.sqlite3"
|
|
db, err := models.InitDB(dataSource)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
env := &Env{dataSource: dataSource, db: db}
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(static.Static)))
|
|
http.Handle("/media/", http.StripPrefix("/media/", http.FileServer(http.Dir("data/media"))))
|
|
http.HandleFunc("/", env.indexHandler)
|
|
http.HandleFunc("/post/", env.postHandler)
|
|
http.ListenAndServe(":8000", nil)
|
|
}
|