Last active
July 2, 2018 16:02
-
-
Save DazWilkin/af9268325dfdc5b62dcb806ef25b888b to your computer and use it in GitHub Desktop.
Attempt at mimicking Runtime Config using file-system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"path/filepath" | |
) | |
const ( | |
defaultRoot = "." | |
defaultPort = "8080" | |
) | |
var ( | |
env = map[string]string{ | |
"path": "SIMPLE_CONFIG_PATH", | |
"port": "SIMPLE_CONFIG_PORT", | |
} | |
root string | |
port string | |
) | |
type Config struct { | |
Variable string `json:"variable"` | |
} | |
// Constructors | |
func ConfigFromBody(body io.ReadCloser) (Config, error) { | |
config := &Config{} | |
decoder := json.NewDecoder(body) | |
err := decoder.Decode(config) | |
return *config, err | |
} | |
func ConfigFromValues(values url.Values) (Config, error) { | |
config := &Config{} | |
var err error | |
variable := values.Get("variable") | |
if variable == "" { | |
log.Println("Parameter 'variable' missing") | |
err = errors.New("Parameter 'variable' missing") | |
} else { | |
log.Printf("Variable: %s\n", variable) | |
config.Variable = variable | |
} | |
return *config, err | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
switch r.Method { | |
case "GET": | |
log.Println("GET") | |
// Expect ..?variable=[path/to/file] | |
config, err := ConfigFromValues(r.URL.Query()) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
log.Printf("variable=%s\n", config.Variable) | |
file := fmt.Sprintf("%s/%s", root, config.Variable) | |
log.Printf("Checking: %s\n", file) | |
_, err = os.Stat(file) | |
if os.IsNotExist(err) { | |
log.Println("File expected but does not exist") | |
http.Error(w, err.Error(), http.StatusNotFound) | |
return | |
} | |
log.Println("OK") | |
fmt.Fprintf(w, "OK") | |
case "POST": | |
log.Println("POST") | |
config, err := ConfigFromBody(r.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Println(config.Variable) | |
// Variable of the form "henry", "/henry" or "some/path/to/henry" | |
// Grab the Variable's path i.e. ".", "/", or "./some/path/to" | |
path := filepath.Dir(config.Variable) | |
// If needed | |
if path != "" { | |
// Create the new path by concat'ing it with the rootPath | |
fullPath := fmt.Sprintf("%s/%s", root, path) | |
log.Printf("Creating path: %s\n", fullPath) | |
err = os.MkdirAll(fullPath, os.ModePerm) | |
if err != nil { | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
} | |
// Variable still contains its path and filename | |
log.Printf("Creating: %s\n", config.Variable) | |
fullFilename := fmt.Sprintf("%s/%s", root, config.Variable) | |
_, err = os.Stat(fullFilename) | |
if err != nil { | |
if os.IsNotExist(err) { | |
log.Println("File does not exist, creating") | |
file, err := os.Create(fullFilename) | |
if err != nil { | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
defer file.Close() | |
log.Println("OK") | |
fmt.Fprintln(w, "ok") | |
} | |
if os.IsExist(err) { | |
log.Println("File already exists, abandoning") | |
http.Error(w, err.Error(), http.StatusConflict) | |
} | |
// Else | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
} | |
} | |
func init() { | |
// Check for path override in environment | |
root = os.Getenv(env["path"]) | |
if root == "" { | |
root = defaultRoot | |
} | |
log.Printf("root: %s\n", root) | |
// Check for port override in environment | |
port = os.Getenv(env["port"]) | |
if port == "" { | |
port = defaultPort | |
} | |
log.Printf("port: %s", port) | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
log.Fatal( | |
http.ListenAndServe( | |
fmt.Sprintf(":%v", port), | |
nil, | |
)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment