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.
Related
I have the following mind-blowingly simple Go code.
package main
import (
"fmt"
"net/http"
)
func main() {
h := http.Header{
"my_id": []string{"XYZ"},
}
fmt.Println("h.Get(\"my_id\") = ", h.Get("my_id"))
}
But when I run it it doesn't work as expected, Here is the output:
h.Get("my_id") =
Why can't I print out the value of the header I just set?
Here is the live code for you to see yourself: https://play.golang.org/p/L45LzVqd341
Header.Get uses http.CanonicalHeaderKey to lookup keys. If you use h.Set or put My_id, it will work.
This is explained in Header.Get documentation.
Yeah the Headers are just a map[string][]string. So you can always get access to them by simply
if myID, ok := h["my_id"]; ok {
fmt.Println("myID", myID)
} else {
fmt.Println("my_id header not found")
}
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 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
I am trying to pass contextInfo of typeUnsafeMutablePointer<Void> to UISaveVideoAtPathToSavedPhotosAlbum and use it in the callback function. For some reason I am unable to access contextInfo as a string using UnsafePointer<String>(x).memory when I am in the callback function.
I am pretty sure it is something simple I am missing but have spent way to many hours trying to figure this out.
Below is some code that I have tried.
The following code works.
var testStr:String = "hello"
takesAMutableVoidPointer(&testStr)
func takesAMutableVoidPointer(x: UnsafeMutablePointer<Void>){
var pStr:String = UnsafePointer<String>(x).memory
println("x = \(x)")
println("pStr = \(pStr)")
}
However the following code does not work.
var testStr:String = "hello"
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath){ //the filePath is compatible
println("Compatible")
//UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, nil, nil)
UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, "video:didFinishSavingWithError:contextInfo:", &testStr)
}
else{
println("Not Compatible")
}
func video(video: NSString, didFinishSavingWithError error:NSError, contextInfo:UnsafeMutablePointer<Void>){
var pStr:String = UnsafePointer<String>(contextInfo).memory
println("contextInfo = \(contextInfo)")
println("pStr = \(pStr)")
}
Once I get to the following line:
var pStr:String = UnsafePointer<String>(contextInfo).memory
I keep getting the following error:
Thread 1: EXC_BAD_ACCESS(code=1, address=0x0)
Any help with this would be greatly appreciated.
Thanks.
Update
Rintaro commented that testStr needs to be top level but the following code works.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var testStr:String = "hello"
takesAMutableVoidPointer(&testStr)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func takesAMutableVoidPointer(x: UnsafeMutablePointer<Void>){
var answer = UnsafePointer<String>(x).memory
println("x = \(x)")
println("answer = \(answer)")
}
}
I am trying not to use global variables unless I have to. I may have to but since I am able to execute the above code, it seems as though I do not need to use a global variable.
As discussed in OP comments, testStr has already been freed.
Is there any way to force the retaining of a variable that has been created in a function? Then release it later?
It's not impossible, but I don't know this is the best way to do that.
Anyway, try this with Playground or OS X "Command Line Tool" template:
import Foundation
func foo() {
var str:NSString = "Hello World"
let ptr = UnsafePointer<Void>(Unmanaged<NSString>.passRetained(str).toOpaque())
bar(ptr)
}
func bar(v:UnsafePointer<Void>) {
let at = dispatch_time(
DISPATCH_TIME_NOW,
Int64(2.0 * Double(NSEC_PER_SEC))
)
dispatch_after(at, dispatch_get_main_queue()) {
baz(v)
}
}
func baz(v:UnsafePointer<Void>) {
println("notified")
let str = Unmanaged<NSString>.fromOpaque(COpaquePointer(v)).takeRetainedValue()
println("info: \(str)")
}
foo()
println("started")
dispatch_main()
Unmanaged<NSString>.passRetained(str) increments the retain count.
Unmanaged<NSString>.fromOpaque(...).takeRetainedValue() decrements it, and extract the object.
I think, using pure Swift String is impossible. because String is struct and is allocated in stack memory. Maybe the buffer of it is allocated in heap, but we cannot access it directly.
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()))