summaryrefslogtreecommitdiff
path: root/main.go
blob: e39e8c50d0b6ba087e163ce14bf74e167a309569 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
	"log"
	"net/http"

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

	"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.Blog{})
}




func main() {

	session.Init()
	mux := mux.NewRouter()
	mux.Use(session.Manager.LoadAndSave)

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

	mux.HandleFunc("/blogs", handlers.CreateBlog).Methods(http.MethodGet)
	mux.HandleFunc("/blogs", handlers.StoreBlog).Methods(http.MethodPost)
	mux.HandleFunc("/blogs/{id}", handlers.GetBlog).Methods(http.MethodGet)

	mux.HandleFunc("GET /posts/{slug}", func(w http.ResponseWriter, r *http.Request) {
	    slug := r.PathValue("slug")
	    fmt.Fprintf(w, "Post: %s", slug)
	  })

	mux.HandleFunc("/login", handlers.LoginGET)
	mux.HandleFunc("/login/submit", handlers.LoginPOST)

	// Logout (POST is recommended; allow GET only if you must)
	mux.HandleFunc("/logout", handlers.LogoutPOST)

	// mux.HandleFunc("/blogs/{id}", handlers.UpdateBlog).Methods(http.MethodPut)
	// mux.HandleFunc("/blogs/{id}", handlers.DeleteBlog).Methods(http.MethodDelete)

	// Example protected route (SCS must already be on the chain)
	mux.Handle("/whoami", middleware.RequireAuth(http.HandlerFunc(handlers.Dashboard)))

	log.Println("Starting server on :8090")
	srv := &http.Server{
		Addr:    ":8090",
		Handler: mux,
	}
	log.Fatal(srv.ListenAndServe())
}