refactor
This commit is contained in:
3
client/go.mod
Normal file
3
client/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module git.dubyatp.xyz/orphanage/client
|
||||||
|
|
||||||
|
go 1.24.4
|
||||||
9
client/httputil/helloworld.go
Normal file
9
client/httputil/helloworld.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package httputil
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func HelloWorld(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("hello world"))
|
||||||
|
})
|
||||||
|
}
|
||||||
17
client/httputil/response.go
Normal file
17
client/httputil/response.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package httputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WriteJSON(w http.ResponseWriter, status int, data interface{}) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
jsonBytes, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(jsonBytes)
|
||||||
|
}
|
||||||
34
client/main.go
Normal file
34
client/main.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"git.dubyatp.xyz/orphanage/client/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
func run(ctx context.Context, w io.Writer, args []string) error {
|
||||||
|
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
config := struct {
|
||||||
|
Host string
|
||||||
|
Port string
|
||||||
|
}{
|
||||||
|
Host: "0.0.0.0",
|
||||||
|
Port: "8080",
|
||||||
|
}
|
||||||
|
|
||||||
|
return server.StartServer(ctx, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
16
client/routes/routes.go
Normal file
16
client/routes/routes.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.dubyatp.xyz/orphanage/client/httputil"
|
||||||
|
"git.dubyatp.xyz/orphanage/client/testfunc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddRoutes(
|
||||||
|
mux *http.ServeMux,
|
||||||
|
) {
|
||||||
|
mux.Handle("/", http.NotFoundHandler())
|
||||||
|
mux.Handle("/helloworld", httputil.HelloWorld(nil))
|
||||||
|
mux.Handle("/testjson", testfunc.HelloWorldJSON(nil))
|
||||||
|
}
|
||||||
49
client/server/server.go
Normal file
49
client/server/server.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.dubyatp.xyz/orphanage/client/routes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewServer() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
routes.AddRoutes(mux)
|
||||||
|
var handler http.Handler = mux
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartServer(ctx context.Context, config struct{ Host, Port string }) error {
|
||||||
|
srv := NewServer()
|
||||||
|
httpServer := &http.Server{
|
||||||
|
Addr: net.JoinHostPort(config.Host, config.Port),
|
||||||
|
Handler: srv,
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
log.Printf("orphan 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()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
37
client/testfunc/testjson.go
Normal file
37
client/testfunc/testjson.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package testfunc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.dubyatp.xyz/orphanage/client/httputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestResponse struct {
|
||||||
|
APIVersion string `json:"apiVersion"`
|
||||||
|
TestMessage string `json:"testMessage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m TestResponse) MarshalJSON() ([]byte, error) {
|
||||||
|
type OrderedTestResponse struct {
|
||||||
|
APIVersion string `json:"apiVersion"`
|
||||||
|
TestMessage string `json:"testMessage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
ordered := OrderedTestResponse{
|
||||||
|
APIVersion: m.APIVersion,
|
||||||
|
TestMessage: m.TestMessage,
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(ordered)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HelloWorldJSON(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
resp := TestResponse{
|
||||||
|
APIVersion: "v1",
|
||||||
|
TestMessage: "this is a test owo",
|
||||||
|
}
|
||||||
|
httputil.WriteJSON(w, http.StatusOK, resp)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user