Mirror

Volodymyr Tkach 71550c46c9 Update README.md 2 years ago
.github 70e99a53d4 Create go.yml 2 years ago
utils 3aa2a5c9d4 Added HTTP redirect handler 2 years ago
LICENSE 103713c3df Initial commit 2 years ago
Makefile c75a16b8b9 Update Makefile 2 years ago
README.md 71550c46c9 Update README.md 2 years ago
go.mod ebb3219d5c go 1.19 2 years ago
go.sum 313f558c84 Add RollBar error reporting 2 years ago

README.md

golang-utils

Different kind of functions for second reusage

utils/http/apiserv

Web server, which can use regular expressions in path. Designed to be simple and for project with API

package server

import (
    "context"
    "fmt"
    "net/http"
    ...
)

// External package
type base.Handler struct {
    Ctx      context.Context
    DB       *database.DataBase
    Shutdown context.CancelFunc
}

func (h base.Handler) FuncMap(w http.ResponseWriter, r *http.Request) template.FuncMap {
    return template.FuncMap{}
}

func NewMux(ctx context.Context, shutdown context.CancelFunc, db *database.DataBase) *apiserv.ServeMux {
    mux := apiserv.NewServeMux()

    handler := base.Handler{
        Ctx:      ctx,
        DB:       db,
        Shutdown: shutdown,
    }

    // Pages
    mux.Get("/", page_index.Handler{Handler: handler})
    mux.Get("/about", page_about.Handler{Handler: handler})

    // API
    mux.Get("/api/v1/app/health", v1_app_health.Handler{Handler: handler})
    mux.Handle("/api/v1/aliases", []string{http.MethodGet, http.MethodPost}, v1_aliases.Handler{Handler: handler})  
    mux.Handle("/api/v1/aliases/{i}", []string{http.MethodGet, http.MethodPut, http.MethodDelete}, v1_aliases.Handler{Handler: handler})

    // Assets
    mux.Get("/favicon.png", helpers.HandleImagePng(web.FaviconPng))
    mux.Get("/robots.txt", helpers.HandleTextPlain(web.RobotsTxt))
    mux.Get("/sitemap.xml", helpers.HandleTextXml(web.SitemapXml))

    // 404
    mux.NotFound(page_404.Handler{Handler: handler})

    return mux
}

func New(ctx context.Context, shutdown context.CancelFunc, db *database.DataBase) (*http.Server, error) {
    mux := NewMux(ctx, shutdown, db)
    srv := &http.Server{
    Addr:   consts.Config.Host + ":" + consts.Config.Port,
            Handler: mux,
    }
    go func() {
        fmt.Printf("Web server: http://%s:%s/\n", consts.Config.Host, consts.Config.Port)
        if err := srv.ListenAndServe(); err != nil {
            if err != http.ErrServerClosed {
                fmt.Printf("Web server startup error: %s\n", err.Error())
                shutdown()
                return
            }
        }
    }()
    return srv, nil
}