Initial Project

This commit is contained in:
2024-01-17 15:56:44 +13:00
parent 196e17cd29
commit 1e5f136257
14 changed files with 307 additions and 244 deletions

View File

@@ -0,0 +1,34 @@
package handlers
import (
"io"
"github.com/guisea/directdnsonly/internal/util"
"github.com/labstack/echo/v4"
)
func DNSAdmin(c echo.Context) error {
// Implementation still to come
if c.Request().Method == "POST" {
action := c.QueryParam("action")
c.Logger().Debug("Action received via querystring: " + action)
body := c.Request().Body
respBytes, err := io.ReadAll(body)
if err != nil {
c.Logger().Error(err)
}
c.Logger().Debugf("Body of request: %s", respBytes)
if action == "" {
c.Logger().Debug("Action was not found, check body")
decoded_params := util.DecodeParams(string(respBytes))
c.Logger().Debugf("Parameters decoded: %s", decoded_params)
action = decoded_params["action"]
}
// zone_file := body
}
if c.Request().Method == "GET" {
// Implement some stuff
}
return nil
}

View File

@@ -0,0 +1,20 @@
package handlers
import (
"net/http"
"github.com/guisea/directdnsonly/internal/responses"
"github.com/guisea/directdnsonly/internal/util"
"github.com/labstack/echo/v4"
)
// Login - LoginTest endpoint allows DirectAdmin to check login details
// As we have set Basic Auth middleware just returns message
func LoginTest(c echo.Context) error {
// Construct a response
resp := responses.DAResponse{}
resp.Error = 0
resp.Message = "Login OK"
// Returns the response to the client
return c.String(http.StatusOK, util.EncodeQueryString(resp))
}

View File

@@ -0,0 +1,8 @@
package providers
type Provider struct {
Error int32 `url:"error"`
Exists int32 `url:"exists,omitempty"`
Message string `url:"text,omitempty"`
Details string `url:"details,omitempty"`
}

View File

@@ -0,0 +1 @@
package providers

View File

@@ -0,0 +1 @@
package providers

View File

@@ -0,0 +1,9 @@
package responses
// Definition of expected fields in response to a DirectAdmin instance
type DAResponse struct {
Error int32 `url:"error"`
Exists int32 `url:"exists,omitempty"`
Message string `url:"text,omitempty"`
Details string `url:"details,omitempty"`
}

36
internal/util/util.go Normal file
View File

@@ -0,0 +1,36 @@
package util
import (
"net/url"
"github.com/google/go-querystring/query"
"github.com/guisea/directdnsonly/internal/responses"
)
func DecodeParams(payload string) map[string]string {
// Parse the query string from the payload
values, err := url.ParseQuery(payload)
if err != nil {
// Handle error, e.g., log it or return an error value
return nil
}
// Initialize a map to store the decoded parameters
params := make(map[string]string)
// Iterate through the parameters
for key, val := range values {
// Store the first value of each parameter in the map
if len(val) > 0 {
params[key] = val[0]
}
}
// Return the decoded parameters as a map
return params
}
func EncodeQueryString(payload responses.DAResponse) string {
QueryStringResponse, _ := query.Values(payload)
return QueryStringResponse.Encode()
}

View File

@@ -0,0 +1,21 @@
package version
import (
"fmt"
"runtime"
)
// GitCommit returns the git commit that was compiled. This will be filled in by the compiler.
var GitCommit string
// Version returns the main version number that is being run at the moment.
const Version = "0.1.0"
// BuildDate returns the date the binary was built
var BuildDate = ""
// GoVersion returns the version of the go runtime used to compile the binary
var GoVersion = runtime.Version()
// OsArch returns the os and arch used to build the binary
var OsArch = fmt.Sprintf("%s %s", runtime.GOOS, runtime.GOARCH)