Skip to content

Commit

Permalink
adds http server
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhang93 committed May 10, 2024
1 parent 75144cf commit cfb57a7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
1 change: 0 additions & 1 deletion cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func spawnAndReload(rootCtx context.Context, processMaker func(logger *slog.Logg
signal.Notify(sighup, syscall.SIGHUP)

spawnErrChan := make(chan error, 1)

go func() {
err := spawn(ctx, processMaker, conf, false)
spawnErrChan <- err
Expand Down
6 changes: 3 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type TPLAgent struct {
func ReadFromFile(path string) (TPLAgent, error) {
confFile, err := os.Open(os.ExpandEnv(path))
if err != nil {
return TPLAgent{}, fatal.NewError(fmt.Errorf("read config:%w", err))
return TPLAgent{}, fatal.NewError(fmt.Errorf("Read config:%w", err))
}
return read(confFile)
return Read(confFile)
}

func read(rr io.Reader) (TPLAgent, error) {
func Read(rr io.Reader) (TPLAgent, error) {
var c TPLAgent
if err := json.NewDecoder(rr).Decode(&c); err != nil {
return TPLAgent{}, fatal.NewError(fmt.Errorf("config decode error:%w", err))
Expand Down
12 changes: 6 additions & 6 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *sampleActions) SetConfig(bb []byte) error {
}

func Test_readConfig(t *testing.T) {
t.Run("read config test", func(t *testing.T) {
t.Run("Read config test", func(t *testing.T) {
configJSON := `{
"agent": {
"log_level": "ERROR",
Expand Down Expand Up @@ -117,7 +117,7 @@ func Test_readConfig(t *testing.T) {
},
},
}
c, err := read(strings.NewReader(configJSON))
c, err := Read(strings.NewReader(configJSON))
if err != nil {
t.Errorf("error reading config:%v", err)
return
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_readConfig(t *testing.T) {

})

t.Run("test sample provider config read", func(t *testing.T) {
t.Run("test sample provider config Read", func(t *testing.T) {
configJSON := `{
"agent": {
"log_level": "ERROR",
Expand All @@ -180,7 +180,7 @@ func Test_readConfig(t *testing.T) {
}
}
`
conf, err := read(strings.NewReader(configJSON))
conf, err := Read(strings.NewReader(configJSON))
if err != nil {
t.Errorf("error reading config:%v\n", err)
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func Test_config_fatalErrors(t *testing.T) {
buff.Reset()
buff.Write(jsonCfg)

_, err = read(&buff)
_, err = Read(&buff)
if !fatal.Is(err) {
t.Error("expected fatal error")
return
Expand Down Expand Up @@ -257,7 +257,7 @@ func Test_config_fatalErrors(t *testing.T) {
return
}

_, err = read(&buff)
_, err = Read(&buff)
if !fatal.Is(err) {
t.Error("expected fatal error")
}
Expand Down
66 changes: 66 additions & 0 deletions internal/httplis/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package httplis

import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/shubhang93/tplagent/internal/config"
"log/slog"
"net/http"
"os"
)

type reloadRequest struct {
Config config.TPLAgent `json:"config"`
ConfigPath string `json:"config_path"`
}

func Start(ctx context.Context, addr string, l *slog.Logger) {
mux := http.NewServeMux()
mux.HandleFunc("POST /config/reload", func(writer http.ResponseWriter, request *http.Request) {
reloadReq := reloadRequest{}
err := json.NewDecoder(request.Body).Decode(&reloadReq)
if err != nil {
writeJSON(writer, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}

_, err = os.Stat(reloadReq.ConfigPath)
if errors.Is(err, os.ErrNotExist) {
writeJSON(writer, http.StatusInternalServerError, map[string]string{
"error": fmt.Sprintf("file not found at %s", reloadReq.ConfigPath),
})
return
}

err = config.Validate(&reloadReq.Config)
if err != nil {
writeJSON(writer, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}

})

s := http.Server{Handler: mux, Addr: addr}

wait := make(chan struct{})
go func() {
defer close(wait)
_ = s.Shutdown(ctx)
}()

err := s.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) && err != nil {
l.Error("ListenAndServe error", slog.String("error", err.Error()))
}

<-wait

}

func writeJSON(writer http.ResponseWriter, status int, data any) {
writer.WriteHeader(status)
_ = json.NewEncoder(writer).Encode(data)
return
}

0 comments on commit cfb57a7

Please sign in to comment.