Skip to content

Commit

Permalink
Add http server using go's stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
guilycst committed May 2, 2024
1 parent a8b52c2 commit 53764e8
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ tmp
web/dist/output.css
posts.bleve
.prod.env

# Ignore .DS_Store files on all directories
**/.DS_Store
File renamed without changes.
Binary file modified blog.bleve/store/root.bolt
Binary file not shown.
4 changes: 2 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"flag"
"path/filepath"

"github.com/guilycst/guigoes/internal/handlers"
gin "github.com/guilycst/guigoes/internal/handlers/gin"
"github.com/guilycst/guigoes/internal/ports"
"github.com/guilycst/guigoes/internal/services"
"github.com/guilycst/guigoes/pkg"
Expand All @@ -24,6 +24,6 @@ func main() {
idxTmp := "/tmp/" + filepath.Base(pkg.BLEVE_IDX_PATH) + "/"
copy.Copy(pkg.BLEVE_IDX_PATH, idxTmp)
pkg.BLEVE_IDX_PATH = idxTmp
r := handlers.NewGinRouter(postsService)
r := gin.NewGinRouter(postsService)
r.Engine.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
42 changes: 42 additions & 0 deletions cmd/server_stdlib/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"flag"
"net/http"
"path/filepath"

stdhdl "github.com/guilycst/guigoes/internal/handlers/std"
"github.com/guilycst/guigoes/internal/ports"
"github.com/guilycst/guigoes/internal/services"
"github.com/guilycst/guigoes/pkg"
"github.com/otiai10/copy"
)

var postsService ports.PostService

func init() {
envfile := flag.String("envfile", ".env", "path to env file")
flag.Parse()
pkg.LoadEnvFile(*envfile)
postsService = services.NewLocalPostService()
}

func main() {
idxTmp := "/tmp/" + filepath.Base(pkg.BLEVE_IDX_PATH) + "/"
copy.Copy(pkg.BLEVE_IDX_PATH, idxTmp)
pkg.BLEVE_IDX_PATH = idxTmp

// mux := http.NewServeMux()

// // Register the routes and handlers
// mux.Handle("/", &homeHandler{})

// Run the server
http.ListenAndServe(":8080", stdhdl.NewStandardRouter(postsService))
}

type homeHandler struct{}

func (h *homeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("This is my home page"))
}
23 changes: 12 additions & 11 deletions internal/handlers/router.go → internal/handlers/gin/ginRouter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package handlers
package ginhdl

import (
"fmt"
"log"
"log/slog"
"net/mail"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (gr GinRouter) SearchPosts(c *gin.Context) {
if ref != "" {
url, err := url.Parse(ref)
if err != nil {
log.Println(err)
slog.Error("Error parsing URL:", err)
c.AbortWithError(500, err)
return
}
Expand All @@ -107,7 +108,7 @@ func (gr GinRouter) SearchPosts(c *gin.Context) {

posts, err := gr.PostSrv.Posts(nil)
if err != nil {
log.Println(err)
slog.Error("Error retrieving posts:", err)
c.AbortWithError(500, err)
return
}
Expand All @@ -124,7 +125,7 @@ func (gr GinRouter) SearchPosts(c *gin.Context) {

posts, err := gr.PostSrv.SearchPosts(search)
if err != nil {
log.Println(err)
slog.Error("Error searching posts:", err)
c.AbortWithError(500, err)
return
}
Expand Down Expand Up @@ -158,7 +159,7 @@ func (gr GinRouter) SubscribeAdd(c *gin.Context) {
log.Printf("Bad email address %s\n", err)
c.AbortWithError(400, err)
}
log.Println("SUBSCRIBED:", email, addr)
slog.Info("SUBSCRIBED:", email, addr)
templates.SubscribeOk("").Render(c.Request.Context(), c.Writer)
c.Status(200)
}
Expand All @@ -172,7 +173,7 @@ func (gr GinRouter) PostAsset(c *gin.Context) {

url, err := url.Parse(ref)
if err != nil {
log.Println(err)
slog.Error("Error parsing URL:", err)
c.AbortWithError(500, err)
return
}
Expand All @@ -182,7 +183,7 @@ func (gr GinRouter) PostAsset(c *gin.Context) {

assetPath, err := gr.PostSrv.GetPostAsset(postName, assetName)
if err != nil {
log.Println(err)
slog.Error("Error retrieving post asset:", err)
if _, ok := err.(*domain.FSResourceNotFoundError); ok {
c.AbortWithError(404, err)
return
Expand All @@ -191,7 +192,7 @@ func (gr GinRouter) PostAsset(c *gin.Context) {
return
}

log.Println("Serving asset: ", assetPath)
slog.Debug("Serving asset: ", assetPath)
c.File(assetPath)
c.Status(200)
}
Expand All @@ -201,7 +202,7 @@ func (gr GinRouter) PostAssetAbs(c *gin.Context) {
assetName := c.Param("asset")
assetPath, err := gr.PostSrv.GetPostAsset(postName, assetName)
if err != nil {
log.Println(err)
slog.Error("Error retrieving post asset:", err)
if _, ok := err.(*domain.FSResourceNotFoundError); ok {
c.AbortWithError(404, err)
return
Expand All @@ -224,7 +225,7 @@ func (gr GinRouter) Post(c *gin.Context) {
func (gr GinRouter) GetPostByName(postName string, frag bool, c *gin.Context) {
post, err := gr.PostSrv.GetPost(postName)
if err != nil {
log.Println(err)
slog.Error("Error retrieving post:", err)

if _, ok := err.(*domain.FSResourceNotFoundError); ok {
gr.NoRoute(c)
Expand Down Expand Up @@ -277,7 +278,7 @@ func (gr GinRouter) Index(c *gin.Context) {

posts, err := gr.PostSrv.Posts(nil)
if err != nil {
log.Println(err)
slog.Error("Error retrieving posts:", err)
c.AbortWithError(500, err)
return
}
Expand Down
Loading

0 comments on commit 53764e8

Please sign in to comment.