I have a yaml configuration file that contains sets of configuration commands to send to network devices. Inside of each set, there are vendor-specific keys and the values for each vendor key can be either a configuration command string, a list of configuration command strings, or a list of key-value pairs mapping a vendor-specific model string to a configuration command string. Below is an example:
# example.yml
---
cmds:
setup:
cisco: "terminal length 0"
config:
cisco:
- basic : "show version"
- basic : "show boot"
"3560" : "3560 boot command"
"2960x": "2960x boot command"
- basic : "dir flash:"
"3560" : "3560 dir command"
cleanup:
cisco: ["terminal no length", "quit"]
I want to combine these commands into a map like so:
var cmdMap = map[string][]string{
"cisco": []string{
"terminal length 0",
"show version",
"show boot",
"dir flash:",
"terminal no length",
"quit",
},
"cisco.3560": []string{
"terminal length 0",
"show version",
"3560 boot command",
"3560 dir command",
"terminal no length",
"quit",
},
"cisco.2960x": []string{
"terminal length 0",
"show version",
"2960x boot command",
"dir flash:",
"terminal no length",
"quit",
}
}
I am using spf13/viper to handle parsing the yaml file and have been able to add the specific commands to each vendor and model, but adding the commands that apply to both vendor and specific model is where I am stuck. This is the actual output of my program followed by my code:
$ go run main.go example.yml
cmdMap["cisco"]
terminal length 0
show version
show boot
dir flash:
terminal no length
quit
# missing terminal length 0, show version, etc.
cmdMap["cisco.3560"]
3560 boot command
3560 dir command
# missing terminal length 0, show version, etc.
cmdMap["cisco.2960x"]
2960x boot command
My code:
package main
import (
"github.com/spf13/viper"
"fmt"
"flag"
"log"
)
func main() {
flag.Parse()
cfgFile := flag.Arg(0)
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile(cfgFile)
if err := v.ReadInConfig(); err != nil {
log.Fatal(err)
}
for k, v := range MapCfgCmds(v.GetStringMap("cmds")) {
fmt.Printf("cmdMap[\"%s\"]\n", k)
for _, cmd := range v {
fmt.Println(cmd)
}
fmt.Println()
}
}
func MapCfgCmds(cfgCmds map[string]interface{}) map[string][]string {
cmdMap := make(map[string][]string)
for _, cmdSet := range cfgCmds {
cmdSet, _ := cmdSet.(map[string]interface{})
for vendor, cmdList := range cmdSet {
switch cmds := cmdList.(type) {
case string:
// single string command (i.e., vendor: cmd)
cmdMap[vendor] = append(cmdMap[vendor], cmds)
case []interface{}:
for _, cmd := range cmds {
switch c := cmd.(type) {
case string:
// list of strings (i.e., vendor: [cmd1,cmd2,...,cmdN])
cmdMap[vendor] = append(cmdMap[vendor], c)
case map[interface{}]interface{}:
// This is where I am stuck
//
// list of key-value pairs (i.e., vendor: {model: modelCmd})
for model, modelCmd := range c {
modelCmd, _ := modelCmd.(string)
if model == "basic" {
cmdMap[vendor] = append(cmdMap[vendor], modelCmd)
continue
}
modelKey := fmt.Sprintf("%s.%s", vendor, model)
cmdMap[modelKey] = append(cmdMap[modelKey], modelCmd)
}
}
}
}
}
}
return cmdMap
}
How can I combine the "universal" and model-specific commands to get the expected cmdMap value from above?
I think viper is not helping you here, in the sense that viper does a lot of things you don't need, but it doesn't do one thing you could use here - clear mapping of the data. If you use the yaml library directly, you could declare a structure that corresponds to your data and makes it easier to understand it.
There are several possible approaches to your problem, here is my attempt at solving it (you might need to tweak few things as I wrote it in the editor, without compiling it):
type Data struct {
Cmds struct {
Setup map[string]interface{} `yaml:"setup"`
Config map[string][]map[string]string `yaml:"config"`
Cleanup map[string][]string `yaml:"cleanup"`
} `yaml:"cmds"`
}
data := Data{}
err := yaml.Unmarshal([]byte(input), &data)
if err != nil {
log.Fatalf("error: %v", err)
}
setupCmds := make(map[string][]string)
cleanupCmds := make(map[string][]string)
result := make(map[string][]string)
// Prepare setup commands, grouped by vendor
for vendor, setupCmd := range data.Cmds.Setup {
setupCmds[vendor] = append(setupCmds[vendor], setupCmd)
}
// Prepare cleanup commands, grouped by vendor
for vendor, commands := range data.Cmds.Cleanup {
cleanupCmds[vendor] = append(cleanupCmds[vendor], commands...)
}
// iterate over vendors and models, combine with setup & cleanup commands and store in result
for vendor, configCmds := range data.Cmds.Config { // vendor = string (e.g. "cisco"), configCmds = []map[string][string] (e.g. - basic: "show version")
// we now how many config commands there will be
result[vendor] = make([]string, len(configCmds))
// variantsCache will store all variants we've seen so far
variantsCache := make(map[string]struct{})
for i, model := range models { // i = int (number of command) model = map[string]string
// we assume "basic" is available for each command
result[vendor][i] = model["basic"]
for variant, command := range model { // variant = string (e.g. "basic"), command = string (e.g. "show version")
if variant == "basic" {
// we already covered that
continue
}
variantKey := vendor + "." + variant
variantsCache[variantKey]
if _, ok := result[variantKey]; !ok {
// first command for this model, create a slice
result[variantKey] = make([]string, len(configCmds))
}
result[variantKey][i] = command
}
}
// We need to iterate over all commands for all variants and copy "basic" command if there is none
for variant, _ := range variantsCache {
for i, command := range result[variant] {
if command == "" {
// copy the "basic" command, since there was no variant specific command
result[variant][i] = result[vendor][i]
}
}
}
}
// combine setup and cleanup with config
for variant, _ := result {
// will return "cisco" for both "cisco" and "cisco.x3650"
vendor := strings.Split(variant, ".")[0]
result[variant] = append(setupCmds[vendor], result[variant]...)
result[variant] = append(result[variant], cleanupCmds[vendor]...)
}
return result
You can combine them after the loop of constructing cmdMap.
for vendor := range cmdMap {
// get the base name of the vendor
s := strings.SplitN(vender, ".", 2)
// if vendor is a basic one, skip it.
if len(s) == 1 {
continue
}
// add basic cmd into the specified ones.
base = s[0]
cmdMap[vendor] = append(cmdMap[vendor], cmdMap[base]...)
}
Note that cmdMap[base]... is a use of variadic parmaters of append. You can see more here: https://golang.org/ref/spec#Passing_arguments_to_..._parameters
Related
I am interested to see if anyone knows of any better alternative to using conditional count statements in Terraform. By "conditional count statement", I mean a statement where depending a condition like a variable input, count will evaluate to create either 0 or 1 of a resource.
A simple example:
resource "xxxxxxxx" "example" {
count = var.example != null ? 1 : 0
}
Here, the resource will only be created if var.example has a value (is not null), otherwise it will not get created.
Conditional counts usually work ok in practice, but sometimes in more complex uses than the one above it introduces a risk of getting an error during Terraform Plan where it cannot evaluate the result of the count pre-apply.
The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.
Is there any better way to achieve the same effect of creating resources on a conditional basis in Terraform?
Keeping in mind that your terraform config not shown I've used a general example of for_each instead of count.
This is an example of how 2 CNAME records will be created using & in this example terraform modules are used but the same can be done directly on terraform resources.
locals {
cname_records = {
"email" = ["email.domain.net."]
"imap" = ["imap.domain.net."]
}
}
module "aws_route53_record_CNAME" {
source = "app.terraform.io/terraform-cloud-org/route53-record/aws"
version = "1.0.0"
for_each = local.cname_records
records = each.value
name = each.key
zone_id = "YOUR-HOSTED-ZONE-ID"
type = "CNAME"
ttl = "300"
}
It varies by case, but there are some cases where you need to get around this. There is a clever trick, but it seems obtuse in the single item (binary exists or not) case. But in a case where there are actually multiple items, this should help.
This is a completely contrived example but I actually use the method quite a bit. In short, a list of things not known until apply, can be replaced by a list of objects with a key you don't care about. The purpose is that for_each doesn't mind if the value is unknown at plan-time, only if the key is.
Consider the following root module with these four modules.
main.tf
resource "aws_s3_bucket" "this" {
bucket = "h4s-test-bucket"
}
# module "with_count" { # cannot be determined until apply
# source = "./with-count"
# x = aws_s3_bucket.this.id
# }
module "with_for_each_over_item" { # handy workaround
source = "./with-for-each-over-item"
x = aws_s3_bucket.this.id
}
output "with_for_each_over_item" {
value = module.with_for_each_over_item
}
# module "with_for_each_list" { # cannot be determined until apply
# source = "./with-for-each-list"
# x = [aws_s3_bucket.this.id]
# }
module "with_for_each_list_better" { # handy workaround
source = "./with-for-each-list-better"
x = [{ y = aws_s3_bucket.this.id }]
}
output "with_for_each_list_better" {
value = module.with_for_each_list_better
}
module "with_for_each_list_best" { # handier workaround
source = "./with-for-each-list-best"
x = [aws_s3_bucket.this.id]
}
output "with_for_each_list_best" {
value = module.with_for_each_list_best
}
with-count/main.tf (problematic)
variable "x" {
type = string
default = null
}
resource "null_resource" "this" {
count = var.x != null ? 1 : 0
}
output "this" {
value = null_resource.this
}
with-for-each-over-item/main.tf (handy workaround)
variable "x" {
type = string
default = null
}
resource "null_resource" "this" {
for_each = { for i, v in [var.x] : i => v }
}
output "this" {
value = null_resource.this
}
with-for-each-list/main.tf (problematic)
variable "x" {
type = list(string)
default = []
}
resource "null_resource" "this" {
for_each = toset(var.x)
}
output "this" {
value = null_resource.this
}
with-for-each-list-better/main.tf (handy workaround)
variable "x" {
type = list(object({ y = string }))
default = []
}
resource "null_resource" "this" {
for_each = { for i, v in var.x : i => v }
}
output "this" {
value = null_resource.this
}
with-for-each-list-best/main.tf (handiest workaround)
variable "x" {
type = list(string)
default = []
}
resource "null_resource" "this" {
for_each = { for i, v in var.x : i => v }
}
output "this" {
value = null_resource.this
}
Summary
In cases where the variable has a value not known at plan-time, consider using an object where the key is known.
I have a recursive function which iterates though directory trees listing the file names located in them.
Here is the function:
void WINAPI SearchFile(PSTR Directory)
{
HANDLE hFind;
WIN32_FIND_DATA FindData;
char SearchName[1024],FullPath[1024];
memset(SearchName,0,sizeof(SearchName));
memset(&FindData,0,sizeof(WIN32_FIND_DATA));
sprintf(SearchName,"%s\\*",Directory);
hFind=FindFirstFile(SearchName,&FindData);
if(hFind!=INVALID_HANDLE_VALUE)
{
while(FindNextFile(hFind,&FindData))
{
if(FindData.cFileName[0]=='.')
{
continue;
}
memset(FullPath,0,sizeof(FullPath));
sprintf(FullPath,"%s\\%s",Directory,FindData.cFileName);
if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
MessageBoxA(NULL, FullPath, "Directory", MB_OK);
SearchFile(FullPath);
}
else
{
MessageBoxA(NULL, FullPath, "File", MB_OK);
}
}
FindClose(hFind);
}
}
There are obviously differences between both functions but I don't understand what's making them act differently. Does anyone know why I am having this problem?
for fast understand error need look for line
goto label;
//SearchFile(FullPath);
at this point hFind containing valid data and FindClose(hFind); need be called for it. but after goto label; executed - your overwrite hFind with hFind = FindFirstFile(SearchName, &FindData); - so you already never close original hFind, never can return to iterate folder after such go to sub-folder. this is key point - need save original hFind before go to sub directory and restore it after. when you do recursive function call - this is done auto - because every sub directory in this case enumerated in self stack frame, which have separate hFind. this is native solution use recursion here.
but possible convert recursion to loop here because we call self always from the single place and as result to this single place. so we can not save return address in stack but do unconditional jump (goto) to known place.
then code have some extra errors, you never check for string buffers overflow, why 1024 as max length is hard-coded when file path can be up to 32768 chars, you not check for reparse point as result can enter to infinite loop, use FindFirstFile instead FindFirstFileEx, etc.
correct code for enumerate sub-folder in loop can be next
void DoEnum(PCWSTR pcszRoot)
{
SIZE_T FileNameLength = wcslen(pcszRoot);
// initial check for . and ..
switch (FileNameLength)
{
case 2:
if (pcszRoot[1] != '.') break;
case 1:
if (pcszRoot[0] == '.') return;
}
static const WCHAR mask[] = L"\\*";
WCHAR FileName[MAXSHORT + 1];
if (_countof(FileName) < FileNameLength + _countof(mask))
{
return;
}
ULONG dwError;
HANDLE hFindFile = 0;
WIN32_FIND_DATA FindData{};
enum { MaxDeep = 0x200 };
//++ stack
HANDLE hFindFileV[MaxDeep];
PWSTR pszV[MaxDeep];
char prefix[MaxDeep+1];
//--stack
ULONG Level = MaxDeep;
memset(prefix, '\t', MaxDeep);
prefix[MaxDeep] = 0;
PWSTR psz = FileName;
goto __enter;
__loop:
hFindFile = FindFirstFileEx(FileName, FindExInfoBasic, &FindData, FindExSearchNameMatch, 0, FIND_FIRST_EX_LARGE_FETCH);
if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
pcszRoot = FindData.cFileName;
// skip . and ..
switch (FileNameLength = wcslen(pcszRoot))
{
case 2:
if (pcszRoot[1] != '.') break;
case 1:
if (pcszRoot[0] == '.') continue;
}
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((SIZE_T)(FileName + _countof(FileName) - psz) < FileNameLength + _countof(mask))
{
continue;
}
__enter:
memcpy(psz, pcszRoot, (1 + FileNameLength) * sizeof(WCHAR));
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
DbgPrint("%sreparse point: <%S>\n", prefix + Level, pcszRoot);
}
else
{
if (Level)
{
DbgPrint("%s<%S>\n", prefix + Level, psz);
hFindFileV[--Level] = hFindFile;
pszV[Level] = psz;
memcpy(psz += FileNameLength, mask, sizeof(mask));
psz++;
goto __loop;
__return:
*--psz = 0;
psz = pszV[Level];
hFindFile = hFindFileV[Level++];
DbgPrint("%s</%S>\n", prefix + Level, psz);
}
}
}
else
{
DbgPrint("%s[%u%u] %S\n", prefix + Level, FindData.nFileSizeLow, FindData.nFileSizeHigh, pcszRoot);
}
if (!hFindFile)
{
// top level exit
return ;
}
} while (FindNextFile(hFindFile, &FindData));
if ((dwError = GetLastError()) == ERROR_NO_MORE_FILES)
{
dwError = NOERROR;
}
FindClose(hFindFile);
}
else
{
dwError = GetLastError();
}
if (dwError)
{
DbgPrint("<%S> err = %u\n", FileName, dwError);
}
goto __return;
}
The reason for the difference is actually the confusion brought to you by goto label.If you are using the recursive version, after the recursive execution is completed, it will return to the recursive place to continue execution.
In your code, you continue to execute while (FindNextFile(hFind, &FindData)), but when you use goto label, it will jump out of the original loop and restart the program from the label, which leads to what you said list a single directory tree before ending.
If you modify the modified code to the following iterative version, you can understand why there is such a problem.
void fun()
{
char* Directory = "D:\\test";
HANDLE hFind;
WIN32_FIND_DATA FindData;
char SearchName[1024], FullPath[1024];
char LastName[1024] = "";
while (1)
{
memset(SearchName, 0, sizeof(SearchName));
memset(&FindData, 0, sizeof(WIN32_FIND_DATA));
sprintf(SearchName, "%s\\*", Directory);
if (strcmp(SearchName, LastName) == 0)
{
return;
}
strcpy(LastName, SearchName);
hFind = FindFirstFile(SearchName, &FindData);
if (hFind != INVALID_HANDLE_VALUE)
{
while (FindNextFile(hFind, &FindData))
{
if (FindData.cFileName[0] == '.')
{
continue;
}
memset(FullPath, 0, sizeof(FullPath));
sprintf(FullPath, "%s\\%s", Directory, FindData.cFileName);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
MessageBoxA(NULL, Directory, "Directory", MB_OK);
char cArray[1024];
memset(cArray, 0, sizeof(cArray));
sprintf(cArray, "%s", FullPath);
Directory = cArray;
break;
}
else
{
MessageBoxA(NULL, FullPath, "File", MB_OK);
}
}
FindClose(hFind);
}
}
}
So you cannot achieve the same purpose as recursion by using goto, here you can only use recursion. Of course, I have provided a way to traverse directories non-recursively using queues, which is a more scientific way.
One of the key things that you obtain from recursion is a separate set of local variables for each call to the recursive function. When a function calls itself, and in the recursive call modifies local variables, those local-variable changes do not (directly) affect the local variables of the caller. In your original program, this applies to variables hFind, FindData, SearchName, and FullPath.
If you want similar behavior in a non-recursive version of the function then you need to manually preserve the state of your traversal of one level of the tree when you descend to another level. The goto statement doesn't do any such thing -- it just redirects the control flow of your program. Although there are a few good use cases for goto in C, they are uncommon, and yours is not one of them.
There are several ways to implement manually preserving state, but I would suggest
creating a structure type in which to store those data that characterize the state of your traversal of a particular level. Those appear to be only hFind and FindData -- it looks like the other locals don't need to be preserved. Maybe something like this, then:
struct dir_state {
HANDLE hFind;
WIN32_FIND_DATA FindData;
};
Dynamically allocating an array of structures of that type.
unsigned depth_limit = DEFAULT_DEPTH_LIMIT;
struct dir_state *traversal_states
= malloc(depth_limit * sizeof(*traversal_states));
if (traversal_states == NULL) // ... handle allocation error ...
Tracking the depth of your tree traversal, and for each directory you process, using the array element whose index is the relative depth of that directory.
// For example:
traversal_states[depth].hFind
= FindFirstFile(SearchName, &traversal_states[depth].FindData);
// etc.
Remembering the size of the array, so as to be able to reallocate it larger if the traversal descends too deep for its current size.
// For example:
if (depth >= depth_limit) {
depth_limit = depth_limit * 3 / 2;
struct dir_state *temp
= realloc(traversal_states, depth_limit * sizeof(*traversal_states));
if (temp == NULL) {
// handle error, discontinuing traversal
}
traversal_states = temp;
}
Also, use an ordinary for, while, or do loop instead of a backward-jumping goto. There will be a few details to work out to track when to use FindFirstFile and when FindNextFile (which you would still have with goto), but I'm sure you can sort it out.
Details are left as an exercise.
Unless necessary due to memory or processing constraints or infinite recursion tail conditions that would be complication to introduce there really isn't much need to not use recursion here, since it leads to a readable and elegant solution.
I also want to point out that in "modern" C, any solution using a GOTO is likely not a solution you want since they are so often confusing to use and leads to memory issues (we have loops now to make all of that so much simpler).
Instead of the GOTOs I would suggest implementing a stack of the directories. Wrap the printing logic a while or do-while, and as you are iterating over the files add any directories to the stack. At every new iteration pop and walk the directory at the head of the stack. The loop condition just needs to check if the directory stack is empty, before continuing its block.
I am reading a tutorial here: http://www.newthinktank.com/2015/02/go-programming-tutorial/
On the "Maps in Maps" section it has:
package main
import "fmt"
func main() {
// We can store multiple items in a map as well
superhero := map[string]map[string]string{
"Superman": map[string]string{
"realname":"Clark Kent",
"city":"Metropolis",
},
"Batman": map[string]string{
"realname":"Bruce Wayne",
"city":"Gotham City",
},
}
// We can output data where the key matches Superman
if temp, hero := superhero["Superman"]; hero {
fmt.Println(temp["realname"], temp["city"])
}
}
I don't understand the "if" statement. Can someone walk me through the syntax on this line:
if temp, hero := superhero["Superman"]; hero {
Like if temp seems nonsensical to an outsider as temp isn't even defined anywhere. What would that even accomplish? Then hero := superhero["Superman"] looks like an assignment. But what is the semicolon doing? why is the final hero there?
Can someone help a newbie out?
Many thanks.
A two-value assignment tests for the existence of a key:
i, ok := m["route"]
In this statement, the first value (i) is assigned the value stored
under the key "route". If that key doesn't exist, i is the value
type's zero value (0). The second value (ok) is a bool that is true if
the key exists in the map, and false if not.
This check is basically used when we are not confirmed about the data inside the map. So we just check for a particular key and if it exists we assign the value to variable. It is a O(1) check.
In your example try to search for a key inside the map which does not exists as:
package main
import "fmt"
func main() {
// We can store multiple items in a map as well
superhero := map[string]map[string]string{
"Superman": map[string]string{
"realname": "Clark Kent",
"city": "Metropolis",
},
"Batman": map[string]string{
"realname": "Bruce Wayne",
"city": "Gotham City",
},
}
// We can output data where the key matches Superman
if temp, hero := superhero["Superman"]; hero {
fmt.Println(temp["realname"], temp["city"])
}
// try to search for a key which doesnot exist
if value, ok := superhero["Hulk"]; ok {
fmt.Println(value)
} else {
fmt.Println("key not found")
}
}
Playground Example
if temp, hero := superhero["Superman"]; hero
in go is similar to writing:
temp, hero := superhero["Superman"]
if hero {
....
}
Here is "Superman" is mapped to a value, hero will be true
else false
In go every query to a map will return an optional second argument which will tell if a certain key is present or not
https://play.golang.org/p/Hl7MajLJV3T
It's more normal to use ok for the boolean variable name. This is equivalent to:
temp, ok := superhero["Superman"]
if ok {
fmt.Println(temp["realname"], temp["city"])
}
The ok is true if there was a key in the map. So there are two forms of map access built into the language, and two forms of this statement. Personally I think this slightly more verbose form with one more line of code is much clearer, but you can use either.So the other form would be:
if temp, ok := superhero["Superman"]; ok {
fmt.Println(temp["realname"], temp["city"])
}
As above. For more see effective go here:
For obvious reasons this is called the “comma ok” idiom. In this
example, if the key is present, the value will be set appropriately and ok
will be true; if not, the value will be set to zero and ok will be
false.
The two forms for accessing maps are:
// value and ok set if key is present, else ok is false
value, ok := map[key]
// value set if key is present
value := map[key]
I have a golang map like this:
var routes map[string]*rest.Route
I've added a bunch of key/values to it like so:
routes["Index"] = &rest.Route{"GET", "/", handlers.GetIndex}
routes["ListStudents"] = &rest.Route{"GET", "/students", handlers.ListStudents}
routes["ViewStudent"] = &rest.Route{"GET", "/students/:id", handlers.ViewStudent}
routes["CreateStudent"] = &rest.Route{"POST", "/students", handlers.CreateStudent}
routes["UpdateStudent"] = &rest.Route{"PUT", "/students/:id", handlers.UpdateStudent}
routes["DeleteStudent"] = &rest.Route{"DELETE", "/students/:id", handlers.DeleteStudent}
When I call a function, SetRoute on individual values, it works, like so:
handler.SetRoutes(routes["Index"])
However, when I try to iterate through the map with range, it doesn't work. I know it doesn't work because of testing that happens afterwards. However, if I print out the keys and values as I call SetRoute, I can see that the keys and values are what I expect. Specifically, the values are addresses.
// This doesn't work, even though printing out k,v shows nothing wrong...
for k, v := range routes {
err := handler.SetRoutes(v)
fmt.Println(k)
fmt.Println(v)
if err != nil {
log.Fatal(err)
}
}
Printing it out shows:
Index
&{GET / 0x442250}
ListStudents
&{GET /students 0x4422f0}
ViewStudent
&{GET /students/:id 0x4427c0}
UpdateStudent
&{PUT /students/:id 0x443520}
DeleteStudent
&{DELETE /students/:id 0x443a30}
CreateSchool
&{POST /schools 0x444660}
CreateStudent
&{POST /students 0x442ed0}
ListSchools
&{GET /schools 0x443d50}
ViewSchool
&{GET /schools/:id 0x444200}
UpdateSchool
&{PUT /schools/:id 0x444cc0}
DeleteSchool
&{DELETE /schools/:id 0x4453b0}
What am I doing wrong?
It seems that handler.SetRoutes is variadic. You can pass multiple routes to it.
This should work:
routes := []rest.Route{
rest.Route{"GET", "/", handlers.GetIndex},
rest.Route{"GET", "/students", handlers.ListStudents},
// more routes
}
handler.SetRoutes(routes...)
So it turns out this isn't a golang issue, but a go-json-rest issue, which is the library I'm using to create a RESTful API in Go.
Essentially, the SetRoutes function isn't ADDING each subsequent value, but simply replacing. So no matter how many times I call it, only the last call will actually be persisted.
SetRoutes was supposed to be called only once. Also this interface is deprecated. you should use the v3 API, which I think is less confusing:
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
// your routes here ...
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
i have viewed source code of mux,but i just want something simple ,not using all features.
i wanna get value of "id" in the url like /url/{id},setting the value in the req.Form and clean the path like mux.
code like
r:=http.NewServeMux()
r.HandlerFunc("/",func(w http.ResponseWriter,r *http.Request){
// Clean path to canonical form and redirect.
if p := cleanPath(req.URL.Path); p != req.URL.Path {
url := *req.URL
url.Path = p
p = url.String()
//根据HTTP的协议重定向
w.Header().Set("Location", p)
w.WriteHeader(http.StatusMovedPermanently)
return
}
// some code check the url parse id and set to req.form
})
//then add some specific url handlers.
in go doc ,it says long Pattern will take higher priority than short Pattern.
and i want to run something(parse id , clean path etc) before all handlers.
i don't want to give up the features of defaultmux. should i redefine a brand new route,or use http.NewServeMux()? if i use http.NewServerMux(),how should i do to add something while keeping the features?
We have used http://www.gorillatoolkit.org/pkg/mux for over a year in our production stack and have been very happy with it.
For some really simple sites I host I use the built in routing something like this:
package main
import (
"flag"
"fmt"
"net/http"
"os"
)
const (
version = "0.1.0"
)
var (
port uint
)
func init() {
flag.UintVar(&port, "port", 8000, "the port to listen on")
flag.UintVar(&port, "p", 8000, "the port to listen on")
}
func main() {
flag.Parse()
// Retrieve the current working directory.
path, err := os.Getwd()
if err != nil {
panic(err)
}
http.HandleFunc("/gallery/view.aspx", handleGallery)
http.HandleFunc("/gallery/viewLarge.aspx", handleViewLarge)
http.HandleFunc("/ir.ashx", handleImageResize)
http.Handle("/", http.FileServer(http.Dir(path)))
panic(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
Let me know what you want your routes to be and I could give you a more specific example to what you are looking for.