keel/examples/middlewares/basicauth/main.go
Kevin Franklin Kim 07f0c394d5
feat: add GoRoutine service
moves all services into the service package
2023-09-08 12:17:23 +02:00

44 lines
841 B
Go

package main
import (
"net/http"
"github.com/foomo/keel"
"github.com/foomo/keel/log"
"github.com/foomo/keel/net/http/middleware"
"github.com/foomo/keel/service"
httputils "github.com/foomo/keel/utils/net/http"
)
func main() {
svr := keel.NewServer()
// get logger
l := svr.Logger()
// create demo service
svs := http.NewServeMux()
svs.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
username := "demo"
// create password hash
passwordHash, err := httputils.HashBasicAuthPassword([]byte("demo"))
log.Must(l, err, "failed to hash password")
svr.AddService(
service.NewHTTP(l, "demo", "localhost:8080", svs,
middleware.BasicAuth(
username,
passwordHash,
middleware.BasicAuthWithRealm("demo"),
),
),
)
svr.Run()
}