Compare commits
	
		
			3 Commits
		
	
	
		
			94222942e6
			...
			9c1057d1ce
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9c1057d1ce | |||
| fa50a2d618 | |||
| 78b274c160 | 
							
								
								
									
										34
									
								
								.drone.yml
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								.drone.yml
									
									
									
									
									
								
							@ -1,35 +1,45 @@
 | 
			
		||||
global-variables:
 | 
			
		||||
  env: &env
 | 
			
		||||
  image: golang
 | 
			
		||||
    commands:
 | 
			
		||||
      - go build -ldflags "-X main.version=${DRONE_TAG##v}" -o release/$GOOS/$GOARCH/ubnt-freifunk-map-api .
 | 
			
		||||
      - tar -cvzf release/ubnt-freifunk-map-api_$GOOS_$GOARCH.tar.gz -C release/$GOOS/$GOARCH ubnt-freifunk-map-api
 | 
			
		||||
 | 
			
		||||
kind: pipeline
 | 
			
		||||
type: docker
 | 
			
		||||
name: build go
 | 
			
		||||
 | 
			
		||||
steps:
 | 
			
		||||
  - name: build linux/amd64
 | 
			
		||||
    image: golang
 | 
			
		||||
    commands:
 | 
			
		||||
      - go build -ldflags "-X main.version=${DRONE_TAG}" -o release/linux/amd64/ubnt-freifunk-map-api .
 | 
			
		||||
      - tar -cvzf release/ubnt-freifunk-map-api_linux_amd64.tar.gz -C release/linux/amd64 ubnt-freifunk-map-api
 | 
			
		||||
  <<: *env
 | 
			
		||||
    environment:
 | 
			
		||||
      GOARCH: amd64
 | 
			
		||||
      GOOS: linux
 | 
			
		||||
 | 
			
		||||
  - name: build linux/arm64
 | 
			
		||||
    image: golang
 | 
			
		||||
    commands:
 | 
			
		||||
      - go build -ldflags "-X main.version=${DRONE_TAG}" -o release/linux/arm64/ubnt-freifunk-map-api .
 | 
			
		||||
      - tar -cvzf release/ubnt-freifunk-map-api_linux_arm64.tar.gz -C release/linux/arm64 ubnt-freifunk-map-api
 | 
			
		||||
  <<: *env
 | 
			
		||||
    environment:
 | 
			
		||||
      GOARCH: arm64
 | 
			
		||||
      GOOS: linux
 | 
			
		||||
 | 
			
		||||
  - name: build windows
 | 
			
		||||
    image: golang
 | 
			
		||||
    commands:
 | 
			
		||||
      - go build -ldflags "-X main.version=${DRONE_TAG}" -o release/windows/amd64/ubnt-freifunk-map-api .
 | 
			
		||||
      - tar -cvzf release/ubnt-freifunk-map-api_windows_amd64.tar.gz -C release/windows/amd64 ubnt-freifunk-map-api
 | 
			
		||||
  <<: *env
 | 
			
		||||
    environment:
 | 
			
		||||
      GOARCH: amd64
 | 
			
		||||
      GOOS: windows
 | 
			
		||||
 | 
			
		||||
  - name: build macos/x64
 | 
			
		||||
  <<: *env
 | 
			
		||||
    environment:
 | 
			
		||||
      GOARCH: amd64
 | 
			
		||||
      GOOS: darwin
 | 
			
		||||
 | 
			
		||||
  - name: build macos/arm64
 | 
			
		||||
  <<: *env
 | 
			
		||||
    environment:
 | 
			
		||||
      GOARCH: arm64
 | 
			
		||||
      GOOS: darwin
 | 
			
		||||
 | 
			
		||||
  - name: docker  
 | 
			
		||||
    image: plugins/docker
 | 
			
		||||
    settings:
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										46
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								main.go
									
									
									
									
									
								
							@ -23,18 +23,27 @@ const (
 | 
			
		||||
// flags
 | 
			
		||||
var token = flag.String("token", "", "Defines the x-auth-token")
 | 
			
		||||
var version = "development"
 | 
			
		||||
var delay time.Duration = 60 * time.Second
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	log.Println("starting version", version, "...")
 | 
			
		||||
	log.Printf("starting version %s...\n", version)
 | 
			
		||||
	// parse all flags
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
 | 
			
		||||
	// check if token is set
 | 
			
		||||
	if *token == "" {
 | 
			
		||||
		log.Fatalln("Please specify an API token via the flag '-token'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// start API processing (runs in a loop)
 | 
			
		||||
	go processAPIs()
 | 
			
		||||
 | 
			
		||||
	// start webserver
 | 
			
		||||
	serveJSON()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func processAPIs() {
 | 
			
		||||
	tick := time.Tick(delay)
 | 
			
		||||
	for range tick {
 | 
			
		||||
		// Variables for runtime
 | 
			
		||||
		var links []link
 | 
			
		||||
		var nodes []node
 | 
			
		||||
@ -131,10 +140,14 @@ func main() {
 | 
			
		||||
 | 
			
		||||
		// create file output
 | 
			
		||||
		log.Println("writing json file")
 | 
			
		||||
	writeJSONtoFile(o)
 | 
			
		||||
 | 
			
		||||
		if err := o.writeToFile(); err != nil {
 | 
			
		||||
			log.Fatalln(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// we're done here
 | 
			
		||||
		log.Println("...done")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getDevices() (devices, error) {
 | 
			
		||||
@ -213,22 +226,19 @@ func addLink(dev unifiAPIResponse, airmaxes unifiAPIAirmax, links []link) []link
 | 
			
		||||
	return links
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func writeJSONtoFile(o output) error {
 | 
			
		||||
	file, err := json.MarshalIndent(o, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("can't marshal to json: %+v", o)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// write to file
 | 
			
		||||
	err = ioutil.WriteFile("example.json", file, 0644)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("can't write to json file example.json")
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getAddresses(ip string) []string {
 | 
			
		||||
	var adresses []string
 | 
			
		||||
	adresses = append(adresses, strings.Split(ip, "/")[0])
 | 
			
		||||
	return adresses
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func serveJSON() {
 | 
			
		||||
	fs := http.FileServer(http.Dir("./output"))
 | 
			
		||||
	http.Handle("/", fs)
 | 
			
		||||
 | 
			
		||||
	log.Println("Listening on :3000...")
 | 
			
		||||
	err := http.ListenAndServe(":3000", nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalln(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										28
									
								
								types.go
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								types.go
									
									
									
									
									
								
							@ -1,6 +1,12 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "time"
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type device struct {
 | 
			
		||||
	Name           string `json:"name"`
 | 
			
		||||
@ -111,6 +117,26 @@ type output struct {
 | 
			
		||||
	Links     []link `json:"links"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (o *output) writeToFile() error {
 | 
			
		||||
	file, err := json.MarshalIndent(o, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("can't marshal to json: %+v", o)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// write to file
 | 
			
		||||
	if _, err := os.Stat("output"); os.IsNotExist(err) {
 | 
			
		||||
		err = os.Mkdir("output", 0755)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("can't create output folder")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	err = ioutil.WriteFile("output/meshviewer.json", file, 0644)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("can't write to json file meshviewer.json")
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type apiResponse struct {
 | 
			
		||||
	StatusCode int    `json:"statusCode"`
 | 
			
		||||
	Error      string `json:"error"`
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user