func StatePost() http.HandlerFunc { return http.HandlerFunc(func(pResponse http.ResponseWriter, pRequest *http.Request) { requestPost := new(RequestPost) contentType := pRequest.Header.Get("Content-Type") var err error = nil buf := new(bytes.Buffer) buf.ReadFrom(pRequest.Body) requestBody := buf.Bytes() fmt.Println("Executing State Method POST") // Si el cuerpo de la peticion contiene informacion se procesa, de lo contrario no se procesaran peticiones vacias if len(requestBody) > 0 { if contentType == "application/x-protobuf" || strings.Contains(contentType, "application/x-protobuf") { fmt.Println("Executing Error Protobuf ") p := &example.Person{ Id: proto.Int32(1234), Name: proto.String("John Doe"), Email: proto.String("*****@*****.**"), } data, err := proto.Marshal(p) if err != nil { // In case does not catch the Host name log.Error("It has occur an Error when the proto.Marshal", err) } pResponse.Header().Set("Content-Type", "application/x-protobuf") pResponse.Write(data) } else if contentType == "application/json" || strings.Contains(contentType, "application/json") { err = json.Unmarshal(requestBody, requestPost) if err != nil { fmt.Println("Executing State Method", pResponse, err) } else { // It manege to catch the name of the machine(HostName) hostname, err := os.Hostname() if err != nil { // In case does not catch the Host name log.Error("It has occur an Error when the HostName", err) } fmt.Println("Executing State Method POST inside") state := StateService{"ok", "Tuesday", "6:42 AM", hostname, "ruote"} js, _ := json.Marshal(state) pResponse.Header().Set("Content-Type", "application/json") pResponse.Write(js) } } } }) }
func State() http.HandlerFunc { return http.HandlerFunc(func(pResponse http.ResponseWriter, r *http.Request) { // It manege to catch the name of the machine(HostName) hostname, err := os.Hostname() if err != nil { // In case does not catch the Host name log.Error("It has occur an Error when the HostName", err) } fmt.Println("Executing State Method") state := StateService{"ok", "Tuesday", "6:42 AM", hostname, "ruote"} js, _ := json.Marshal(state) pResponse.Header().Set("Content-Type", "application/json") pResponse.Write(js) }) /* // It manege to catch the name of the machine(HostName) hostname, err := os.Hostname() if err != nil { // In case does not catch the Host name log.Error("It has occur an Error when the HostName", err) } state := StateService{"ok","Tuesday","6:41 AM",hostname,"ruote"} js, _ := json.Marshal(state) pResponse.Header().Set("Content-Type", "application/json") pResponse.Write(js) */ }
func main() { // It is used to get the name of the file flag.Parse() var cfgConfig util.Config // It reads the configuration file and it will filled the fileConfig Struct err1 := gcfg.ReadFileInto(&cfgConfig, *fileConfig) if err1 != nil { panic(err1) } // It sets up the Log File initLogs(cfgConfig.Logs.LogPath, cfgConfig.Logs.LogName, cfgConfig.Logs.LogLevel) //initLogs("/home/ubuntu/workspace/logs/", "DockerServiceLog.log", 1) //This method contains the mapping between URL and Handlers. handler.SetUpHandlers("c9") // If debug is enable, this message will be printed log.Debug("Server Started") // The server is started // Only the ports 8080, 8081 y 8082 can be visibles on the Internet when it works with cloud9 err := http.ListenAndServe(":8080", nil) // If an Error occur if err != nil { log.Error("The service could be started", err.Error()) } }