Barebones HTTP server setup
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
tmp/
|
||||
projectreshoot
|
||||
19
handlers/root.go
Normal file
19
handlers/root.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func HandleRoot() http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
templ := template.Must(template.ParseFiles("templates/index.html"))
|
||||
templ.Execute(w, nil)
|
||||
},
|
||||
)
|
||||
}
|
||||
60
main.go
Normal file
60
main.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func run(ctx context.Context, w io.Writer, args []string) error {
|
||||
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
config := &Config{
|
||||
Host: "",
|
||||
Port: "3333",
|
||||
}
|
||||
|
||||
srv := NewServer(config)
|
||||
httpServer := &http.Server{
|
||||
Addr: net.JoinHostPort(config.Host, config.Port),
|
||||
Handler: srv,
|
||||
}
|
||||
|
||||
go func() {
|
||||
fmt.Fprintf(w, "Listening on %s\n", httpServer.Addr)
|
||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
fmt.Fprintf(os.Stderr, "Error listening and serving: %s\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-ctx.Done()
|
||||
shutdownCtx := context.Background()
|
||||
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
||||
defer cancel()
|
||||
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error shutting down http server: %s\n", err)
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
fmt.Fprintln(w, "Shutting down")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
if err := run(ctx, os.Stdout, os.Args); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
29
middleware/logging.go
Normal file
29
middleware/logging.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type wrappedWriter struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
}
|
||||
|
||||
func (w *wrappedWriter) WriteHeader(statusCode int) {
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
w.statusCode = statusCode
|
||||
}
|
||||
|
||||
func Logging(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
wrapped := &wrappedWriter{
|
||||
ResponseWriter: w,
|
||||
statusCode: http.StatusOK,
|
||||
}
|
||||
next.ServeHTTP(wrapped, r)
|
||||
log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
|
||||
})
|
||||
}
|
||||
13
routes.go
Normal file
13
routes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"projectreshoot/handlers"
|
||||
)
|
||||
|
||||
func addRoutes(
|
||||
mux *http.ServeMux,
|
||||
config Config,
|
||||
) {
|
||||
mux.Handle("GET /", handlers.HandleRoot())
|
||||
}
|
||||
24
server.go
Normal file
24
server.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"projectreshoot/middleware"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
|
||||
func NewServer(
|
||||
config *Config,
|
||||
) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
mux,
|
||||
*config,
|
||||
)
|
||||
var handler http.Handler = mux
|
||||
handler = middleware.Logging(handler)
|
||||
return handler
|
||||
}
|
||||
7
templates/index.html
Normal file
7
templates/index.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<!doctype html>
|
||||
<head>
|
||||
<title>My site!!</title>
|
||||
</head>
|
||||
<body>
|
||||
It's been far too long since i wrote raw HTML and honestly I kind of miss it
|
||||
</body>
|
||||
Reference in New Issue
Block a user