summaryrefslogtreecommitdiff
path: root/main.go
blob: dbccf982114f89a44eaf3d242c751ac0a00f67cf (plain)
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
package main

import (
	"log"
	"fmt"
	"net/http"

	"wyo.town/netzah/models"
	"wyo.town/netzah/handlers"

	"github.com/gorilla/mux"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func init() {
	dsn := "host=localhost user=_postgresql password=postGRES dbname=netzah port=5432 sslmode=disable TimeZone=US/Mountain client_encoding=UTF8"
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	db.AutoMigrate(&models.User{})
	db.AutoMigrate(&models.Blog{})
	db.AutoMigrate(&models.Comment{})
}

func main() {
	r := mux.NewRouter()

	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("Hello, World!"))
	})

	// Blog endpoints
	r.HandleFunc("/blogs", handlers.CreateBlog).Methods("GET")
	r.HandleFunc("/blogs", handlers.StoreBlog).Methods("POST")
	r.HandleFunc("/blogs/{id}", handlers.GetBlog).Methods("GET")
	// r.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods("PUT")  
	// r.HandleFunc("/blogs/{id}", handlers.DeleteBlog).Methods("DELETE") // Also consider adding to handle a form submission for deletion
	fmt.Println("Starting server on :8090")
	if err := http.ListenAndServe(":8090", r); err != nil {
		log.Fatal(err)
	}
}