phoenix/views/routes.go
Ivan R. 6d25c4e8af
feat!: migrate to net/http
With the release of Go 1.22, the standard library now has
all the necessary functions that allow us to abandon Gin.

I hope this rewrite will lower the entry barrier for new developers.
As a nice bonus, the size of the program has decreased from 20 to 15.4 MB.

To solve issue #81, request logging has been improved.
Now all errors are displayed in the logs.
2024-03-25 15:52:18 +05:00

63 lines
1.6 KiB
Go

package views
import (
"net/http"
"github.com/ordinary-dev/phoenix/views/middleware"
"github.com/ordinary-dev/phoenix/views/pages"
)
// Create and configure an HTTP server.
//
// Unfortunately, I haven't found a way to use PUT and DELETE methods without JavaScript.
// POST is used instead.
func GetHttpServer() (*http.Server, error) {
if err := pages.LoadTemplates(); err != nil {
return nil, err
}
mux := http.NewServeMux()
mux.Handle("/assets/", http.StripPrefix(
"/assets",
http.FileServer(http.Dir("assets")),
))
mux.HandleFunc("GET /signin", pages.ShowSignInForm)
mux.HandleFunc("POST /signin", pages.AuthorizeUser)
mux.HandleFunc("GET /registration", pages.ShowRegistrationForm)
mux.HandleFunc("POST /registration", pages.CreateUser)
protectedMux := http.NewServeMux()
mux.Handle("/", middleware.RequireAuth(protectedMux))
protectedMux.HandleFunc("GET /", pages.ShowMainPage)
protectedMux.HandleFunc("GET /settings", pages.ShowSettings)
// Groups.
// Create group.
protectedMux.HandleFunc("POST /groups", pages.CreateGroup)
// Update group.
protectedMux.HandleFunc("POST /groups/{id}/update", pages.UpdateGroup)
// Delete group.
protectedMux.HandleFunc("POST /groups/{id}/delete", pages.DeleteGroup)
// Links.
// Create link.
protectedMux.HandleFunc("POST /links", pages.CreateLink)
// Update link.
protectedMux.HandleFunc("POST /links/{id}/update", pages.UpdateLink)
// Delete link.
protectedMux.HandleFunc("POST /links/{id}/delete", pages.DeleteLink)
return &http.Server{
Addr: ":8080",
Handler: middleware.LoggingMiddleware(
middleware.SecurityHeadersMiddleware(mux),
),
}, nil
}