Skip to content
Permalink
Browse files

Add SIGHUP to reload license list

  • Loading branch information...
paultag committed Apr 22, 2016
1 parent 19d83cc commit 92141c41baef11ad81f0123c77d6835e998013ad
Showing with 66 additions and 11 deletions.
  1. +5 −11 api.go
  2. +61 −0 reload.go
16 api.go
@@ -19,8 +19,6 @@ import (
"encoding/json"
"net/http"
"os"

"github.com/opensourceorg/api/license"
)

//
@@ -44,22 +42,18 @@ func writeError(w http.ResponseWriter, message string, code int) error {

func main() {
mux := http.NewServeMux()
licenses, err := license.LoadLicensesFiles(os.Args[1])
if err != nil {
panic(err)
}
blob := Blobs{}

licenseIdMap := licenses.GetIdMap()
licenseTagMap := licenses.GetTagMap()
go Reloader(os.Args[1], &blob)

licensesEndpoint := "/licenses/"
mux.HandleFunc(licensesEndpoint, func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == licensesEndpoint {
writeJSON(w, licenses, 200)
writeJSON(w, blob.Licenses, 200)
return
}
path := req.URL.Path[len(licensesEndpoint):]
if licenses, ok := licenseTagMap[path]; ok {
if licenses, ok := blob.LicenseTagMap[path]; ok {
writeJSON(w, licenses, 200)
return
}
@@ -69,7 +63,7 @@ func main() {
licenseEndpoint := "/license/"
mux.HandleFunc(licenseEndpoint, func(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path[len(licenseEndpoint):]
if license, ok := licenseIdMap[path]; ok {
if license, ok := blob.LicenseIdMap[path]; ok {
writeJSON(w, license, 200)
return
}
@@ -0,0 +1,61 @@
/* {{{ Copyright (c) Paul R. Tagliamonte <paultag@opensource.org>, 2015-2016
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. }}} */

package main

import (
"os"
"os/signal"
"syscall"

"github.com/opensourceorg/api/license"
)

type Blobs struct {
Licenses license.Licenses
LicenseIdMap map[string]license.License
LicenseTagMap map[string][]license.License
}

func loadBlob(path string, blob *Blobs) error {
licenses, err := license.LoadLicensesFiles(path)
if err != nil {
return err
}
licenseIdMap := licenses.GetIdMap()
licenseTagMap := licenses.GetTagMap()

blob.Licenses = licenses
blob.LicenseIdMap = licenseIdMap
blob.LicenseTagMap = licenseTagMap

return nil
}

func Reloader(file string, target *Blobs) {
if err := loadBlob(file, target); err != nil {
panic(err)
}

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
for _ = range c {
if err := loadBlob(file, target); err != nil {
panic(err)
}
}
}

// vim: foldmethod=marker

0 comments on commit 92141c4

Please sign in to comment.
You can’t perform that action at this time.