-
Notifications
You must be signed in to change notification settings - Fork 9
Getting Started
Douglas Hubler edited this page Nov 6, 2022
·
1 revision
From an active Go project with go
command in the PATH
run this command
go get github.com/freeconf/restconf
This is necesssary for some built in services that require YANG files
mkdir yang
go run github.com/freeconf/yang/cmd/fc-yang get -dir 'yang'
Put file in ./yang
to make things easier.
module hello {
revision 0;
leaf message {
config false;
type string;
}
}
There is a lot of flexibility on how you initialize RESTCONF, here is one way
package main
import (
"log"
"github.com/freeconf/restconf"
"github.com/freeconf/restconf/device"
"github.com/freeconf/yang/nodeutil"
"github.com/freeconf/yang/source"
)
// This is not specific to restconf, just example data structure for your
// application
type MyApp struct {
Message string
}
func main() {
// your app instance
app := MyApp{}
// where to find YANG files
ypath := source.Dir("./yang")
// organize modules.
d := device.New(ypath)
// register your application module. you can register as many as you want here.
// param 1 - name of the module, "hello.yang" must exist in ypath
// param 2 - code that connects (bridges) from your App to yang interface
// there are many options in nodeutil package to base your
// implementation on. Here we use reflection because our yang file aligns
// with out application data structure.
d.Add("hello", nodeutil.Reflect{}.Object(&app))
// create the RESTCONF server
restconf.NewServer(d)
// this will apply configuration and startup web server
if err := d.ApplyStartupConfigFile("./startup.json"); err != nil {
log.Fatal(err)
}
select {}
}
Create this file with static configuration for your application and accompanying RESTCONF server
{
"fc-restconf" : {
"web" : {
"port": ":8080"
}
},
"hello" : {
"message" : "hello"
}
}
go run ./main.go
# read data
curl http://localhost:8080/restconf/data/hello:
# set data
curl -X PUT -d '{"message":"goodbye"}' http://localhost:8080/restconf/data/hello:
# read back new data
curl http://localhost:8080/restconf/data/hello:
That's it. See this rest of the documentation on how to expand from here.